Skip to content

Commit

Permalink
pytester: Hookrecorder: improve assertoutcome
Browse files Browse the repository at this point in the history
Before:

        def assertoutcome(self, passed: int = 0, skipped: int = 0, failed: int = 0) -> None:
            realpassed, realskipped, realfailed = self.listoutcomes()
            assert passed == len(realpassed)
    >       assert skipped == len(realskipped)
    E       assert 1 == 0
    E        +  where 0 = len([])

After:

    >       reprec = testdir.inline_run(testpath, "-s")
    E       AssertionError: ([], [], [<TestReport 'nodeid' when='call' outcome='failed'>])
    E       assert {'failed': 1, 'passed': 0, 'skipped': 0} == {'failed': 0, 'passed': 0, 'skipped': 1}
  • Loading branch information
blueyed committed Nov 13, 2019
1 parent e2022a6 commit 6ddf7c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/6176.improvement.rst
@@ -0,0 +1 @@
Improved failure reporting with pytester's ``Hookrecorder.assertoutcome``.
15 changes: 11 additions & 4 deletions src/_pytest/pytester.py
Expand Up @@ -332,10 +332,17 @@ def countoutcomes(self) -> List[int]:
return [len(x) for x in self.listoutcomes()]

def assertoutcome(self, passed: int = 0, skipped: int = 0, failed: int = 0) -> None:
realpassed, realskipped, realfailed = self.listoutcomes()
assert passed == len(realpassed)
assert skipped == len(realskipped)
assert failed == len(realfailed)
__tracebackhide__ = True

outcomes = self.listoutcomes()
realpassed, realskipped, realfailed = outcomes
obtained = {
"passed": len(realpassed),
"skipped": len(realskipped),
"failed": len(realfailed),
}
expected = {"passed": passed, "skipped": skipped, "failed": failed}
assert obtained == expected, outcomes

def clear(self) -> None:
self.calls[:] = []
Expand Down
9 changes: 8 additions & 1 deletion testing/test_assertion.py
Expand Up @@ -70,7 +70,14 @@ def test_dummy_failure(testdir): # how meta!
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["*assert 1 == 0*"])
result.stdout.fnmatch_lines(
[
"E * AssertionError: ([[][]], [[][]], [[]<TestReport *>[]])*",
"E * assert"
" {'failed': 1, 'passed': 0, 'skipped': 0} =="
" {'failed': 0, 'passed': 1, 'skipped': 0}",
]
)

@pytest.mark.parametrize("mode", ["plain", "rewrite"])
def test_pytest_plugins_rewrite(self, testdir, mode):
Expand Down

0 comments on commit 6ddf7c3

Please sign in to comment.