【发布时间】:2015-07-04 17:51:53
【问题描述】:
我的项目中有以下文件树:
...
tests/
__init__.py
test_abc.py
...
我在__init__.py 中定义了一个夹具:
@pytest.fixture(autouse=True)
def client():
return TestClient()
我想在test_abc.py使用这个夹具:
from . import client
def test_def():
client.get(...) # Throws an error
如何在我的其他模块的测试方法中使用我的 autouse 夹具而不传递对夹具的显式引用或使用 pytest 装饰器?
【问题讨论】:
-
您应该将夹具放入
conftest文件:pytest.org/latest/… -
我不想把它放在那里...这是解决夹具的唯一方法吗?
-
另一种方式是写一个插件:pytest.org/latest/…
-
我已经尝试将灯具移动到
conftest.py,但仍然是NameError: global name 'client' is not defined。 -
在你的测试方法中声明一个带有夹具名称的参数:
def test_def(client):。