我正在conftest.py中创建一个对象,并在一些固定环境中使用它。我还需要在我的测试模块中使用这个对象。目前,我正在测试模块中导入conftest.py,并使用该‘助手’对象。我很肯定这不是推荐的方法。我期待着你的建议。
谢谢您:)
以下是我问题的虚拟编码版本:
conftest.py
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
import conftest
helper = conftest.helper
def test_method1(sample):
helper.foo()
...发布于 2021-08-12 03:28:33
如前所述,如果我在测试中有一个助手类的话,我以前也会通过补丁来处理这样的场景。
conftest.py
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
def test_method1(helper, sample):
helper.foo()https://stackoverflow.com/questions/68738078
复制相似问题