无法使 pytest 依赖项适用于不同的模块

Can't make pytest dependency work for different modules

and the doc 之后,我正在尝试使文件之间的测试相互依赖:

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 =========================

如何不跳过相关测试?

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

pytest-order works together with pytest-dependency. If it is installed and you run your tests with the option --order-dependencies 如果需要,它将使用 dependency 标记对您的测试进行排序。像往常一样,您可以将选项添加到 pytest.ini:

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

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

免责声明:
我是 pytest-order.

的维护者