Skip to content

Commit 7c3e4d1

Browse files
committed
Document usage with test classes.
1 parent c9c4e60 commit 7c3e4d1

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

doc/examples/testclass.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pytest
2+
3+
4+
class TestClass(object):
5+
6+
@pytest.mark.dependency()
7+
@pytest.mark.xfail(reason="deliberate fail")
8+
def test_a(self):
9+
assert False
10+
11+
@pytest.mark.dependency()
12+
def test_b(self):
13+
pass
14+
15+
@pytest.mark.dependency(depends=["test_a"])
16+
def test_c(self):
17+
pass
18+
19+
@pytest.mark.dependency(depends=["test_b"])
20+
def test_d(self):
21+
pass
22+
23+
@pytest.mark.dependency(depends=["test_b", "test_c"])
24+
def test_e(self):
25+
pass
26+
27+
28+
class TestClassNamed(object):
29+
30+
@pytest.mark.dependency(name="a")
31+
@pytest.mark.xfail(reason="deliberate fail")
32+
def test_a(self):
33+
assert False
34+
35+
@pytest.mark.dependency(name="b", depends=["a"])
36+
def test_b(self):
37+
pass
38+
39+
@pytest.mark.dependency(name="c")
40+
def test_c(self):
41+
pass
42+
43+
@pytest.mark.dependency(name="d", depends=["c"])
44+
def test_d(self):
45+
pass
46+
47+
@pytest.mark.dependency(name="e", depends=["b", "c"])
48+
def test_e(self):
49+
pass

doc/src/usage.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ set:
4949

5050
.. _usage-parametrized:
5151

52+
Using test classes
53+
------------------
54+
55+
Tests may be grouped in classes in pytest. Marking the dependencies
56+
of methods in test classes works the same way as for simple test
57+
functions. In the following example we define two test classes. Each
58+
works in the same manner as the previous examples respectively:
59+
60+
.. literalinclude:: ../examples/testclass.py
61+
62+
In `TestClass` the default names for the tests are used, which is the
63+
name of the respective method in this case, while in `TestClassNamed`
64+
these names are overridden an explicit name argument to the
65+
:func:`pytest.mark.dependency` marker.
66+
5267
Parametrized tests
5368
------------------
5469

0 commit comments

Comments
 (0)