Skip to content

Commit 806ea8a

Browse files
committed
Add examples how to depend on all instances of a parametrized test.
Related to Issue #9.
1 parent 35fe28a commit 806ea8a

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

doc/examples/all_params.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
3+
def instances(name, params):
4+
def vstr(val):
5+
if isinstance(val, (list, tuple)):
6+
return "-".join([str(v) for v in val])
7+
else:
8+
return str(val)
9+
return ["%s[%s]" % (name, vstr(v)) for v in params]
10+
11+
12+
params_a = range(17)
13+
14+
@pytest.mark.parametrize("x", params_a)
15+
@pytest.mark.dependency()
16+
def test_a(x):
17+
if x == 13:
18+
pytest.xfail("deliberate fail")
19+
assert False
20+
else:
21+
pass
22+
23+
@pytest.mark.dependency(depends=instances("test_a", params_a))
24+
def test_b():
25+
pass
26+
27+
params_c = zip(range(0,8,2), range(2,6))
28+
29+
@pytest.mark.parametrize("x,y", params_c)
30+
@pytest.mark.dependency()
31+
def test_c(x, y):
32+
if x > y:
33+
pytest.xfail("deliberate fail")
34+
assert False
35+
else:
36+
pass
37+
38+
@pytest.mark.dependency(depends=instances("test_c", params_c))
39+
def test_d():
40+
pass
41+
42+
params_e = ['abc', 'def']
43+
44+
@pytest.mark.parametrize("s", params_e)
45+
@pytest.mark.dependency()
46+
def test_e(s):
47+
if 'e' in s:
48+
pytest.xfail("deliberate fail")
49+
assert False
50+
else:
51+
pass
52+
53+
@pytest.mark.dependency(depends=instances("test_e", params_e))
54+
def test_f():
55+
pass

doc/src/advanced.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,30 @@ In this example, both `test_b[7]` and `test_c[7]` are skipped, because
5252
`test_a[7]` deliberately fails.
5353

5454
.. __: https://docs.pytest.org/en/latest/fixture.html#automatic-grouping-of-tests-by-fixture-instances
55+
56+
Depend on all instances of a parametrized test at once
57+
------------------------------------------------------
58+
59+
If a test depends on a all instances of a parametrized test at once,
60+
listing all of them in the :func:`pytest.mark.dependency` marker
61+
explicitly might not be the best solution. But you can dynamically
62+
compile these lists from the parameter values, as in the following
63+
example:
64+
65+
.. literalinclude:: ../examples/all_params.py
66+
67+
Here, `test_b`, `test_d`, and `test_f` will be skipped because they
68+
depend on all instances of `test_a`, `test_c`, and `test_e`
69+
respectively, but `test_a[13]`, `test_c[6-5]`, and `test_e[def]` fail.
70+
The list of the test instances is compiled in the helper function
71+
`instances()`.
72+
73+
Unfortunately you need knowledge how pytest encodes parameter values
74+
in test instance names to write this helper function. Note in
75+
particular how lists of parameter values are compiled into one single
76+
string in the case of multi parameter tests. But also note that this
77+
example of the `instances()` helper will only work for simple cases.
78+
It requires the parameter values to be scalars that can easily be
79+
converted to strings. And it will fail if the same list of parameters
80+
is passed to the same test more then once, because then, pytest will
81+
add an index to the name to disambiguate the parameter values.

0 commit comments

Comments
 (0)