Skip to content

Commit ad2e490

Browse files
authored
Merge pull request #368 from stephenfin/stdlib-alignment
stdlib alignment
2 parents 48d7c5d + 6aa2341 commit ad2e490

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

testtools/testcase.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import sys
2222
import types
2323
import unittest
24+
from unittest.case import SkipTest
2425
import warnings
2526

2627
from testtools.compat import reraise
@@ -49,9 +50,15 @@
4950
)
5051

5152

52-
class TestSkipped(Exception):
53+
class TestSkipped(SkipTest):
5354
"""Raised within TestCase.run() when a test is skipped."""
54-
TestSkipped = try_import('unittest.case.SkipTest', TestSkipped)
55+
def __init__(self, *args, **kwargs):
56+
warnings.warn(
57+
'Use SkipTest from unittest instead.',
58+
DeprecationWarning,
59+
stacklevel=2,
60+
)
61+
super().__init__(*args, **kwargs)
5562

5663

5764
class _UnexpectedSuccess(Exception):
@@ -60,8 +67,6 @@ class _UnexpectedSuccess(Exception):
6067
Note that this exception is private plumbing in testtools' testcase
6168
module.
6269
"""
63-
_UnexpectedSuccess = try_import(
64-
'unittest.case._UnexpectedSuccess', _UnexpectedSuccess)
6570

6671

6772
class _ExpectedFailure(Exception):
@@ -70,8 +75,6 @@ class _ExpectedFailure(Exception):
7075
Note that this exception is private plumbing in testtools' testcase
7176
module.
7277
"""
73-
_ExpectedFailure = try_import(
74-
'unittest.case._ExpectedFailure', _ExpectedFailure)
7578

7679

7780
# Copied from unittest before python 3.4 release. Used to maintain
@@ -222,7 +225,7 @@ class TestCase(unittest.TestCase):
222225
and an optional list of exception handlers.
223226
"""
224227

225-
skipException = TestSkipped
228+
skipException = SkipTest
226229

227230
run_tests_with = RunTest
228231

@@ -276,12 +279,24 @@ def __eq__(self, other):
276279
return False
277280
return self.__dict__ == getattr(other, '__dict__', None)
278281

282+
# We need to explicitly set this since we're overriding __eq__
283+
# https://docs.python.org/3/reference/datamodel.html#object.__hash__
279284
__hash__ = unittest.TestCase.__hash__
280285

281286
def __repr__(self):
282287
# We add id to the repr because it makes testing testtools easier.
283288
return f"<{self.id()} id=0x{id(self):0x}>"
284289

290+
def _deprecate(original_func):
291+
def deprecated_func(*args, **kwargs):
292+
warnings.warn(
293+
'Please use {0} instead.'.format(original_func.__name__),
294+
DeprecationWarning,
295+
stacklevel=2,
296+
)
297+
return original_func(*args, **kwargs)
298+
return deprecated_func
299+
285300
def addDetail(self, name, content_object):
286301
"""Add a detail to be reported with this test's outcome.
287302
@@ -394,7 +409,7 @@ def assertEqual(self, expected, observed, message=''):
394409
matcher = _FlippedEquals(expected)
395410
self.assertThat(observed, matcher, message)
396411

397-
failUnlessEqual = assertEquals = assertEqual
412+
failUnlessEqual = assertEquals = _deprecate(assertEqual)
398413

399414
def assertIn(self, needle, haystack, message=''):
400415
"""Assert that needle is in haystack."""
@@ -468,7 +483,8 @@ def match(self, matchee):
468483
our_callable = Nullary(callableObj, *args, **kwargs)
469484
self.assertThat(our_callable, matcher)
470485
return capture.matchee
471-
failUnlessRaises = assertRaises
486+
487+
failUnlessRaises = _deprecate(assertRaises)
472488

473489
def assertThat(self, matchee, matcher, message='', verbose=False):
474490
"""Assert that matchee is matched by matcher.
@@ -481,7 +497,8 @@ def assertThat(self, matchee, matcher, message='', verbose=False):
481497
if mismatch_error is not None:
482498
raise mismatch_error
483499

484-
assertItemsEqual = unittest.TestCase.assertCountEqual
500+
assertItemsEqual = _deprecate(unittest.TestCase.assertCountEqual)
501+
485502
def addDetailUniqueName(self, name, content_object):
486503
"""Add a detail to the test, but ensure it's name is unique.
487504
@@ -601,7 +618,8 @@ def onException(self, exc_info, tb_label='traceback'):
601618
:seealso addOnException:
602619
"""
603620
if exc_info[0] not in [
604-
self.skipException, _UnexpectedSuccess, _ExpectedFailure]:
621+
self.skipException, _UnexpectedSuccess, _ExpectedFailure,
622+
]:
605623
self._report_traceback(exc_info, tb_label=tb_label)
606624
for handler in self.__exception_handlers:
607625
handler(exc_info)

tox.ini

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
[tox]
2-
envlist = py36,py37,py38,py39,py310,py311,py312,pypy3
3-
minversion = 1.6
2+
envlist = py37,py38,py39,py310,py311,py312,pypy3
3+
minversion = 4.2
44

55
[testenv]
66
usedevelop = True
7-
deps =
8-
sphinx
9-
setuptools>=61
10-
setuptools-scm
117
extras =
128
test
139
twisted
1410
commands =
15-
python -W once -m testtools.run testtools.tests.test_suite
11+
python -W once -m testtools.run testtools.tests.test_suite {posargs}

0 commit comments

Comments
 (0)