我想知道如何通过当前运行测试模块的夹具,或如何知道夹具?(我需要为包中的每个测试模块加载特定的配置文件)。
我可以在每个测试模块中创建夹具,但是我想要更通用的解决方案。提前谢谢。
发布于 2018-11-20 09:28:31
通常,您可以在这样的夹具中获得模块名:
@pytest.fixture
def fixture_global(request):
module_name = request.module.__name__
print(module_name)
# some logic depends on module name它甚至可能是conftest中的全局夹具,但通过这种方式,不适用于会话范围固定设备,因为它们并不是每个使用它的模块都被调用的。
如果您想要一个基本共享的夹具代码,以及一些特定模块的特定代码,我会建议一个更好的方法。
在全局conftest.py中放置带有通用代码的基座夹具。如果希望将其用于特定的测试模块,只需将全局夹具作为参数注入本地夹具即可。就像这样:
conftest.py
@pytest.fixture
def global_fixture():
# universal code for vary modules
return universal_objtest_module.py
@pytest.fixture
def local_fixture(global_fixture):
# specific code that uses global fixture resulthttps://stackoverflow.com/questions/53389396
复制相似问题