Session scope with pytest-dependency

532 views Asked by At

Referring to the sample code copied from pytest-dependency, slight changes by removing "tests" folder, I am expecting "test_e" and "test_g" to pass, however, both are skipped. Kindly advise if I have done anything silly that stopping the session scope from working properly.

Note:

  • pytest-dependency 0.5.1 is used.
  • Both modules are stored relative to the current working directory respectively.

test_mod_01.py

import pytest

@pytest.mark.dependency()
def test_a():
    pass

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
    def test_b():
       assert False

@pytest.mark.dependency(depends=["test_a"])
def test_c():
    pass

class TestClass(object):

    @pytest.mark.dependency()
    def test_b(self):
        pass

test_mod_02.py

import pytest

@pytest.mark.dependency()
@pytest.mark.xfail(reason="deliberate fail")
def test_a():
    assert False

@pytest.mark.dependency(
    depends=["./test_mod_01.py::test_a", "./test_mod_01.py::test_c"],
    scope='session'
)
def test_e():
    pass

@pytest.mark.dependency(
    depends=["./test_mod_01.py::test_b", "./test_mod_02.py::test_e"],
    scope='session'
)
def test_f():
    pass

@pytest.mark.dependency(
    depends=["./test_mod_01.py::TestClass::test_b"],
    scope='session'
)
def test_g():
    pass

Unexpected output

=========================================================== test session starts ===========================================================
...
collected 4 items                                                                                                                         

test_mod_02.py xsss                                                                                                                 
[100%]

====================================================== 3 skipped, 1 xfailed in 0.38s ======================================================

Expected output

=========================================================== test session starts ===========================================================
...
collected 4 items                                                                                                                         

test_mod_02.py x.s.                                                                                                                 
[100%]

====================================================== 2 passed, 1 skipped, 1 xfailed in 0.38s ======================================================
1

There are 1 answers

0
MrBean Bremen On BEST ANSWER

The first problem is that pytest-dependency uses the full test node names if used in session scope. That means that you have to exactly match that string, which never contains relative paths like "." in your case. Instead of using "./test_mod_01.py::test_c", you have to use something like "tests/test_mod_01.py::test_c", or "test_mod_01.py::test_c", depending where your test root is

An easy way to find out the needed names is to look at the test output using pytest -v. Pytest shows the test ids (e.g. the unique names of the tests) in the output exactly as they are needed in the depends argument, e.g. in this case if you get something like:

$ pytest -v
...
test_mod_01.py::test_a  PASSED
test_mod_01.py::test_b  PASSED
test_mod_01.py::test_c  PASSED
test_mod_02.py::test_a  PASSED
...

you know that you for example use depends=["test_mod_01.py::test_a"].

The second problem is that pytest-dependency will only work if the tests other tests are depend on are run before in the same test session, e.g. in your case both test_mod_01 and test_mod_02 modules have to be in the same test session. The test dependencies are looked up at runtime in the list of tests that already have been run.

Note that this also means that you cannot make tests in test_mod_01 depend on tests in test_mod_02, if you run the tests in the default order. You have to ensure that the tests are run in the correct order either by adapting the names accordingly, or by using some ordering plugin like pytest-order, which has an option (--order-dependencies) to order the tests if needed in such a case.

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