pytest,检索调用夹具的测试

pytest, retrieve test that call fixture

pytest fixture 中有没有办法找出调用它的测试?

我尝试过 inspect 但 returns call_fixture_func ... conftest.py ...

@fixture(scope="function")
def resource(app_config):
    curframe = inspect.currentframe()
    caller = inspect.getouterframes(curframe, 2)[1][3]
    resource = app_config.handler.get(caller)
    yield resource
    resource.finish_test()

...测试文件...

@mark.smoke
def test_resource_is_happy(resource):
    print(f"Test starting on {resource.name}")
    ....

谢谢

是的,有办法:

@pytest.fixture
def your_fixture(request):
    print(request.module)
    print(request.cls)
    print(request.function)


def test_something(your_fixture):
    pass

输出:

test.py <module 'test' from '/path/to/file/test.py'>
None
<function test_something at 0x7fa5205bd140>

在此处阅读更多信息:https://docs.pytest.org/en/2.8.7/builtin.html#_pytest.python.FixtureRequest