Skip to content

Commit 7e1692c

Browse files
committed
Add a page on advanced usage.
1 parent 340a3ac commit 7e1692c

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

doc/examples/dyn-parametrized.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
3+
# Test data
4+
# Consider a bunch of Nodes, some of them are parents and some are children.
5+
6+
class Node(object):
7+
NodeMap = {}
8+
def __init__(self, name, parent=None):
9+
self.name = name
10+
self.children = []
11+
self.NodeMap[self.name] = self
12+
if parent:
13+
self.parent = self.NodeMap[parent]
14+
self.parent.children.append(self)
15+
else:
16+
self.parent = None
17+
def __str__(self):
18+
return self.name
19+
20+
parents = [ Node("a"), Node("b"), Node("c"), Node("d"), ]
21+
childs = [ Node("e", "a"), Node("f", "a"), Node("g", "a"),
22+
Node("h", "b"), Node("i", "c"), Node("j", "c"),
23+
Node("k", "d"), Node("l", "d"), Node("m", "d"), ]
24+
25+
# The test for the parent shall depend on the test of all its children.
26+
# Create enriched parameter lists, decorated with the dependency marker.
27+
28+
childparam = [
29+
pytest.mark.dependency(name="test_child[%s]" % c)(c) for c in childs
30+
]
31+
parentparam = [
32+
pytest.mark.dependency(
33+
name="test_parent[%s]" % p,
34+
depends=["test_child[%s]" % c for c in p.children]
35+
)(p) for p in parents
36+
]
37+
38+
@pytest.mark.parametrize("c", childparam)
39+
def test_child(c):
40+
if c.name == "l":
41+
pytest.xfail("deliberate fail")
42+
assert False
43+
44+
@pytest.mark.parametrize("p", parentparam)
45+
def test_parent(p):
46+
pass
47+

doc/src/advanced.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Advanced usage
2+
==============
3+
4+
This section contains some advanced examples for using
5+
pytest-dependency.
6+
7+
Dynamic compilation of marked parameters
8+
----------------------------------------
9+
10+
Sometimes, the parameter values for parametrized tests cannot easily
11+
be typed as a simple list. It may need to be compiled at run time
12+
depending on a set of test data. This also works together with
13+
marking dependencies in the individual test instances.
14+
15+
Consider the following example test module:
16+
17+
.. literalinclude:: ../examples/dyn-parametrized.py
18+
19+
In principle, this example works the very same way as the basic
20+
example for parametrized tests, see :ref:`usage-parametrized`. The
21+
only difference is that the lists of paramters are dynamically
22+
compiled beforehand. The test for child `l` deliberately fails, just
23+
to show the effect. As a consequence, the test for its parent `d`
24+
will be skipped.

doc/src/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Content of the documentation
1414
about
1515
install
1616
usage
17+
advanced
1718
reference

doc/src/usage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ set:
4747

4848
.. literalinclude:: ../examples/named.py
4949

50+
.. _usage-parametrized:
51+
5052
Parametrized tests
5153
------------------
5254

0 commit comments

Comments
 (0)