Using pytest-dependency

The plugin defines a new marker pytest.mark.dependency().

Basic usage

Consider the following example test module:

import pytest

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

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

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

@pytest.mark.dependency(depends=["test_b"])
def test_d():
    pass

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

All the tests are decorated with pytest.mark.dependency(). This will cause the test results to be registered internally and thus other tests may depend on them. The list of dependencies of a test may be set in the optional depends argument to the marker. Running this test, we will get the following result:

$ py.test -rsx basic.py
============================= test session starts ==============================
platform linux -- Python 3.4.6, pytest-2.8.7, py-1.4.30, pluggy-0.3.1
rootdir: /home/user/pytest-dependency-0.2, inifile: 
plugins: dependency-0.2
collected 5 items 

basic.py x.s.s
=========================== short test summary info ============================
SKIP [1] /usr/lib/python3.4/site-packages/pytest_dependency.py:65: test_e depends on test_c
SKIP [1] /usr/lib/python3.4/site-packages/pytest_dependency.py:65: test_c depends on test_a
XFAIL basic.py::test_a
  deliberate fail

================ 2 passed, 2 skipped, 1 xfailed in 0.02 seconds ================

The first test has deliberately been set to fail to illustrate the effect. We will get the following resuts:

test_a
deliberatly fails.
test_b
succeeds.
test_c
will be skipped because it depends on test_a.
test_d
depends on test_b which did succeed. It will be run and succeed as well.
test_e
depends on test_b and test_c. test_b did succeed, but test_c has been skipped. So this one will also be skipped.

Naming tests

Tests are referenced by their name in the depends argument. The default for this name is the node ID defined by pytest, that is the name of the test function, extended by the parameters if applicable. In some cases, it’s not easy to predict the names of the node IDs. For this reason, the name of the tests can be overridden by an explicit name argument to the marker. The following example works exactly as the last one, only the test names are explicitely set:

import pytest

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

@pytest.mark.dependency(name="b")
def test_b():
    pass

@pytest.mark.dependency(name="c", depends=["a"])
def test_c():
    pass

@pytest.mark.dependency(name="d", depends=["b"])
def test_d():
    pass

@pytest.mark.dependency(name="e", depends=["b", "c"])
def test_e():
    pass

Using test classes

Tests may be grouped in classes in pytest. Marking the dependencies of methods in test classes works the same way as for simple test functions. In the following example we define two test classes. Each works in the same manner as the previous examples respectively:

import pytest


class TestClass(object):

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

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

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

    @pytest.mark.dependency(depends=["TestClass::test_b"])
    def test_d(self):
        pass

    @pytest.mark.dependency(depends=["TestClass::test_b", "TestClass::test_c"])
    def test_e(self):
        pass


class TestClassNamed(object):

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

    @pytest.mark.dependency(name="b", depends=["a"])
    def test_b(self):
        pass

    @pytest.mark.dependency(name="c")
    def test_c(self):
        pass

    @pytest.mark.dependency(name="d", depends=["c"])
    def test_d(self):
        pass

    @pytest.mark.dependency(name="e", depends=["b", "c"])
    def test_e(self):
        pass

In TestClass the default names for the tests are used, which is build from the name of the class and the respective method in this case, while in TestClassNamed these names are overridden by an explicit name argument to the pytest.mark.dependency() marker.

Parametrized tests

In the same way as the pytest.mark.skip() and pytest.mark.xfail() markers, the pytest.mark.dependency() marker may be applied to individual test instances in the case of parametrized tests. Consider the following example:

import pytest

@pytest.mark.parametrize("x,y", [
    pytest.mark.dependency(name="a1")((0,0)),
    pytest.mark.dependency(name="a2")(pytest.mark.xfail((0,1))),
    pytest.mark.dependency(name="a3")((1,0)),
    pytest.mark.dependency(name="a4")((1,1))
])
def test_a(x,y):
    assert y <= x

@pytest.mark.parametrize("u,v", [
    pytest.mark.dependency(name="b1", depends=["a1", "a2"])((1,2)),
    pytest.mark.dependency(name="b2", depends=["a1", "a3"])((1,3)),
    pytest.mark.dependency(name="b3", depends=["a1", "a4"])((1,4)),
    pytest.mark.dependency(name="b4", depends=["a2", "a3"])((2,3)),
    pytest.mark.dependency(name="b5", depends=["a2", "a4"])((2,4)),
    pytest.mark.dependency(name="b6", depends=["a3", "a4"])((3,4))
])
def test_b(u,v):
    pass

@pytest.mark.parametrize("w", [
    pytest.mark.dependency(name="c1", depends=["b1", "b2", "b6"])(1),
    pytest.mark.dependency(name="c2", depends=["b2", "b3", "b6"])(2),
    pytest.mark.dependency(name="c3", depends=["b2", "b4", "b6"])(3)
])
def test_c(w):
    pass

The test instance test_a[0-1], named a2 in the pytest.mark.dependency() marker, is going to fail. As a result, the dependent tests b1, b4, b5, and in turn c1 and c3 will be skipped.

Marking dependencies at runtime

Sometimes, dependencies of test instances are too complicated to be formulated explicitely beforehand using the pytest.mark.dependency() marker. It may be easier to compile the list of dependencies of a test at run time. In such cases, the function pytest_dependency.depends() comes handy. Consider the following example:

import pytest
from pytest_dependency import depends

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

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

@pytest.mark.dependency()
def test_c(request):
    depends(request, ["test_b"])
    pass

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

Tests test_c and test_d set their dependencies at runtime calling pytest_dependency.depends(). The first argument is the value of the request pytest fixture, the second argument is the list of dependencies. It has the same effect as passing this list as the depends argument to the pytest.mark.dependency() marker.

The present example is certainly somewhat artificial, as the use of the pytest_dependency.depends() function would not be needed in such a simple case. For a more involved example that can not as easily be formulated with the static the depends argument, see Grouping tests using fixtures.