Skip to content

Commit 35fe28a

Browse files
committed
Add examples on grouping tests using fixtures to advanced usage.
1 parent d721ca6 commit 35fe28a

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

doc/examples/group-fixture.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
from pytest_dependency import depends
3+
4+
@pytest.fixture(scope="module", params=range(1,10))
5+
def testcase(request):
6+
param = request.param
7+
return param
8+
9+
@pytest.mark.dependency()
10+
def test_a(testcase):
11+
if testcase % 7 == 0:
12+
pytest.xfail("deliberate fail")
13+
assert False
14+
15+
@pytest.mark.dependency()
16+
def test_b(request, testcase):
17+
depends(request, ["test_a[%d]" % testcase])
18+
pass

doc/examples/group-fixture2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
from pytest_dependency import depends
3+
4+
@pytest.fixture(scope="module", params=range(1,10))
5+
def testcase(request):
6+
param = request.param
7+
return param
8+
9+
@pytest.fixture(scope="module")
10+
def dep_testcase(request, testcase):
11+
depends(request, ["test_a[%d]" % testcase])
12+
return testcase
13+
14+
@pytest.mark.dependency()
15+
def test_a(testcase):
16+
if testcase % 7 == 0:
17+
pytest.xfail("deliberate fail")
18+
assert False
19+
20+
@pytest.mark.dependency()
21+
def test_b(dep_testcase):
22+
pass
23+
24+
@pytest.mark.dependency()
25+
def test_c(dep_testcase):
26+
pass

doc/src/advanced.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,33 @@ only difference is that the lists of paramters are dynamically
2222
compiled beforehand. The test for child `l` deliberately fails, just
2323
to show the effect. As a consequence, the test for its parent `d`
2424
will be skipped.
25+
26+
Grouping tests using fixtures
27+
-----------------------------
28+
29+
pytest features the `automatic grouping of tests by fixture
30+
instances`__. This is particularly useful if there is a set of test
31+
cases and a series of tests shall be run for each of the test case
32+
respectively.
33+
34+
Consider the following example:
35+
36+
.. literalinclude:: ../examples/group-fixture.py
37+
38+
The test instances of `test_b` depend on `test_a` for the same
39+
parameter value. The test `test_a[7]` deliberately fails, as a
40+
consequence `test_b[7]` will be skipped. Note that we need to call
41+
:func:`pytest_dependency.depends` to mark the dependencies, because
42+
there is no way to use the :func:`pytest.mark.dependency` marker on
43+
the parameter values here.
44+
45+
If many tests in the series depend on a single test, it might be an
46+
option, to move the call to :func:`pytest_dependency.depends` in a
47+
fixture on its own. Consider:
48+
49+
.. literalinclude:: ../examples/group-fixture2.py
50+
51+
In this example, both `test_b[7]` and `test_c[7]` are skipped, because
52+
`test_a[7]` deliberately fails.
53+
54+
.. __: https://docs.pytest.org/en/latest/fixture.html#automatic-grouping-of-tests-by-fixture-instances

0 commit comments

Comments
 (0)