From 6ddf7c3d42efcf01c8641893e7331f9e33e6877d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 13 Nov 2019 02:13:35 +0100 Subject: [PATCH] pytester: Hookrecorder: improve assertoutcome 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: ([], [], []) E assert {'failed': 1, 'passed': 0, 'skipped': 0} == {'failed': 0, 'passed': 0, 'skipped': 1} --- changelog/6176.improvement.rst | 1 + src/_pytest/pytester.py | 15 +++++++++++---- testing/test_assertion.py | 9 ++++++++- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 changelog/6176.improvement.rst diff --git a/changelog/6176.improvement.rst b/changelog/6176.improvement.rst new file mode 100644 index 000000000..39787da2e --- /dev/null +++ b/changelog/6176.improvement.rst @@ -0,0 +1 @@ +Improved failure reporting with pytester's ``Hookrecorder.assertoutcome``. diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 9f3b4d8ab..ca780a9f5 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -332,10 +332,17 @@ class HookRecorder: 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[:] = [] diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 6c700567a..e4d68ff8c 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -70,7 +70,14 @@ class TestImportHookInstallation: """ ) result = testdir.runpytest_subprocess() - result.stdout.fnmatch_lines(["*assert 1 == 0*"]) + result.stdout.fnmatch_lines( + [ + "E * AssertionError: ([[][]], [[][]], [[][]])*", + "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):