From 13bf5fd3d94539f907fc0c72dfff75c3b1c87f74 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Sat, 23 Apr 2022 22:53:48 +0100 Subject: Fix various test failures with Python 3.11 The changes for https://peps.python.org/pep-0657/ require a number of changes in our tests. Some tests still fail due to https://twistedmatrix.com/trac/ticket/10336, so I'm not adding 3.11 to the test matrix yet. Fixes #325. --- testtools/tests/test_run.py | 4 ++-- testtools/tests/test_testresult.py | 23 +++++++++++++---------- testtools/tests/test_testsuite.py | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/testtools/tests/test_run.py b/testtools/tests/test_run.py index ee96eec..9808421 100644 --- a/testtools/tests/test_run.py +++ b/testtools/tests/test_run.py @@ -202,9 +202,9 @@ unittest.loader._FailedTest.runexample Failed to import test module: runexample Traceback (most recent call last): File ".../loader.py", line ..., in _find_test_path - package = self._get_module_from_name(name) + package = self._get_module_from_name(name)... File ".../loader.py", line ..., in _get_module_from_name - __import__(name) + __import__(name)... File ".../runexample/__init__.py", line 1 class not in ...^... diff --git a/testtools/tests/test_testresult.py b/testtools/tests/test_testresult.py index 72ffb0a..d419f02 100644 --- a/testtools/tests/test_testresult.py +++ b/testtools/tests/test_testresult.py @@ -1265,11 +1265,11 @@ class TestTestResult(TestCase): DocTestMatches( 'Traceback (most recent call last):\n' ' File "...testtools...runtest.py", line ..., in _run_user\n' - ' return fn(*args, **kwargs)\n' + ' return fn(*args, **kwargs)\n...' ' File "...testtools...testcase.py", line ..., in _run_test_method\n' - ' return self._get_test_method()()\n' + ' return self._get_test_method()()\n...' ' File "...testtools...tests...test_testresult.py", line ..., in error\n' - ' 1/0\n' + ' 1/0\n...' 'ZeroDivisionError: ...\n', doctest.ELLIPSIS | doctest.REPORT_UDIFF)) @@ -1282,7 +1282,7 @@ class TestTestResult(TestCase): DocTestMatches( 'Traceback (most recent call last):\n' ' File "...testtools...tests...test_testresult.py", line ..., in error\n' - ' 1/0\n' + ' 1/0\n...' 'ZeroDivisionError: ...\n', doctest.ELLIPSIS)) @@ -1321,17 +1321,17 @@ class TestTestResult(TestCase): DocTestMatches( 'Traceback (most recent call last):\n' ' File "...testtools...runtest.py", line ..., in _run_user\n' - ' return fn(*args, **kwargs)\n' + ' return fn(*args, **kwargs)\n...' ' args = ...\n' ' fn = ...\n' ' kwargs = ...\n' ' self = ...\n' ' File "...testtools...testcase.py", line ..., in _run_test_method\n' - ' return self._get_test_method()()\n' + ' return self._get_test_method()()\n...' ' result = ...\n' ' self = ...\n' ' File "...testtools...tests...test_testresult.py", line ..., in error\n' - ' 1/0\n' + ' 1/0\n...' ' a = 1\n' ' self = ...\n' 'ZeroDivisionError: ...\n', @@ -2644,12 +2644,15 @@ class TestNonAsciiResults(TestCase): " raise RuntimeError\n" " def __repr__(self):\n" " raise RuntimeError\n") + if sys.version_info >= (3, 11): + expected = "UnprintableError: \n" + else: + expected = ( + "UnprintableError: \n") textoutput = self._test_external_case( modulelevel=exception_class, testline="raise UnprintableError") - self.assertIn(self._as_output( - "UnprintableError: \n"), - textoutput) + self.assertIn(self._as_output(expected), textoutput) def test_non_ascii_dirname(self): """Script paths in the traceback can be non-ascii""" diff --git a/testtools/tests/test_testsuite.py b/testtools/tests/test_testsuite.py index 358ac3a..26d2152 100644 --- a/testtools/tests/test_testsuite.py +++ b/testtools/tests/test_testsuite.py @@ -178,7 +178,7 @@ class TestConcurrentStreamTestSuiteRun(TestCase): "Traceback (most recent call last):\n") self.assertThat(events[2][6].decode('utf8'), DocTestMatches("""\ File "...testtools/testsuite.py", line ..., in _run_test - test.run(process_result) + test.run(process_result)... """, doctest.ELLIPSIS)) self.assertThat(events[3][6].decode('utf8'), DocTestMatches("""\ TypeError: ...run() takes ...1 ...argument...2...given... -- cgit v1.2.1 From 56537559a34c4bc1e5f11e68e6378a26c241b474 Mon Sep 17 00:00:00 2001 From: Ben Beecher Date: Sun, 22 Jan 2017 17:35:08 -0500 Subject: Making sure that TestCase can be hashed --- testtools/testcase.py | 7 +++++++ testtools/tests/test_testcase.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/testtools/testcase.py b/testtools/testcase.py index 012def3..2c88545 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -275,6 +275,13 @@ class TestCase(unittest.TestCase): return False return self.__dict__ == other.__dict__ + def __hash__(self): + hashfn = getattr(unittest.TestCase, '__hash__', None) + if hashfn is not None: + return hashfn(self) + return id(self) + + def __repr__(self): # We add id to the repr because it makes testing testtools easier. return f"<{self.id()} id=0x{id(self):0x}>" diff --git a/testtools/tests/test_testcase.py b/testtools/tests/test_testcase.py index 00cb60d..c2766ff 100644 --- a/testtools/tests/test_testcase.py +++ b/testtools/tests/test_testcase.py @@ -86,6 +86,10 @@ class TestPlaceHolder(TestCase): test = PlaceHolder("test id", "description") self.assertEqual("description", test.shortDescription()) + def test_testcase_is_hashable(self): + test = hash(self) + self.assertEqual(unittest.TestCase.__hash__(self), test) + def test_repr_just_id(self): # repr(placeholder) shows you how the object was constructed. test = PlaceHolder("test id") -- cgit v1.2.1 From cf6b8b0ec9e8a16fa64bddbe0ae69f0f235a550a Mon Sep 17 00:00:00 2001 From: Ben Beecher Date: Thu, 29 Aug 2019 15:52:02 -0400 Subject: less code --- testtools/testcase.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/testtools/testcase.py b/testtools/testcase.py index 2c88545..ea5a8d7 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -276,11 +276,8 @@ class TestCase(unittest.TestCase): return self.__dict__ == other.__dict__ def __hash__(self): - hashfn = getattr(unittest.TestCase, '__hash__', None) - if hashfn is not None: - return hashfn(self) - return id(self) - + hashfn = getattr(unittest.TestCase, '__hash__', id) + return hashfn(self) def __repr__(self): # We add id to the repr because it makes testing testtools easier. -- cgit v1.2.1 From e223b5dfada74e8a7438f699c81ab2baeb08df60 Mon Sep 17 00:00:00 2001 From: Ben Beecher Date: Sat, 30 Apr 2022 20:05:24 -0400 Subject: simplifying code --- testtools/testcase.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/testtools/testcase.py b/testtools/testcase.py index ea5a8d7..f3bd510 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -275,9 +275,7 @@ class TestCase(unittest.TestCase): return False return self.__dict__ == other.__dict__ - def __hash__(self): - hashfn = getattr(unittest.TestCase, '__hash__', id) - return hashfn(self) + __hash__ = unittest.TestCase.__hash__ def __repr__(self): # We add id to the repr because it makes testing testtools easier. -- cgit v1.2.1