Can't make pytest dependency work for different modules

1.2k views Asked by At

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

How to not skip dependent tests?

2

There are 2 answers

12
MrBean Bremen On BEST 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.

0
Matelko On

I had the same issue, but after while I found pytest-depends plugin, which is close similar to the dependency plugin but the main difference is 'depends' works as you want. So If you have all tests in one direction but the dependency is only on test files then you can try with:

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


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

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


test_services_up.py
@pytest.mark.depends()
def test_sssss():
    assert True

This should help and work, 'depends' plugin has also auto order, which means that it will run firstly main tests. You can find more here: https://pypi.org/project/pytest-depends/