-1

按照这个答案文档,我试图让测试在文件之间相互依赖:

test_connectivity.py

@pytest.mark.dependency(depends=["test_services_up.py::test_sssss"], scope="session")
def test_aaa():
    assert False


@pytest.mark.dependency(depends=["tests/test_services_up.py::test_sssss"], scope="session")
def test_bbb():
    assert False

@pytest.mark.dependency(depends=["atp/tests/test_services_up.py::test_sssss"], scope="session")
def test_ccc():
    assert False

test_services_up.py

@pytest.mark.dependency()
def test_sssss():
    assert True

文件夹结构:

    atp
    ----|tests
    --------|test_connectivity.py
    --------|test_services_up.py

输出:

> ssh://me@host:port~/src/uv-fleet-atp/venv/bin/python -u ~/.pycharm_helpers/pycharm/_jb_pytest_runner.py --path ~/src/uv-fleet-atp/atp/tests
============================= test session starts ==============================
collected 4 items                                                              

test_connectivity.py::test_aaa SKIPPED (test_aaa depends on test_ser...)
Skipped: test_aaa depends on test_services_up.py::test_sssss

test_connectivity.py::test_bbb SKIPPED (test_bbb depends on tests/te...)
Skipped: test_bbb depends on tests/test_services_up.py::test_sssss

test_connectivity.py::test_ccc SKIPPED (test_ccc depends on atp/test...)
Skipped: test_ccc depends on atp/tests/test_services_up.py::test_sssss

test_services_up.py::test_sssss PASSED


========================= 1 passed, 3 skipped in 0.35s =========================

如何不跳过依赖测试?

4

1 回答 1

1

如评论中所述,pytest-dependency不排序测试,它依赖于以正确顺序执行的测试才能使依赖项起作用。这背后的原因是pytest-dependency被认为只做一件事和一件事,即根据测试关系跳过测试。排序可以通过相应地安排测试来手动完成(例如,调整名称,大多数用户似乎都在做),或者依赖一个排序插件。对此有过争议性的讨论,当然不是每个人都同意这种哲学(我倾向于同意),但最终决定权是插件作者的特权。

pytest-orderpytest-dependency. 如果它已安装并且您使用选项--order-dependencies运行测试,它将dependency在需要时使用标记对您的测试进行排序。像往常一样,您可以改为将选项添加到pytest.ini

[pytest]
; always order tests with dependency markers
addopts = --order-dependencies

对于找不到的测试,它应该发出相应的警告。

免责声明:
我是pytest-order.

于 2022-02-13T11:22:38.247 回答