Ad

Can't Make Pytest Dependency Work For Different Modules

- 1 answer

Following this answer and the doc, I am trying to make tests depend on each other between files:

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

and test_services_up.py:

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

The folder structure:

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

Output:

> ssh://[email protected]: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 =========================

How to not skip dependent tests?

Ad

Answer

As mentioned in the comments, pytest-dependency does not order tests, it relies on the tests executed in the correct order for dependencies to work. The reasoning behind this is that pytest-dependency is thought to do one thing and one thing only, which is to skip tests depending on the test relationships. Ordering can either be done manually by arranging the tests accordingly (e.g. adapt the names, what most users seem to do), or rely on an ordering plugin. There has been a controversial discussion about this, and certainly not everyone agrees with that philosophy (I tend to agree), but in the end it is the prerogative of the plugin author to decide.

pytest-order works together with pytest-dependency. If it is installed and you run your tests with the option --order-dependencies it will order your tests with dependency markers if needed. As usual, you can instead add the option to pytest.ini:

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

As for tests that cannot be found, it should issue a respective warning.

Disclaimer:
I'm the maintainer of pytest-order.

Ad
source: stackoverflow.com
Ad