我想要实现的基本上是这个,但是有一个类范围的参数化夹具。
问题是,如果我generate_fixture and inject_fixture
从帮助文件中导入方法 (),则注入夹具代码似乎被调用得太晚了。这是一个完整的工作代码示例:
# all of the code in one file
import pytest
import pytest_check as check
def generate_fixture(params):
@pytest.fixture(scope='class', params=params)
def my_fixture(request, session):
request.cls.param = request.param
print(params)
return my_fixture
def inject_fixture(name, someparam):
globals()[name] = generate_fixture(someparam)
inject_fixture('myFixture', 'cheese')
@pytest.mark.usefixtures('myFixture')
class TestParkingInRadius:
def test_custom_fixture(self):
check.equal(True, self.param, 'Sandwhich')
如果我将生成和注入助手移动到他们自己的文件中(根本不更改它们),我会得到一个未找到固定装置的错误,即如果测试文件看起来像这样:
import pytest
import pytest_check as check
from .helpers import inject_fixture
inject_fixture('myFixture', 'cheese')
@pytest.mark.usefixtures('myFixture')
class TestParkingInRadius:
def test_custom_fixture(self):
check.equal(True, self.param, 'Sandwhich')
我在设置时遇到错误:E fixture 'myFixture' not found
后跟可用夹具列表(不包括注入的夹具)。
有人可以帮助解释为什么会这样吗?必须在每个测试文件中定义这些函数有点违背了这样做的全部意义(保持干燥)。