diff --git a/testtools/testcase.py b/testtools/testcase.py index 004fdb5c..a9b017bb 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -21,6 +21,7 @@ import sys import types import unittest +from unittest.case import SkipTest import warnings from testtools.compat import reraise @@ -49,9 +50,15 @@ ) -class TestSkipped(Exception): +class TestSkipped(SkipTest): """Raised within TestCase.run() when a test is skipped.""" -TestSkipped = try_import('unittest.case.SkipTest', TestSkipped) + def __init__(self, *args, **kwargs): + warnings.warn( + 'Use SkipTest from unittest instead.', + DeprecationWarning, + stacklevel=2, + ) + super().__init__(*args, **kwargs) class _UnexpectedSuccess(Exception): @@ -60,8 +67,6 @@ class _UnexpectedSuccess(Exception): Note that this exception is private plumbing in testtools' testcase module. """ -_UnexpectedSuccess = try_import( - 'unittest.case._UnexpectedSuccess', _UnexpectedSuccess) class _ExpectedFailure(Exception): @@ -70,8 +75,6 @@ class _ExpectedFailure(Exception): Note that this exception is private plumbing in testtools' testcase module. """ -_ExpectedFailure = try_import( - 'unittest.case._ExpectedFailure', _ExpectedFailure) # Copied from unittest before python 3.4 release. Used to maintain @@ -222,7 +225,7 @@ class TestCase(unittest.TestCase): and an optional list of exception handlers. """ - skipException = TestSkipped + skipException = SkipTest run_tests_with = RunTest @@ -276,12 +279,24 @@ def __eq__(self, other): return False return self.__dict__ == getattr(other, '__dict__', None) + # We need to explicitly set this since we're overriding __eq__ + # https://docs.python.org/3/reference/datamodel.html#object.__hash__ __hash__ = unittest.TestCase.__hash__ def __repr__(self): # We add id to the repr because it makes testing testtools easier. return f"<{self.id()} id=0x{id(self):0x}>" + def _deprecate(original_func): + def deprecated_func(*args, **kwargs): + warnings.warn( + 'Please use {0} instead.'.format(original_func.__name__), + DeprecationWarning, + stacklevel=2, + ) + return original_func(*args, **kwargs) + return deprecated_func + def addDetail(self, name, content_object): """Add a detail to be reported with this test's outcome. @@ -394,7 +409,7 @@ def assertEqual(self, expected, observed, message=''): matcher = _FlippedEquals(expected) self.assertThat(observed, matcher, message) - failUnlessEqual = assertEquals = assertEqual + failUnlessEqual = assertEquals = _deprecate(assertEqual) def assertIn(self, needle, haystack, message=''): """Assert that needle is in haystack.""" @@ -468,7 +483,8 @@ def match(self, matchee): our_callable = Nullary(callableObj, *args, **kwargs) self.assertThat(our_callable, matcher) return capture.matchee - failUnlessRaises = assertRaises + + failUnlessRaises = _deprecate(assertRaises) def assertThat(self, matchee, matcher, message='', verbose=False): """Assert that matchee is matched by matcher. @@ -481,7 +497,8 @@ def assertThat(self, matchee, matcher, message='', verbose=False): if mismatch_error is not None: raise mismatch_error - assertItemsEqual = unittest.TestCase.assertCountEqual + assertItemsEqual = _deprecate(unittest.TestCase.assertCountEqual) + def addDetailUniqueName(self, name, content_object): """Add a detail to the test, but ensure it's name is unique. @@ -601,7 +618,8 @@ def onException(self, exc_info, tb_label='traceback'): :seealso addOnException: """ if exc_info[0] not in [ - self.skipException, _UnexpectedSuccess, _ExpectedFailure]: + self.skipException, _UnexpectedSuccess, _ExpectedFailure, + ]: self._report_traceback(exc_info, tb_label=tb_label) for handler in self.__exception_handlers: handler(exc_info) diff --git a/tox.ini b/tox.ini index 5c529e86..68364d4a 100644 --- a/tox.ini +++ b/tox.ini @@ -1,15 +1,11 @@ [tox] -envlist = py36,py37,py38,py39,py310,py311,py312,pypy3 -minversion = 1.6 +envlist = py37,py38,py39,py310,py311,py312,pypy3 +minversion = 4.2 [testenv] usedevelop = True -deps = - sphinx - setuptools>=61 - setuptools-scm extras = test twisted commands = - python -W once -m testtools.run testtools.tests.test_suite + python -W once -m testtools.run testtools.tests.test_suite {posargs}