在tes末尾获取pytest中所有请求的fixture的列表

2024-04-16 15:58:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下示例代码:

# In my conftest.py
@pytest.mark.fixture(scope="function")
def func_fix():
    ...

@pytest.mark.fixture(scope="session")
def sess_fix():
    ...

def pytest_unconfigure(config):
    # HELP NEEDED

# In my test code:
def test_foo(sess_fix, func_fix):
    ...

我想根据是否请求了func_fix(),在pytest_unconfigure中执行操作。在另一个获得request的fixture中,我可以检查request.fixturenames,但我不确定如何在pytest_unconfigure()中执行此操作。我当前的解决方案包括在func_fix()中设置config.foo = bar,然后在pytest_unconfigure()中检查该设置,这在假设一切正常的情况下有效。在

问题是当sess_fix()失败时,config.foo = bar显然没有设置,所以我无法在pytest_unconfigure()中采取行动。在

我不需要func_fix()来实际运行。我只需要知道用户想运行它。在

编辑:我承认我有点滥用定稿器。我也欢迎任何关于更好地处理拆卸功能的建议。在

有办法吗?在


Tags: intestconfigfoopytestrequestmydef