From dfab640cc1d99369b45e472026a5c00c95383c34 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Nov 2020 19:57:59 +0100 Subject: [PATCH 1/3] tests: fix/harden test_unittest_not_shown_in_traceback --- testing/test_unittest.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 53506b56f88..f4f84629af2 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -692,8 +692,8 @@ def _post_teardown(self): ) -def test_unittest_not_shown_in_traceback(testdir): - testdir.makepyfile( +def test_unittest_not_shown_in_traceback(testdir: "Testdir") -> None: + p1 = testdir.makepyfile( """ import unittest class t(unittest.TestCase): @@ -702,8 +702,26 @@ def test_hello(self): self.assertEqual(x, 4) """ ) - res = testdir.runpytest() - res.stdout.no_fnmatch_line("*failUnlessEqual*") + result = testdir.runpytest(p1) + result.stdout.fnmatch_lines( + [ + "=*= FAILURES =*=", + "_*_ t.test_hello _*_", + "", + "self = ", + "", + " def test_hello(self):", + " x = 3", + "> self.assertEqual(x, 4)", + "E AssertionError: 3 != 4", + "", + "test_unittest_not_shown_in_traceback.py:5: AssertionError: 3 != 4", + "=*= short test summary info =*=", + "FAILED test_unittest_not_shown_in_traceback.py:5::t::test_hello - AssertionError: 3 != 4", + "=*= 1 failed in *s =*=", + ], + consecutive=True, + ) def test_unorderable_types(testdir): From 9b5f8a708d0c7fe464429c6088daa39a15ee9ddb Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Nov 2020 20:55:41 +0100 Subject: [PATCH 2/3] harden test_simple_unittest --- testing/test_unittest.py | 42 +++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index f4f84629af2..d4efc6ab55d 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1,12 +1,15 @@ import gc import pytest +from _pytest.compat import TYPE_CHECKING from _pytest.config import ExitCode -from _pytest.pytester import Testdir +if TYPE_CHECKING: + from _pytest.pytester import Testdir -def test_simple_unittest(testdir): - testpath = testdir.makepyfile( + +def test_simple_unittest(testdir: "Testdir") -> None: + p1 = testdir.makepyfile( """ import unittest class MyTestCase(unittest.TestCase): @@ -16,9 +19,34 @@ def test_failing(self): self.assertEqual('foo', 'bar') """ ) - reprec = testdir.inline_run(testpath) - assert reprec.matchreport("testpassing").passed - assert reprec.matchreport("test_failing").failed + result = testdir.runpytest(p1, "-rap") + result.stdout.fnmatch_lines( + [ + "collected 2 items", + "", + "test_simple_unittest.py F. *", + "", + "=*= FAILURES =*=", + "_*_ MyTestCase.test_failing _*_", + "", + "self = ", + "", + " def test_failing(self):", + "> self.assertEqual('foo', 'bar')", + "E AssertionError: 'foo' != 'bar'", + "E - foo", + "E + bar", + "", + "test_simple_unittest.py:6: AssertionError: 'foo' != 'bar'...", + "=*= short test summary info =*=", + "FAILED test_simple_unittest.py:6::MyTestCase::test_failing" + r" - AssertionError: 'foo' != 'bar'\n- foo\n+ bar", + "PASSED test_simple_unittest.py::MyTestCase::testpassing", + "=*= 1 failed, 1 passed in *s =*=", + ], + consecutive=True, + ) + assert result.ret == 1 def test_runTest_method(testdir): @@ -744,7 +772,7 @@ class Test(unittest.TestCase): assert result.ret == ExitCode.NO_TESTS_COLLECTED -def test_order(testdir: Testdir) -> None: +def test_order(testdir: "Testdir") -> None: """Test for unittest's behavior to sort tests by default (via TestLoader). This could be made configurable maybe.""" From 09146fb79c22d632caa8187d16451df045ae99f2 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 30 Nov 2020 20:56:59 +0100 Subject: [PATCH 3/3] rm test_unittest_not_shown_in_traceback --- testing/test_unittest.py | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index d4efc6ab55d..0dfc8d8f329 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -720,38 +720,6 @@ def _post_teardown(self): ) -def test_unittest_not_shown_in_traceback(testdir: "Testdir") -> None: - p1 = testdir.makepyfile( - """ - import unittest - class t(unittest.TestCase): - def test_hello(self): - x = 3 - self.assertEqual(x, 4) - """ - ) - result = testdir.runpytest(p1) - result.stdout.fnmatch_lines( - [ - "=*= FAILURES =*=", - "_*_ t.test_hello _*_", - "", - "self = ", - "", - " def test_hello(self):", - " x = 3", - "> self.assertEqual(x, 4)", - "E AssertionError: 3 != 4", - "", - "test_unittest_not_shown_in_traceback.py:5: AssertionError: 3 != 4", - "=*= short test summary info =*=", - "FAILED test_unittest_not_shown_in_traceback.py:5::t::test_hello - AssertionError: 3 != 4", - "=*= 1 failed in *s =*=", - ], - consecutive=True, - ) - - def test_unorderable_types(testdir): testdir.makepyfile( """