Skip to content

Commit 44914a7

Browse files
committed
Merge branch 'master' into doc
2 parents ab9eb75 + 98860b6 commit 44914a7

File tree

5 files changed

+139
-4
lines changed

5 files changed

+139
-4
lines changed

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
* Version 0.3 (not yet released)
55

6+
** New features
7+
8+
+ Issue #7: Add a configuration switch to implicitly mark all tests.
9+
610
** Incompatible changes
711

812
+ Prepend the class name to the default test name for test class
@@ -44,6 +48,8 @@
4448
+ Issue #13: Do not import pytest in setup.py to make it compatible
4549
with pipenv.
4650

51+
+ Issue #15: tests fail with pytest 3.3.0.
52+
4753
+ Clarify in the documentation that Python 3.1 is not officially
4854
supported because pytest 2.8 does not support it. There is no
4955
known issue with Python 3.1 though.

doc/src/configuration.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ Configuration file options can be set in the `ini file`.
1414
This is a builtin configuration option of pytest itself. Since
1515
pytest-dependency requires pytest 2.8.0 or newer, it is
1616
recommended to set this option accordingly.
17+
18+
automark_dependency
19+
This is a flag. If set to `False`, the default, the outcome of
20+
a test will only be registered if the test has been decorated
21+
with the :func:`pytest.mark.dependency` marker. As a results,
22+
all tests, the dependencies and the dependent tests must be
23+
decorated. If set to `True`, the outcome of all tests will be
24+
registered. It has the same effect as if all tests are
25+
implicitly decorated with :func:`pytest.mark.dependency`.

pytest_dependency.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55

66
import pytest
77

8+
_automark = False
9+
10+
11+
def _get_bool(value):
12+
"""Evaluate string representation of a boolean value.
13+
"""
14+
if value:
15+
if value.lower() in ["0", "no", "n", "false", "f", "off"]:
16+
return False
17+
elif value.lower() in ["1", "yes", "y", "true", "t", "on"]:
18+
return True
19+
else:
20+
raise ValueError("Invalid truth value '%s'" % value)
21+
else:
22+
return False
23+
824

925
class DependencyItemStatus(object):
1026
"""Status of a test item in a dependency manager.
@@ -45,8 +61,7 @@ def getManager(cls, item, scope='module'):
4561
def __init__(self):
4662
self.results = {}
4763

48-
def addResult(self, item, marker, rep):
49-
name = marker.kwargs.get('name')
64+
def addResult(self, item, name, rep):
5065
if not name:
5166
if item.cls:
5267
name = "%s::%s" % (item.cls.__name__, item.name)
@@ -83,16 +98,28 @@ def depends(request, other):
8398
manager.checkDepend(other, item)
8499

85100

101+
def pytest_addoption(parser):
102+
parser.addini("automark_dependency",
103+
"Add the dependency marker to all tests automatically",
104+
default=False)
105+
106+
107+
def pytest_configure(config):
108+
global _automark
109+
_automark = _get_bool(config.getini("automark_dependency"))
110+
111+
86112
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
87113
def pytest_runtest_makereport(item, call):
88114
"""Store the test outcome if this item is marked "dependency".
89115
"""
90116
outcome = yield
91117
marker = item.get_marker("dependency")
92-
if marker is not None:
118+
if marker is not None or _automark:
93119
rep = outcome.get_result()
120+
name = marker.kwargs.get('name') if marker is not None else None
94121
manager = DependencyManager.getManager(item)
95-
manager.addResult(item, marker, rep)
122+
manager.addResult(item, name, rep)
96123

97124

98125
def pytest_runtest_setup(item):

tests/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
@pytest.fixture
77
def ctestdir(testdir):
8+
testdir.makefile('.ini', pytest="""
9+
[pytest]
10+
console_output_style = classic
11+
""")
812
testdir.makeconftest("""
913
import sys
1014
if "pytest_dependency" not in sys.modules:

tests/test_04_automark.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""Test the automark_dependency option.
2+
"""
3+
4+
import pytest
5+
6+
7+
def test_not_set(ctestdir):
8+
"""No pytest.ini file, e.g. automark_dependency is not set.
9+
10+
Since automark_dependency defaults to false and test_a is not
11+
marked, the outcome of test_a will not be recorded. As a result,
12+
test_b will be skipped due to a missing dependency.
13+
"""
14+
ctestdir.makepyfile("""
15+
import pytest
16+
17+
def test_a():
18+
pass
19+
20+
@pytest.mark.dependency(depends=["test_a"])
21+
def test_b():
22+
pass
23+
""")
24+
result = ctestdir.runpytest("--verbose", "-rs")
25+
result.assert_outcomes(passed=1, skipped=1, failed=0)
26+
result.stdout.fnmatch_lines("""
27+
*::test_a PASSED
28+
*::test_b SKIPPED
29+
""")
30+
31+
32+
def test_set_false(ctestdir):
33+
"""A pytest.ini is present, automark_dependency is set to false.
34+
35+
Since automark_dependency is set to false and test_a is not
36+
marked, the outcome of test_a will not be recorded. As a result,
37+
test_b will be skipped due to a missing dependency.
38+
"""
39+
ctestdir.makefile('.ini', pytest="""
40+
[pytest]
41+
automark_dependency = false
42+
console_output_style = classic
43+
""")
44+
ctestdir.makepyfile("""
45+
import pytest
46+
47+
def test_a():
48+
pass
49+
50+
@pytest.mark.dependency(depends=["test_a"])
51+
def test_b():
52+
pass
53+
""")
54+
result = ctestdir.runpytest("--verbose", "-rs")
55+
result.assert_outcomes(passed=1, skipped=1, failed=0)
56+
result.stdout.fnmatch_lines("""
57+
*::test_a PASSED
58+
*::test_b SKIPPED
59+
""")
60+
61+
62+
def test_set_true(ctestdir):
63+
"""A pytest.ini is present, automark_dependency is set to false.
64+
65+
Since automark_dependency is set to true, the outcome of test_a
66+
will be recorded, even though it is not marked. As a result,
67+
test_b will be skipped due to a missing dependency.
68+
"""
69+
ctestdir.makefile('.ini', pytest="""
70+
[pytest]
71+
automark_dependency = true
72+
console_output_style = classic
73+
""")
74+
ctestdir.makepyfile("""
75+
import pytest
76+
77+
def test_a():
78+
pass
79+
80+
@pytest.mark.dependency(depends=["test_a"])
81+
def test_b():
82+
pass
83+
""")
84+
result = ctestdir.runpytest("--verbose", "-rs")
85+
result.assert_outcomes(passed=2, skipped=0, failed=0)
86+
result.stdout.fnmatch_lines("""
87+
*::test_a PASSED
88+
*::test_b PASSED
89+
""")

0 commit comments

Comments
 (0)