Skip to content

Commit 1d3cb52

Browse files
committed
Added support for xfailed and xpassed outcomes to the pytester.RunResult.assert_outcomes signature.
1 parent 28aff05 commit 1d3cb52

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Vitaly Lashmanov
213213
Vlad Dragos
214214
Wil Cooley
215215
William Lee
216+
Wim Glenn
216217
Wouter van Ackooy
217218
Xuan Luong
218219
Xuecong Liao

changelog/3837.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for 'xfailed' and 'xpassed' outcomes to the ``pytester.RunResult.assert_outcomes`` signature.

src/_pytest/pytester.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def parseoutcomes(self):
406406
return d
407407
raise ValueError("Pytest terminal report not found")
408408

409-
def assert_outcomes(self, passed=0, skipped=0, failed=0, error=0):
409+
def assert_outcomes(self, passed=0, skipped=0, failed=0, error=0, xpassed=0, xfailed=0):
410410
"""Assert that the specified outcomes appear with the respective
411411
numbers (0 means it didn't occur) in the text output from a test run.
412412
@@ -417,10 +417,18 @@ def assert_outcomes(self, passed=0, skipped=0, failed=0, error=0):
417417
"skipped": d.get("skipped", 0),
418418
"failed": d.get("failed", 0),
419419
"error": d.get("error", 0),
420+
"xpassed": d.get("xpassed", 0),
421+
"xfailed": d.get("xfailed", 0),
420422
}
421-
assert obtained == dict(
422-
passed=passed, skipped=skipped, failed=failed, error=error
423-
)
423+
expected = {
424+
"passed": passed,
425+
"skipped": skipped,
426+
"failed": failed,
427+
"error": error,
428+
"xpassed": xpassed,
429+
"xfailed": xfailed,
430+
}
431+
assert obtained == expected
424432

425433

426434
class CwdSnapshot(object):

testing/test_pytester.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,57 @@ def test_hello(testdir):
8383
result.assert_outcomes(passed=1)
8484

8585

86+
def test_runresult_assertion_on_xfail(testdir):
87+
testdir.makepyfile(
88+
"""
89+
import pytest
90+
91+
pytest_plugins = "pytester"
92+
93+
@pytest.mark.xfail
94+
def test_potato():
95+
assert False
96+
"""
97+
)
98+
result = testdir.runpytest()
99+
result.assert_outcomes(xfailed=1)
100+
assert result.ret == 0
101+
102+
103+
def test_runresult_assertion_on_xpassed(testdir):
104+
testdir.makepyfile(
105+
"""
106+
import pytest
107+
108+
pytest_plugins = "pytester"
109+
110+
@pytest.mark.xfail
111+
def test_potato():
112+
assert True
113+
"""
114+
)
115+
result = testdir.runpytest()
116+
result.assert_outcomes(xpassed=1)
117+
assert result.ret == 0
118+
119+
120+
def test_xpassed_with_strict_is_considered_a_failure(testdir):
121+
testdir.makepyfile(
122+
"""
123+
import pytest
124+
125+
pytest_plugins = "pytester"
126+
127+
@pytest.mark.xfail(strict=True)
128+
def test_potato():
129+
assert True
130+
"""
131+
)
132+
result = testdir.runpytest()
133+
result.assert_outcomes(failed=1)
134+
assert result.ret != 0
135+
136+
86137
def make_holder():
87138
class apiclass(object):
88139
def pytest_xyz(self, arg):

0 commit comments

Comments
 (0)