This question already has answers here:
py.test: how to get the current test's name from the setup method?
                                
                                    (6个答案)
                                
                        
                                在10个月前关闭。
            
                    
pytest固定装置中是否有办法弄清楚什么测试叫它?

我尝试用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}")
    ....


谢谢

最佳答案

是的,有一种方法:

@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

关于python - pytest,检索调用fixture的测试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54946126/

10-15 20:05