0

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 ======================================================
CarolL
  • 59
  • 2
  • 8
  • Can you show the test output, please? That should give you a hint what is happening. And please don't use screenshots but use actual code as text instead, check [how to ask a good question](https://stackoverflow.com/help/how-to-ask). – MrBean Bremen Sep 13 '21 at 16:37
  • @MrBeanBremen Thanks for the reminder. Updated the questions with code snippet copied from the pytest-dependency doc. – CarolL Sep 14 '21 at 22:37
  • 1
    Ok, the first problem is that `pytest-dependency` expects test nodes names, which never will contain relative paths like ".", so in your case it may be something like "mod_tests/test_mod_01.py::test_c" instead of "./test_mod_01.py::test_c". The second problem is that the output you show is only from one test module - to make this work, you have to run all tests at once, otherwise `pytest-dependency` will not know about the tests in "test_mod_01". – MrBean Bremen Sep 15 '21 at 15:41
  • 1
    @MrBeanBremen Great, it works after removing ./ from the test nodes name. And you are absolutely correct, required to run test_mod_01 and test_mod_02 at once. Would you like to add your answer, so that I can accept it for your credit. – CarolL Sep 15 '21 at 16:59

1 Answers1

1

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.

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.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46