pytest,检索调用夹具的测试[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

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

我尝试使用inspect但是返回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}")
    ....

谢谢

python testing pytest fixtures
1个回答
1
投票

是的,有一种方法:

@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

© www.soinside.com 2019 - 2024. All rights reserved.