首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否不鼓励在测试模块中导入conftest.py?

是否不鼓励在测试模块中导入conftest.py?
EN

Stack Overflow用户
提问于 2021-08-11 07:54:01
回答 1查看 1.5K关注 0票数 1

我正在conftest.py中创建一个对象,并在一些固定环境中使用它。我还需要在我的测试模块中使用这个对象。目前,我正在测试模块中导入conftest.py,并使用该‘助手’对象。我很肯定这不是推荐的方法。我期待着你的建议。

谢谢您:)

以下是我问题的虚拟编码版本:

conftest.py

代码语言:javascript
复制
import pytest

class Helper():

    def __init__(self, img_path:str):
        self.img_path = img_path

    def grayscale(self):
        pass

    def foo(self):
        pass

helper = Helper("sample.png")

@pytest.fixture()
def sample():
    return helper.grayscale()

test_module.py

代码语言:javascript
复制
import conftest

helper = conftest.helper

def test_method1(sample):
    helper.foo()
    ...
EN

回答 1

Stack Overflow用户

发布于 2021-08-12 03:28:33

如前所述,如果我在测试中有一个助手类的话,我以前也会通过补丁来处理这样的场景。

conftest.py

代码语言:javascript
复制
import pytest


class Helper():
    def __init__(self, img_path: str):
        self.img_path = img_path

    def grayscale(self):
        pass

    def foo(self):
        pass


@pytest.fixture(scope="session")
def helper():
    return Helper("sample.png")


@pytest.fixture()
def sample(helper):
    return helper.grayscale()

test_module.py

代码语言:javascript
复制
def test_method1(helper, sample):
    helper.foo()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68738078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档