如何在pytest中对固定装置施加顺序?
How to impose order on fixtures in pytest?
我正在尝试使用 pytest-dependency 使 fixtures 按顺序发生,无论它们如何命名,也无论它们在测试的参数列表。
我需要这个的原因是创建需要初始化的固定装置,它依赖于其他需要初始化的固定装置,并且它们必须按顺序发生。我有很多这样的东西,我不想依赖命名或参数列表中的顺序。
我也不想使用 pytest_sessionstart
,因为它不能接受 fixture 输入,这会导致非常不干净的代码。
the doc 中的简单示例展示了如何为测试创建编程依赖项:
import pytest
@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
assert False
@pytest.mark.dependency()
def test_b():
pass
@pytest.mark.dependency(depends=["test_a"])
def test_c():
pass
@pytest.mark.dependency(depends=["test_b"])
def test_d():
pass
@pytest.mark.dependency(depends=["test_b", "test_c"])
def test_e():
pass
这适用于输出:
============================= test session starts =============================
collecting ... collected 5 items
test_sanity.py::test_z XFAIL (deliberate fail) [ 20%]
@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_z():
> assert False
E assert False
test_sanity.py:6: AssertionError
test_sanity.py::test_x PASSED [ 40%]
test_sanity.py::test_c SKIPPED (test_c depends on test_z) [ 60%]
Skipped: test_c depends on test_z
test_sanity.py::test_d PASSED [ 80%]
test_sanity.py::test_w SKIPPED (test_w depends on test_c) [100%]
Skipped: test_w depends on test_c
=================== 2 passed, 2 skipped, 1 xfailed in 0.05s ===================
现在我想对灯具做同样的事情。
我的尝试:
conftest.py
:
import pytest
pytest_plugins = ["dependency"]
@pytest.mark.dependency()
@pytest.fixture(autouse=True)
def zzzshould_happen_first():
assert False
@pytest.mark.dependency(depends=["zzzshould_happen_first"])
@pytest.fixture(autouse=True)
def should_happen_last():
assert False
和test_sanity.py
:
def test_nothing():
assert True
产出
test_sanity.py::test_nothing ERROR [100%]
test setup failed
@pytest.mark.dependency(depends=["zzzshould_happen_first"])
@pytest.fixture(autouse=True)
def should_happen_last():
> assert False
E assert False
conftest.py:15: AssertionError
我预计 zzzshould_happen_first
会出现错误。
有没有办法对灯具进行排序,这样
- 他们的名字被忽略
- 它们在参数列表中的顺序被忽略
- 其他pytest特性如
autouse
仍然可以应用
您可以直接使用 pytest 将夹具作为依赖项提供。像这样:
import pytest
@pytest.fixture(autouse=True)
def zzzshould_happen_first():
assert False
@pytest.fixture(autouse=True)
def should_happen_last(zzzshould_happen_first):
assert False
def test_nothing():
assert True
它给了你想要的:
test setup failed
@pytest.fixture(autouse=True)
def zzzshould_happen_first():
> assert False
E assert False
answer.py:7: AssertionError
我正在尝试使用 pytest-dependency 使 fixtures 按顺序发生,无论它们如何命名,也无论它们在测试的参数列表。
我需要这个的原因是创建需要初始化的固定装置,它依赖于其他需要初始化的固定装置,并且它们必须按顺序发生。我有很多这样的东西,我不想依赖命名或参数列表中的顺序。
我也不想使用 pytest_sessionstart
,因为它不能接受 fixture 输入,这会导致非常不干净的代码。
the doc 中的简单示例展示了如何为测试创建编程依赖项:
import pytest
@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
assert False
@pytest.mark.dependency()
def test_b():
pass
@pytest.mark.dependency(depends=["test_a"])
def test_c():
pass
@pytest.mark.dependency(depends=["test_b"])
def test_d():
pass
@pytest.mark.dependency(depends=["test_b", "test_c"])
def test_e():
pass
这适用于输出:
============================= test session starts =============================
collecting ... collected 5 items
test_sanity.py::test_z XFAIL (deliberate fail) [ 20%]
@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_z():
> assert False
E assert False
test_sanity.py:6: AssertionError
test_sanity.py::test_x PASSED [ 40%]
test_sanity.py::test_c SKIPPED (test_c depends on test_z) [ 60%]
Skipped: test_c depends on test_z
test_sanity.py::test_d PASSED [ 80%]
test_sanity.py::test_w SKIPPED (test_w depends on test_c) [100%]
Skipped: test_w depends on test_c
=================== 2 passed, 2 skipped, 1 xfailed in 0.05s ===================
现在我想对灯具做同样的事情。
我的尝试:
conftest.py
:
import pytest
pytest_plugins = ["dependency"]
@pytest.mark.dependency()
@pytest.fixture(autouse=True)
def zzzshould_happen_first():
assert False
@pytest.mark.dependency(depends=["zzzshould_happen_first"])
@pytest.fixture(autouse=True)
def should_happen_last():
assert False
和test_sanity.py
:
def test_nothing():
assert True
产出
test_sanity.py::test_nothing ERROR [100%]
test setup failed
@pytest.mark.dependency(depends=["zzzshould_happen_first"])
@pytest.fixture(autouse=True)
def should_happen_last():
> assert False
E assert False
conftest.py:15: AssertionError
我预计 zzzshould_happen_first
会出现错误。
有没有办法对灯具进行排序,这样
- 他们的名字被忽略
- 它们在参数列表中的顺序被忽略
- 其他pytest特性如
autouse
仍然可以应用
您可以直接使用 pytest 将夹具作为依赖项提供。像这样:
import pytest
@pytest.fixture(autouse=True)
def zzzshould_happen_first():
assert False
@pytest.fixture(autouse=True)
def should_happen_last(zzzshould_happen_first):
assert False
def test_nothing():
assert True
它给了你想要的:
test setup failed
@pytest.fixture(autouse=True)
def zzzshould_happen_first():
> assert False
E assert False
answer.py:7: AssertionError