Skip to content

Commit 65dc1f5

Browse files
committed
Document that the dependency marker may be applied to a class as a
whole. Close #39.
1 parent a8cb125 commit 65dc1f5

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

doc/examples/mark-class.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
4+
@pytest.mark.dependency()
5+
@pytest.mark.xfail(reason="deliberate fail")
6+
def test_f():
7+
assert False
8+
9+
10+
@pytest.mark.dependency(depends=["test_f"])
11+
class TestClass(object):
12+
13+
def test_a(self):
14+
pass
15+
16+
@pytest.mark.dependency()
17+
def test_b(self):
18+
pass
19+
20+
def test_c(self):
21+
pass

doc/src/usage.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ explicit `name` argument to the :func:`pytest.mark.dependency` marker.
7373
The name of the class is prepended to the method name to form the
7474
default name for the test.
7575

76+
Applying the dependency marker to a class as a whole
77+
----------------------------------------------------
78+
79+
The :func:`pytest.mark.dependency` marker may also be applied to a test
80+
class as a whole. This has the same effect as applying that marker
81+
with the same arguments to each method of the class individually.
82+
Consider:
83+
84+
.. literalinclude:: ../examples/mark-class.py
85+
86+
The tests `TestClass::test_a` and `TestClass::test_c` will be skipped,
87+
because they depend on `test_f`. But `TestClass::test_b` will be run,
88+
because it is individually marked. The marker on the test method
89+
overrides the marker on the class and thus effectively clears the
90+
dependency list for `TestClass::test_b`.
91+
7692
.. _usage-parametrized:
7793

7894
Parametrized tests

tests/test_09_examples_usage.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ def test_testclass(ctestdir):
6060
""")
6161

6262

63+
def test_mark_class(ctestdir):
64+
"""Applying the dependency marker to a class as a whole
65+
"""
66+
with get_example("mark-class.py").open("rt") as f:
67+
ctestdir.makepyfile(f.read())
68+
result = ctestdir.runpytest("--verbose")
69+
result.assert_outcomes(passed=1, skipped=2, failed=0, xfailed=1)
70+
result.stdout.re_match_lines(r"""
71+
.*::test_f XFAIL(?:\s+\(.*\))?
72+
.*::TestClass::test_a SKIPPED(?:\s+\(.*\))?
73+
.*::TestClass::test_b PASSED
74+
.*::TestClass::test_c SKIPPED(?:\s+\(.*\))?
75+
""")
76+
77+
6378
def test_parametrized(ctestdir):
6479
"""Parametrized tests
6580
"""

0 commit comments

Comments
 (0)