Skip to content

Commit

Permalink
Document that the dependency marker may be applied to a class as a
Browse files Browse the repository at this point in the history
whole.  Close #39.
  • Loading branch information
RKrahl committed Dec 25, 2021
1 parent a8cb125 commit 65dc1f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions doc/examples/mark-class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest


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


@pytest.mark.dependency(depends=["test_f"])
class TestClass(object):

def test_a(self):
pass

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

def test_c(self):
pass
16 changes: 16 additions & 0 deletions doc/src/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ explicit `name` argument to the :func:`pytest.mark.dependency` marker.
The name of the class is prepended to the method name to form the
default name for the test.

Applying the dependency marker to a class as a whole
----------------------------------------------------

The :func:`pytest.mark.dependency` marker may also be applied to a test
class as a whole. This has the same effect as applying that marker
with the same arguments to each method of the class individually.
Consider:

.. literalinclude:: ../examples/mark-class.py

The tests `TestClass::test_a` and `TestClass::test_c` will be skipped,
because they depend on `test_f`. But `TestClass::test_b` will be run,
because it is individually marked. The marker on the test method
overrides the marker on the class and thus effectively clears the
dependency list for `TestClass::test_b`.

.. _usage-parametrized:

Parametrized tests
Expand Down
15 changes: 15 additions & 0 deletions tests/test_09_examples_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ def test_testclass(ctestdir):
""")


def test_mark_class(ctestdir):
"""Applying the dependency marker to a class as a whole
"""
with get_example("mark-class.py").open("rt") as f:
ctestdir.makepyfile(f.read())
result = ctestdir.runpytest("--verbose")
result.assert_outcomes(passed=1, skipped=2, failed=0, xfailed=1)
result.stdout.re_match_lines(r"""
.*::test_f XFAIL(?:\s+\(.*\))?
.*::TestClass::test_a SKIPPED(?:\s+\(.*\))?
.*::TestClass::test_b PASSED
.*::TestClass::test_c SKIPPED(?:\s+\(.*\))?
""")


def test_parametrized(ctestdir):
"""Parametrized tests
"""
Expand Down

0 comments on commit 65dc1f5

Please sign in to comment.