我在 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()
测试模块.py
import conftest
helper = conftest.helper
def test_method1(sample):
helper.foo()
...