Skip to content

Commit 886c254

Browse files
committed
Add an example on how to implement an or-like dependency. Ref. #53, #57
1 parent 2807908 commit 886c254

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

doc/examples/or_dependency.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
from pytest_dependency import depends
3+
4+
def depends_or(request, other, scope='module'):
5+
"""Add dependency on any of the other tests.
6+
7+
Call pytest.skip() unless a successful outcome of any of the tests
8+
in other has been registered previously. This helper is similar
9+
to `pytest_dependency.depends()`. It takes the same arguments.
10+
But while `pytest_dependency.depends()` combines the tests in
11+
`other` in an and-like manner, it skips the current test unless
12+
all other tests did succeed, this function combines them in an
13+
or-like manner, it runs the current test if at least one of the
14+
other tests did succeed.
15+
"""
16+
item = request.node
17+
for o in other:
18+
try:
19+
depends(request, [o], scope)
20+
except pytest.skip.Exception:
21+
continue
22+
else:
23+
return
24+
pytest.skip("%s depends on any of %s" % (item.name, ", ".join(other)))
25+
26+
27+
@pytest.mark.dependency()
28+
def test_ap():
29+
pass
30+
31+
@pytest.mark.dependency()
32+
@pytest.mark.xfail(reason="deliberate fail")
33+
def test_ax():
34+
assert False
35+
36+
@pytest.mark.dependency()
37+
def test_bp():
38+
pass
39+
40+
@pytest.mark.dependency()
41+
@pytest.mark.xfail(reason="deliberate fail")
42+
def test_bx():
43+
assert False
44+
45+
@pytest.mark.dependency()
46+
def test_c(request):
47+
depends_or(request, ["test_ax", "test_bx"])
48+
pass
49+
50+
@pytest.mark.dependency()
51+
def test_d(request):
52+
depends_or(request, ["test_ax", "test_bp"])
53+
pass
54+
55+
@pytest.mark.dependency()
56+
def test_e(request):
57+
depends_or(request, ["test_ap", "test_bx"])
58+
pass
59+
60+
@pytest.mark.dependency()
61+
def test_f(request):
62+
depends_or(request, ["test_ap", "test_bp"])
63+
pass

0 commit comments

Comments
 (0)