Skip to content

Commit ccb0848

Browse files
committed
Merge branch 'depend-function'.
2 parents 06e39e9 + 272a09d commit ccb0848

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
History of changes to pytest-dependency
22
=======================================
33

4+
* Version 0.2.0 (not yet released)
5+
6+
** New features
7+
8+
+ Issue #4: Add a depend() function to add a dependency to a test at
9+
runtime.
10+
411
* Version 0.1.0 (2017-01-29)
512

613
+ Initial release as an independent Python module.

pytest_dependency.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@ def checkDepend(self, depends):
6262
pytest.skip("depends on %s" % i)
6363

6464

65+
def depends(request, other):
66+
"""Add dependency on other test.
67+
68+
Call pytest.skip() unless a successful outcome of all of the tests
69+
in other has been registered previously. This has the same effect
70+
as the `depends` keyword argument to the `dependency` marker. In
71+
contrast to the marker, this function may be called at runtime
72+
during a test.
73+
74+
:param request: the value of the `request` pytest fixture related
75+
to the current test.
76+
:param other: dependencies, a list of names of tests that this
77+
test depends on.
78+
:type other: iterable of :class:`str`
79+
"""
80+
item = request.node
81+
manager = DependencyManager.getManager(item)
82+
manager.checkDepend(other)
83+
84+
6585
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
6686
def pytest_runtest_makereport(item, call):
6787
"""Store the test outcome if this item is marked "dependency".

tests/test_03_runtime.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Using depends() to mark dependencies at runtime.
2+
"""
3+
4+
import pytest
5+
6+
7+
def test_skip_depend_runtime(ctestdir):
8+
"""One test is skipped, other dependent tests are skipped as well.
9+
This also includes indirect dependencies.
10+
"""
11+
ctestdir.makepyfile("""
12+
import pytest
13+
from pytest_dependency import depends
14+
15+
@pytest.mark.dependency()
16+
def test_a():
17+
pass
18+
19+
@pytest.mark.dependency()
20+
def test_b():
21+
pytest.skip("explicit skip")
22+
23+
@pytest.mark.dependency()
24+
def test_c(request):
25+
depends(request, ["test_b"])
26+
pass
27+
28+
@pytest.mark.dependency()
29+
def test_d(request):
30+
depends(request, ["test_a", "test_c"])
31+
pass
32+
""")
33+
result = ctestdir.runpytest("--verbose")
34+
result.assert_outcomes(passed=1, skipped=3, failed=0)
35+
result.stdout.fnmatch_lines("""
36+
*::test_a PASSED
37+
*::test_b SKIPPED
38+
*::test_c SKIPPED
39+
*::test_d SKIPPED
40+
""")

0 commit comments

Comments
 (0)