unittest

Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's Smalltalk testing framework (used with permission).

This module contains the core framework classes that form the basis of specific test cases and suites (TestCase, TestSuite etc.), and also a text-based utility class for running the tests and reporting the results (TextTestRunner).

Simple usage:

import unittest

class IntegerArithmeticTestCase(unittest.TestCase): def testAdd(self): # test method names begin with 'test' self.assertEqual((1 + 2), 3) self.assertEqual(0 + 1, 1) def testMultiply(self): self.assertEqual((0 * 10), 0) self.assertEqual((5 * 8), 40)

if __name__ == '__main__': unittest.main

Further information is available in the bundled documentation, and from

http://docs.python.org/library/unittest.html

Copyright (c) 1999-2003 Steve Purcell Copyright (c) 2003-2010 Python Software Foundation This module is free software, and you may redistribute it and/or modify it under the same terms as Python itself, so long as this copyright message and disclaimer are retained in their original form.

IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

 1"""
 2Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's
 3Smalltalk testing framework (used with permission).
 4
 5This module contains the core framework classes that form the basis of
 6specific test cases and suites (TestCase, TestSuite etc.), and also a
 7text-based utility class for running the tests and reporting the results
 8 (TextTestRunner).
 9
10Simple usage:
11
12    import unittest
13
14    class IntegerArithmeticTestCase(unittest.TestCase):
15        def testAdd(self):  # test method names begin with 'test'
16            self.assertEqual((1 + 2), 3)
17            self.assertEqual(0 + 1, 1)
18        def testMultiply(self):
19            self.assertEqual((0 * 10), 0)
20            self.assertEqual((5 * 8), 40)
21
22    if __name__ == '__main__':
23        unittest.main()
24
25Further information is available in the bundled documentation, and from
26
27  http://docs.python.org/library/unittest.html
28
29Copyright (c) 1999-2003 Steve Purcell
30Copyright (c) 2003-2010 Python Software Foundation
31This module is free software, and you may redistribute it and/or modify
32it under the same terms as Python itself, so long as this copyright message
33and disclaimer are retained in their original form.
34
35IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
36SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
37THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
38DAMAGE.
39
40THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
41LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
42PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
43AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
44SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45"""
46
47__all__ = ['TestResult', 'TestCase', 'IsolatedAsyncioTestCase', 'TestSuite',
48           'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
49           'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
50           'expectedFailure', 'TextTestResult', 'installHandler',
51           'registerResult', 'removeResult', 'removeHandler',
52           'addModuleCleanup', 'doModuleCleanups', 'enterModuleContext']
53
54__unittest = True
55
56from .result import TestResult
57from .case import (addModuleCleanup, TestCase, FunctionTestCase, SkipTest, skip,
58                   skipIf, skipUnless, expectedFailure, doModuleCleanups,
59                   enterModuleContext)
60from .suite import BaseTestSuite, TestSuite
61from .loader import TestLoader, defaultTestLoader
62from .main import TestProgram, main
63from .runner import TextTestRunner, TextTestResult
64from .signals import installHandler, registerResult, removeResult, removeHandler
65# IsolatedAsyncioTestCase will be imported lazily.
66
67
68# Lazy import of IsolatedAsyncioTestCase from .async_case
69# It imports asyncio, which is relatively heavy, but most tests
70# do not need it.
71
72def __dir__():
73    return globals().keys() | {'IsolatedAsyncioTestCase'}
74
75def __getattr__(name):
76    if name == 'IsolatedAsyncioTestCase':
77        global IsolatedAsyncioTestCase
78        from .async_case import IsolatedAsyncioTestCase
79        return IsolatedAsyncioTestCase
80    raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
class TestResult:
 25class TestResult(object):
 26    """Holder for test result information.
 27
 28    Test results are automatically managed by the TestCase and TestSuite
 29    classes, and do not need to be explicitly manipulated by writers of tests.
 30
 31    Each instance holds the total number of tests run, and collections of
 32    failures and errors that occurred among those test runs. The collections
 33    contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
 34    formatted traceback of the error that occurred.
 35    """
 36    _previousTestClass = None
 37    _testRunEntered = False
 38    _moduleSetUpFailed = False
 39    def __init__(self, stream=None, descriptions=None, verbosity=None):
 40        self.failfast = False
 41        self.failures = []
 42        self.errors = []
 43        self.testsRun = 0
 44        self.skipped = []
 45        self.expectedFailures = []
 46        self.unexpectedSuccesses = []
 47        self.collectedDurations = []
 48        self.shouldStop = False
 49        self.buffer = False
 50        self.tb_locals = False
 51        self._stdout_buffer = None
 52        self._stderr_buffer = None
 53        self._original_stdout = sys.stdout
 54        self._original_stderr = sys.stderr
 55        self._mirrorOutput = False
 56
 57    def printErrors(self):
 58        "Called by TestRunner after test run"
 59
 60    def startTest(self, test):
 61        "Called when the given test is about to be run"
 62        self.testsRun += 1
 63        self._mirrorOutput = False
 64        self._setupStdout()
 65
 66    def _setupStdout(self):
 67        if self.buffer:
 68            if self._stderr_buffer is None:
 69                self._stderr_buffer = io.StringIO()
 70                self._stdout_buffer = io.StringIO()
 71            sys.stdout = self._stdout_buffer
 72            sys.stderr = self._stderr_buffer
 73
 74    def startTestRun(self):
 75        """Called once before any tests are executed.
 76
 77        See startTest for a method called before each test.
 78        """
 79
 80    def stopTest(self, test):
 81        """Called when the given test has been run"""
 82        self._restoreStdout()
 83        self._mirrorOutput = False
 84
 85    def _restoreStdout(self):
 86        if self.buffer:
 87            if self._mirrorOutput:
 88                output = sys.stdout.getvalue()
 89                error = sys.stderr.getvalue()
 90                if output:
 91                    if not output.endswith('\n'):
 92                        output += '\n'
 93                    self._original_stdout.write(STDOUT_LINE % output)
 94                if error:
 95                    if not error.endswith('\n'):
 96                        error += '\n'
 97                    self._original_stderr.write(STDERR_LINE % error)
 98
 99            sys.stdout = self._original_stdout
100            sys.stderr = self._original_stderr
101            self._stdout_buffer.seek(0)
102            self._stdout_buffer.truncate()
103            self._stderr_buffer.seek(0)
104            self._stderr_buffer.truncate()
105
106    def stopTestRun(self):
107        """Called once after all tests are executed.
108
109        See stopTest for a method called after each test.
110        """
111
112    @failfast
113    def addError(self, test, err):
114        """Called when an error has occurred. 'err' is a tuple of values as
115        returned by sys.exc_info().
116        """
117        self.errors.append((test, self._exc_info_to_string(err, test)))
118        self._mirrorOutput = True
119
120    @failfast
121    def addFailure(self, test, err):
122        """Called when an error has occurred. 'err' is a tuple of values as
123        returned by sys.exc_info()."""
124        self.failures.append((test, self._exc_info_to_string(err, test)))
125        self._mirrorOutput = True
126
127    def addSubTest(self, test, subtest, err):
128        """Called at the end of a subtest.
129        'err' is None if the subtest ended successfully, otherwise it's a
130        tuple of values as returned by sys.exc_info().
131        """
132        # By default, we don't do anything with successful subtests, but
133        # more sophisticated test results might want to record them.
134        if err is not None:
135            if getattr(self, 'failfast', False):
136                self.stop()
137            if issubclass(err[0], test.failureException):
138                errors = self.failures
139            else:
140                errors = self.errors
141            errors.append((subtest, self._exc_info_to_string(err, test)))
142            self._mirrorOutput = True
143
144    def addSuccess(self, test):
145        "Called when a test has completed successfully"
146        pass
147
148    def addSkip(self, test, reason):
149        """Called when a test is skipped."""
150        self.skipped.append((test, reason))
151
152    def addExpectedFailure(self, test, err):
153        """Called when an expected failure/error occurred."""
154        self.expectedFailures.append(
155            (test, self._exc_info_to_string(err, test)))
156
157    @failfast
158    def addUnexpectedSuccess(self, test):
159        """Called when a test was expected to fail, but succeed."""
160        self.unexpectedSuccesses.append(test)
161
162    def addDuration(self, test, elapsed):
163        """Called when a test finished to run, regardless of its outcome.
164        *test* is the test case corresponding to the test method.
165        *elapsed* is the time represented in seconds, and it includes the
166        execution of cleanup functions.
167        """
168        # support for a TextTestRunner using an old TestResult class
169        if hasattr(self, "collectedDurations"):
170            # Pass test repr and not the test object itself to avoid resources leak
171            self.collectedDurations.append((str(test), elapsed))
172
173    def wasSuccessful(self):
174        """Tells whether or not this result was a success."""
175        # The hasattr check is for test_result's OldResult test.  That
176        # way this method works on objects that lack the attribute.
177        # (where would such result instances come from? old stored pickles?)
178        return ((len(self.failures) == len(self.errors) == 0) and
179                (not hasattr(self, 'unexpectedSuccesses') or
180                 len(self.unexpectedSuccesses) == 0))
181
182    def stop(self):
183        """Indicates that the tests should be aborted."""
184        self.shouldStop = True
185
186    def _exc_info_to_string(self, err, test):
187        """Converts a sys.exc_info()-style tuple of values into a string."""
188        exctype, value, tb = err
189        tb = self._clean_tracebacks(exctype, value, tb, test)
190        tb_e = traceback.TracebackException(
191            exctype, value, tb,
192            capture_locals=self.tb_locals, compact=True)
193        msgLines = list(tb_e.format())
194
195        if self.buffer:
196            output = sys.stdout.getvalue()
197            error = sys.stderr.getvalue()
198            if output:
199                if not output.endswith('\n'):
200                    output += '\n'
201                msgLines.append(STDOUT_LINE % output)
202            if error:
203                if not error.endswith('\n'):
204                    error += '\n'
205                msgLines.append(STDERR_LINE % error)
206        return ''.join(msgLines)
207
208    def _clean_tracebacks(self, exctype, value, tb, test):
209        ret = None
210        first = True
211        excs = [(exctype, value, tb)]
212        seen = {id(value)}  # Detect loops in chained exceptions.
213        while excs:
214            (exctype, value, tb) = excs.pop()
215            # Skip test runner traceback levels
216            while tb and self._is_relevant_tb_level(tb):
217                tb = tb.tb_next
218
219            # Skip assert*() traceback levels
220            if exctype is test.failureException:
221                self._remove_unittest_tb_frames(tb)
222
223            if first:
224                ret = tb
225                first = False
226            else:
227                value.__traceback__ = tb
228
229            if value is not None:
230                for c in (value.__cause__, value.__context__):
231                    if c is not None and id(c) not in seen:
232                        excs.append((type(c), c, c.__traceback__))
233                        seen.add(id(c))
234        return ret
235
236    def _is_relevant_tb_level(self, tb):
237        return '__unittest' in tb.tb_frame.f_globals
238
239    def _remove_unittest_tb_frames(self, tb):
240        '''Truncates usercode tb at the first unittest frame.
241
242        If the first frame of the traceback is in user code,
243        the prefix up to the first unittest frame is returned.
244        If the first frame is already in the unittest module,
245        the traceback is not modified.
246        '''
247        prev = None
248        while tb and not self._is_relevant_tb_level(tb):
249            prev = tb
250            tb = tb.tb_next
251        if prev is not None:
252            prev.tb_next = None
253
254    def __repr__(self):
255        return ("<%s run=%i errors=%i failures=%i>" %
256               (util.strclass(self.__class__), self.testsRun, len(self.errors),
257                len(self.failures)))

Holder for test result information.

Test results are automatically managed by the TestCase and TestSuite classes, and do not need to be explicitly manipulated by writers of tests.

Each instance holds the total number of tests run, and collections of failures and errors that occurred among those test runs. The collections contain tuples of (testcase, exceptioninfo), where exceptioninfo is the formatted traceback of the error that occurred.

TestResult(stream=None, descriptions=None, verbosity=None)
39    def __init__(self, stream=None, descriptions=None, verbosity=None):
40        self.failfast = False
41        self.failures = []
42        self.errors = []
43        self.testsRun = 0
44        self.skipped = []
45        self.expectedFailures = []
46        self.unexpectedSuccesses = []
47        self.collectedDurations = []
48        self.shouldStop = False
49        self.buffer = False
50        self.tb_locals = False
51        self._stdout_buffer = None
52        self._stderr_buffer = None
53        self._original_stdout = sys.stdout
54        self._original_stderr = sys.stderr
55        self._mirrorOutput = False
failfast
failures
errors
testsRun
skipped
expectedFailures
unexpectedSuccesses
collectedDurations
shouldStop
buffer
tb_locals
def printErrors(self):
57    def printErrors(self):
58        "Called by TestRunner after test run"

Called by TestRunner after test run

def startTest(self, test):
60    def startTest(self, test):
61        "Called when the given test is about to be run"
62        self.testsRun += 1
63        self._mirrorOutput = False
64        self._setupStdout()

Called when the given test is about to be run

def startTestRun(self):
74    def startTestRun(self):
75        """Called once before any tests are executed.
76
77        See startTest for a method called before each test.
78        """

Called once before any tests are executed.

See startTest for a method called before each test.

def stopTest(self, test):
80    def stopTest(self, test):
81        """Called when the given test has been run"""
82        self._restoreStdout()
83        self._mirrorOutput = False

Called when the given test has been run

def stopTestRun(self):
106    def stopTestRun(self):
107        """Called once after all tests are executed.
108
109        See stopTest for a method called after each test.
110        """

Called once after all tests are executed.

See stopTest for a method called after each test.

@failfast
def addError(self, test, err):
112    @failfast
113    def addError(self, test, err):
114        """Called when an error has occurred. 'err' is a tuple of values as
115        returned by sys.exc_info().
116        """
117        self.errors.append((test, self._exc_info_to_string(err, test)))
118        self._mirrorOutput = True

Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().

@failfast
def addFailure(self, test, err):
120    @failfast
121    def addFailure(self, test, err):
122        """Called when an error has occurred. 'err' is a tuple of values as
123        returned by sys.exc_info()."""
124        self.failures.append((test, self._exc_info_to_string(err, test)))
125        self._mirrorOutput = True

Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().

def addSubTest(self, test, subtest, err):
127    def addSubTest(self, test, subtest, err):
128        """Called at the end of a subtest.
129        'err' is None if the subtest ended successfully, otherwise it's a
130        tuple of values as returned by sys.exc_info().
131        """
132        # By default, we don't do anything with successful subtests, but
133        # more sophisticated test results might want to record them.
134        if err is not None:
135            if getattr(self, 'failfast', False):
136                self.stop()
137            if issubclass(err[0], test.failureException):
138                errors = self.failures
139            else:
140                errors = self.errors
141            errors.append((subtest, self._exc_info_to_string(err, test)))
142            self._mirrorOutput = True

Called at the end of a subtest. 'err' is None if the subtest ended successfully, otherwise it's a tuple of values as returned by sys.exc_info().

def addSuccess(self, test):
144    def addSuccess(self, test):
145        "Called when a test has completed successfully"
146        pass

Called when a test has completed successfully

def addSkip(self, test, reason):
148    def addSkip(self, test, reason):
149        """Called when a test is skipped."""
150        self.skipped.append((test, reason))

Called when a test is skipped.

def addExpectedFailure(self, test, err):
152    def addExpectedFailure(self, test, err):
153        """Called when an expected failure/error occurred."""
154        self.expectedFailures.append(
155            (test, self._exc_info_to_string(err, test)))

Called when an expected failure/error occurred.

@failfast
def addUnexpectedSuccess(self, test):
157    @failfast
158    def addUnexpectedSuccess(self, test):
159        """Called when a test was expected to fail, but succeed."""
160        self.unexpectedSuccesses.append(test)

Called when a test was expected to fail, but succeed.

def addDuration(self, test, elapsed):
162    def addDuration(self, test, elapsed):
163        """Called when a test finished to run, regardless of its outcome.
164        *test* is the test case corresponding to the test method.
165        *elapsed* is the time represented in seconds, and it includes the
166        execution of cleanup functions.
167        """
168        # support for a TextTestRunner using an old TestResult class
169        if hasattr(self, "collectedDurations"):
170            # Pass test repr and not the test object itself to avoid resources leak
171            self.collectedDurations.append((str(test), elapsed))

Called when a test finished to run, regardless of its outcome. test is the test case corresponding to the test method. elapsed is the time represented in seconds, and it includes the execution of cleanup functions.

def wasSuccessful(self):
173    def wasSuccessful(self):
174        """Tells whether or not this result was a success."""
175        # The hasattr check is for test_result's OldResult test.  That
176        # way this method works on objects that lack the attribute.
177        # (where would such result instances come from? old stored pickles?)
178        return ((len(self.failures) == len(self.errors) == 0) and
179                (not hasattr(self, 'unexpectedSuccesses') or
180                 len(self.unexpectedSuccesses) == 0))

Tells whether or not this result was a success.

def stop(self):
182    def stop(self):
183        """Indicates that the tests should be aborted."""
184        self.shouldStop = True

Indicates that the tests should be aborted.

class TestCase:
 363class TestCase(object):
 364    """A class whose instances are single test cases.
 365
 366    By default, the test code itself should be placed in a method named
 367    'runTest'.
 368
 369    If the fixture may be used for many test cases, create as
 370    many test methods as are needed. When instantiating such a TestCase
 371    subclass, specify in the constructor arguments the name of the test method
 372    that the instance is to execute.
 373
 374    Test authors should subclass TestCase for their own tests. Construction
 375    and deconstruction of the test's environment ('fixture') can be
 376    implemented by overriding the 'setUp' and 'tearDown' methods respectively.
 377
 378    If it is necessary to override the __init__ method, the base class
 379    __init__ method must always be called. It is important that subclasses
 380    should not change the signature of their __init__ method, since instances
 381    of the classes are instantiated automatically by parts of the framework
 382    in order to be run.
 383
 384    When subclassing TestCase, you can set these attributes:
 385    * failureException: determines which exception will be raised when
 386        the instance's assertion methods fail; test methods raising this
 387        exception will be deemed to have 'failed' rather than 'errored'.
 388    * longMessage: determines whether long messages (including repr of
 389        objects used in assert methods) will be printed on failure in *addition*
 390        to any explicit message passed.
 391    * maxDiff: sets the maximum length of a diff in failure messages
 392        by assert methods using difflib. It is looked up as an instance
 393        attribute so can be configured by individual tests if required.
 394    """
 395
 396    failureException = AssertionError
 397
 398    longMessage = True
 399
 400    maxDiff = 80*8
 401
 402    # If a string is longer than _diffThreshold, use normal comparison instead
 403    # of difflib.  See #11763.
 404    _diffThreshold = 2**16
 405
 406    def __init_subclass__(cls, *args, **kwargs):
 407        # Attribute used by TestSuite for classSetUp
 408        cls._classSetupFailed = False
 409        cls._class_cleanups = []
 410        super().__init_subclass__(*args, **kwargs)
 411
 412    def __init__(self, methodName='runTest'):
 413        """Create an instance of the class that will use the named test
 414           method when executed. Raises a ValueError if the instance does
 415           not have a method with the specified name.
 416        """
 417        self._testMethodName = methodName
 418        self._outcome = None
 419        self._testMethodDoc = 'No test'
 420        try:
 421            testMethod = getattr(self, methodName)
 422        except AttributeError:
 423            if methodName != 'runTest':
 424                # we allow instantiation with no explicit method name
 425                # but not an *incorrect* or missing method name
 426                raise ValueError("no such test method in %s: %s" %
 427                      (self.__class__, methodName))
 428        else:
 429            self._testMethodDoc = testMethod.__doc__
 430        self._cleanups = []
 431        self._subtest = None
 432
 433        # Map types to custom assertEqual functions that will compare
 434        # instances of said type in more detail to generate a more useful
 435        # error message.
 436        self._type_equality_funcs = {}
 437        self.addTypeEqualityFunc(dict, 'assertDictEqual')
 438        self.addTypeEqualityFunc(list, 'assertListEqual')
 439        self.addTypeEqualityFunc(tuple, 'assertTupleEqual')
 440        self.addTypeEqualityFunc(set, 'assertSetEqual')
 441        self.addTypeEqualityFunc(frozenset, 'assertSetEqual')
 442        self.addTypeEqualityFunc(str, 'assertMultiLineEqual')
 443
 444    def addTypeEqualityFunc(self, typeobj, function):
 445        """Add a type specific assertEqual style function to compare a type.
 446
 447        This method is for use by TestCase subclasses that need to register
 448        their own type equality functions to provide nicer error messages.
 449
 450        Args:
 451            typeobj: The data type to call this function on when both values
 452                    are of the same type in assertEqual().
 453            function: The callable taking two arguments and an optional
 454                    msg= argument that raises self.failureException with a
 455                    useful error message when the two arguments are not equal.
 456        """
 457        self._type_equality_funcs[typeobj] = function
 458
 459    def addCleanup(self, function, /, *args, **kwargs):
 460        """Add a function, with arguments, to be called when the test is
 461        completed. Functions added are called on a LIFO basis and are
 462        called after tearDown on test failure or success.
 463
 464        Cleanup items are called even if setUp fails (unlike tearDown)."""
 465        self._cleanups.append((function, args, kwargs))
 466
 467    def enterContext(self, cm):
 468        """Enters the supplied context manager.
 469
 470        If successful, also adds its __exit__ method as a cleanup
 471        function and returns the result of the __enter__ method.
 472        """
 473        return _enter_context(cm, self.addCleanup)
 474
 475    @classmethod
 476    def addClassCleanup(cls, function, /, *args, **kwargs):
 477        """Same as addCleanup, except the cleanup items are called even if
 478        setUpClass fails (unlike tearDownClass)."""
 479        cls._class_cleanups.append((function, args, kwargs))
 480
 481    @classmethod
 482    def enterClassContext(cls, cm):
 483        """Same as enterContext, but class-wide."""
 484        return _enter_context(cm, cls.addClassCleanup)
 485
 486    def setUp(self):
 487        "Hook method for setting up the test fixture before exercising it."
 488        pass
 489
 490    def tearDown(self):
 491        "Hook method for deconstructing the test fixture after testing it."
 492        pass
 493
 494    @classmethod
 495    def setUpClass(cls):
 496        "Hook method for setting up class fixture before running tests in the class."
 497
 498    @classmethod
 499    def tearDownClass(cls):
 500        "Hook method for deconstructing the class fixture after running all tests in the class."
 501
 502    def countTestCases(self):
 503        return 1
 504
 505    def defaultTestResult(self):
 506        return result.TestResult()
 507
 508    def shortDescription(self):
 509        """Returns a one-line description of the test, or None if no
 510        description has been provided.
 511
 512        The default implementation of this method returns the first line of
 513        the specified test method's docstring.
 514        """
 515        doc = self._testMethodDoc
 516        return doc.strip().split("\n")[0].strip() if doc else None
 517
 518
 519    def id(self):
 520        return "%s.%s" % (strclass(self.__class__), self._testMethodName)
 521
 522    def __eq__(self, other):
 523        if type(self) is not type(other):
 524            return NotImplemented
 525
 526        return self._testMethodName == other._testMethodName
 527
 528    def __hash__(self):
 529        return hash((type(self), self._testMethodName))
 530
 531    def __str__(self):
 532        return "%s (%s.%s)" % (self._testMethodName, strclass(self.__class__), self._testMethodName)
 533
 534    def __repr__(self):
 535        return "<%s testMethod=%s>" % \
 536               (strclass(self.__class__), self._testMethodName)
 537
 538    @contextlib.contextmanager
 539    def subTest(self, msg=_subtest_msg_sentinel, **params):
 540        """Return a context manager that will return the enclosed block
 541        of code in a subtest identified by the optional message and
 542        keyword parameters.  A failure in the subtest marks the test
 543        case as failed but resumes execution at the end of the enclosed
 544        block, allowing further test code to be executed.
 545        """
 546        if self._outcome is None or not self._outcome.result_supports_subtests:
 547            yield
 548            return
 549        parent = self._subtest
 550        if parent is None:
 551            params_map = _OrderedChainMap(params)
 552        else:
 553            params_map = parent.params.new_child(params)
 554        self._subtest = _SubTest(self, msg, params_map)
 555        try:
 556            with self._outcome.testPartExecutor(self._subtest, subTest=True):
 557                yield
 558            if not self._outcome.success:
 559                result = self._outcome.result
 560                if result is not None and result.failfast:
 561                    raise _ShouldStop
 562            elif self._outcome.expectedFailure:
 563                # If the test is expecting a failure, we really want to
 564                # stop now and register the expected failure.
 565                raise _ShouldStop
 566        finally:
 567            self._subtest = parent
 568
 569    def _addExpectedFailure(self, result, exc_info):
 570        try:
 571            addExpectedFailure = result.addExpectedFailure
 572        except AttributeError:
 573            warnings.warn("TestResult has no addExpectedFailure method, reporting as passes",
 574                          RuntimeWarning)
 575            result.addSuccess(self)
 576        else:
 577            addExpectedFailure(self, exc_info)
 578
 579    def _addUnexpectedSuccess(self, result):
 580        try:
 581            addUnexpectedSuccess = result.addUnexpectedSuccess
 582        except AttributeError:
 583            warnings.warn("TestResult has no addUnexpectedSuccess method, reporting as failure",
 584                          RuntimeWarning)
 585            # We need to pass an actual exception and traceback to addFailure,
 586            # otherwise the legacy result can choke.
 587            try:
 588                raise _UnexpectedSuccess from None
 589            except _UnexpectedSuccess:
 590                result.addFailure(self, sys.exc_info())
 591        else:
 592            addUnexpectedSuccess(self)
 593
 594    def _addDuration(self, result, elapsed):
 595        try:
 596            addDuration = result.addDuration
 597        except AttributeError:
 598            warnings.warn("TestResult has no addDuration method",
 599                          RuntimeWarning)
 600        else:
 601            addDuration(self, elapsed)
 602
 603    def _callSetUp(self):
 604        self.setUp()
 605
 606    def _callTestMethod(self, method):
 607        if method() is not None:
 608            warnings.warn(f'It is deprecated to return a value that is not None from a '
 609                          f'test case ({method})', DeprecationWarning, stacklevel=3)
 610
 611    def _callTearDown(self):
 612        self.tearDown()
 613
 614    def _callCleanup(self, function, /, *args, **kwargs):
 615        function(*args, **kwargs)
 616
 617    def run(self, result=None):
 618        if result is None:
 619            result = self.defaultTestResult()
 620            startTestRun = getattr(result, 'startTestRun', None)
 621            stopTestRun = getattr(result, 'stopTestRun', None)
 622            if startTestRun is not None:
 623                startTestRun()
 624        else:
 625            stopTestRun = None
 626
 627        result.startTest(self)
 628        try:
 629            testMethod = getattr(self, self._testMethodName)
 630            if (getattr(self.__class__, "__unittest_skip__", False) or
 631                getattr(testMethod, "__unittest_skip__", False)):
 632                # If the class or method was skipped.
 633                skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
 634                            or getattr(testMethod, '__unittest_skip_why__', ''))
 635                _addSkip(result, self, skip_why)
 636                return result
 637
 638            expecting_failure = (
 639                getattr(self, "__unittest_expecting_failure__", False) or
 640                getattr(testMethod, "__unittest_expecting_failure__", False)
 641            )
 642            outcome = _Outcome(result)
 643            start_time = time.perf_counter()
 644            try:
 645                self._outcome = outcome
 646
 647                with outcome.testPartExecutor(self):
 648                    self._callSetUp()
 649                if outcome.success:
 650                    outcome.expecting_failure = expecting_failure
 651                    with outcome.testPartExecutor(self):
 652                        self._callTestMethod(testMethod)
 653                    outcome.expecting_failure = False
 654                    with outcome.testPartExecutor(self):
 655                        self._callTearDown()
 656                self.doCleanups()
 657                self._addDuration(result, (time.perf_counter() - start_time))
 658
 659                if outcome.success:
 660                    if expecting_failure:
 661                        if outcome.expectedFailure:
 662                            self._addExpectedFailure(result, outcome.expectedFailure)
 663                        else:
 664                            self._addUnexpectedSuccess(result)
 665                    else:
 666                        result.addSuccess(self)
 667                return result
 668            finally:
 669                # explicitly break reference cycle:
 670                # outcome.expectedFailure -> frame -> outcome -> outcome.expectedFailure
 671                outcome.expectedFailure = None
 672                outcome = None
 673
 674                # clear the outcome, no more needed
 675                self._outcome = None
 676
 677        finally:
 678            result.stopTest(self)
 679            if stopTestRun is not None:
 680                stopTestRun()
 681
 682    def doCleanups(self):
 683        """Execute all cleanup functions. Normally called for you after
 684        tearDown."""
 685        outcome = self._outcome or _Outcome()
 686        while self._cleanups:
 687            function, args, kwargs = self._cleanups.pop()
 688            with outcome.testPartExecutor(self):
 689                self._callCleanup(function, *args, **kwargs)
 690
 691        # return this for backwards compatibility
 692        # even though we no longer use it internally
 693        return outcome.success
 694
 695    @classmethod
 696    def doClassCleanups(cls):
 697        """Execute all class cleanup functions. Normally called for you after
 698        tearDownClass."""
 699        cls.tearDown_exceptions = []
 700        while cls._class_cleanups:
 701            function, args, kwargs = cls._class_cleanups.pop()
 702            try:
 703                function(*args, **kwargs)
 704            except Exception:
 705                cls.tearDown_exceptions.append(sys.exc_info())
 706
 707    def __call__(self, *args, **kwds):
 708        return self.run(*args, **kwds)
 709
 710    def debug(self):
 711        """Run the test without collecting errors in a TestResult"""
 712        testMethod = getattr(self, self._testMethodName)
 713        if (getattr(self.__class__, "__unittest_skip__", False) or
 714            getattr(testMethod, "__unittest_skip__", False)):
 715            # If the class or method was skipped.
 716            skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
 717                        or getattr(testMethod, '__unittest_skip_why__', ''))
 718            raise SkipTest(skip_why)
 719
 720        self._callSetUp()
 721        self._callTestMethod(testMethod)
 722        self._callTearDown()
 723        while self._cleanups:
 724            function, args, kwargs = self._cleanups.pop()
 725            self._callCleanup(function, *args, **kwargs)
 726
 727    def skipTest(self, reason):
 728        """Skip this test."""
 729        raise SkipTest(reason)
 730
 731    def fail(self, msg=None):
 732        """Fail immediately, with the given message."""
 733        raise self.failureException(msg)
 734
 735    def assertFalse(self, expr, msg=None):
 736        """Check that the expression is false."""
 737        if expr:
 738            msg = self._formatMessage(msg, "%s is not false" % safe_repr(expr))
 739            raise self.failureException(msg)
 740
 741    def assertTrue(self, expr, msg=None):
 742        """Check that the expression is true."""
 743        if not expr:
 744            msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
 745            raise self.failureException(msg)
 746
 747    def _formatMessage(self, msg, standardMsg):
 748        """Honour the longMessage attribute when generating failure messages.
 749        If longMessage is False this means:
 750        * Use only an explicit message if it is provided
 751        * Otherwise use the standard message for the assert
 752
 753        If longMessage is True:
 754        * Use the standard message
 755        * If an explicit message is provided, plus ' : ' and the explicit message
 756        """
 757        if not self.longMessage:
 758            return msg or standardMsg
 759        if msg is None:
 760            return standardMsg
 761        try:
 762            # don't switch to '{}' formatting in Python 2.X
 763            # it changes the way unicode input is handled
 764            return '%s : %s' % (standardMsg, msg)
 765        except UnicodeDecodeError:
 766            return  '%s : %s' % (safe_repr(standardMsg), safe_repr(msg))
 767
 768    def assertRaises(self, expected_exception, *args, **kwargs):
 769        """Fail unless an exception of class expected_exception is raised
 770           by the callable when invoked with specified positional and
 771           keyword arguments. If a different type of exception is
 772           raised, it will not be caught, and the test case will be
 773           deemed to have suffered an error, exactly as for an
 774           unexpected exception.
 775
 776           If called with the callable and arguments omitted, will return a
 777           context object used like this::
 778
 779                with self.assertRaises(SomeException):
 780                    do_something()
 781
 782           An optional keyword argument 'msg' can be provided when assertRaises
 783           is used as a context object.
 784
 785           The context manager keeps a reference to the exception as
 786           the 'exception' attribute. This allows you to inspect the
 787           exception after the assertion::
 788
 789               with self.assertRaises(SomeException) as cm:
 790                   do_something()
 791               the_exception = cm.exception
 792               self.assertEqual(the_exception.error_code, 3)
 793        """
 794        context = _AssertRaisesContext(expected_exception, self)
 795        try:
 796            return context.handle('assertRaises', args, kwargs)
 797        finally:
 798            # bpo-23890: manually break a reference cycle
 799            context = None
 800
 801    def assertWarns(self, expected_warning, *args, **kwargs):
 802        """Fail unless a warning of class warnClass is triggered
 803           by the callable when invoked with specified positional and
 804           keyword arguments.  If a different type of warning is
 805           triggered, it will not be handled: depending on the other
 806           warning filtering rules in effect, it might be silenced, printed
 807           out, or raised as an exception.
 808
 809           If called with the callable and arguments omitted, will return a
 810           context object used like this::
 811
 812                with self.assertWarns(SomeWarning):
 813                    do_something()
 814
 815           An optional keyword argument 'msg' can be provided when assertWarns
 816           is used as a context object.
 817
 818           The context manager keeps a reference to the first matching
 819           warning as the 'warning' attribute; similarly, the 'filename'
 820           and 'lineno' attributes give you information about the line
 821           of Python code from which the warning was triggered.
 822           This allows you to inspect the warning after the assertion::
 823
 824               with self.assertWarns(SomeWarning) as cm:
 825                   do_something()
 826               the_warning = cm.warning
 827               self.assertEqual(the_warning.some_attribute, 147)
 828        """
 829        context = _AssertWarnsContext(expected_warning, self)
 830        return context.handle('assertWarns', args, kwargs)
 831
 832    def _assertNotWarns(self, expected_warning, *args, **kwargs):
 833        """The opposite of assertWarns. Private due to low demand."""
 834        context = _AssertNotWarnsContext(expected_warning, self)
 835        return context.handle('_assertNotWarns', args, kwargs)
 836
 837    def assertLogs(self, logger=None, level=None):
 838        """Fail unless a log message of level *level* or higher is emitted
 839        on *logger_name* or its children.  If omitted, *level* defaults to
 840        INFO and *logger* defaults to the root logger.
 841
 842        This method must be used as a context manager, and will yield
 843        a recording object with two attributes: `output` and `records`.
 844        At the end of the context manager, the `output` attribute will
 845        be a list of the matching formatted log messages and the
 846        `records` attribute will be a list of the corresponding LogRecord
 847        objects.
 848
 849        Example::
 850
 851            with self.assertLogs('foo', level='INFO') as cm:
 852                logging.getLogger('foo').info('first message')
 853                logging.getLogger('foo.bar').error('second message')
 854            self.assertEqual(cm.output, ['INFO:foo:first message',
 855                                         'ERROR:foo.bar:second message'])
 856        """
 857        # Lazy import to avoid importing logging if it is not needed.
 858        from ._log import _AssertLogsContext
 859        return _AssertLogsContext(self, logger, level, no_logs=False)
 860
 861    def assertNoLogs(self, logger=None, level=None):
 862        """ Fail unless no log messages of level *level* or higher are emitted
 863        on *logger_name* or its children.
 864
 865        This method must be used as a context manager.
 866        """
 867        from ._log import _AssertLogsContext
 868        return _AssertLogsContext(self, logger, level, no_logs=True)
 869
 870    def _getAssertEqualityFunc(self, first, second):
 871        """Get a detailed comparison function for the types of the two args.
 872
 873        Returns: A callable accepting (first, second, msg=None) that will
 874        raise a failure exception if first != second with a useful human
 875        readable error message for those types.
 876        """
 877        #
 878        # NOTE(gregory.p.smith): I considered isinstance(first, type(second))
 879        # and vice versa.  I opted for the conservative approach in case
 880        # subclasses are not intended to be compared in detail to their super
 881        # class instances using a type equality func.  This means testing
 882        # subtypes won't automagically use the detailed comparison.  Callers
 883        # should use their type specific assertSpamEqual method to compare
 884        # subclasses if the detailed comparison is desired and appropriate.
 885        # See the discussion in http://bugs.python.org/issue2578.
 886        #
 887        if type(first) is type(second):
 888            asserter = self._type_equality_funcs.get(type(first))
 889            if asserter is not None:
 890                if isinstance(asserter, str):
 891                    asserter = getattr(self, asserter)
 892                return asserter
 893
 894        return self._baseAssertEqual
 895
 896    def _baseAssertEqual(self, first, second, msg=None):
 897        """The default assertEqual implementation, not type specific."""
 898        if not first == second:
 899            standardMsg = '%s != %s' % _common_shorten_repr(first, second)
 900            msg = self._formatMessage(msg, standardMsg)
 901            raise self.failureException(msg)
 902
 903    def assertEqual(self, first, second, msg=None):
 904        """Fail if the two objects are unequal as determined by the '=='
 905           operator.
 906        """
 907        assertion_func = self._getAssertEqualityFunc(first, second)
 908        assertion_func(first, second, msg=msg)
 909
 910    def assertNotEqual(self, first, second, msg=None):
 911        """Fail if the two objects are equal as determined by the '!='
 912           operator.
 913        """
 914        if not first != second:
 915            msg = self._formatMessage(msg, '%s == %s' % (safe_repr(first),
 916                                                          safe_repr(second)))
 917            raise self.failureException(msg)
 918
 919    def assertAlmostEqual(self, first, second, places=None, msg=None,
 920                          delta=None):
 921        """Fail if the two objects are unequal as determined by their
 922           difference rounded to the given number of decimal places
 923           (default 7) and comparing to zero, or by comparing that the
 924           difference between the two objects is more than the given
 925           delta.
 926
 927           Note that decimal places (from zero) are usually not the same
 928           as significant digits (measured from the most significant digit).
 929
 930           If the two objects compare equal then they will automatically
 931           compare almost equal.
 932        """
 933        if first == second:
 934            # shortcut
 935            return
 936        if delta is not None and places is not None:
 937            raise TypeError("specify delta or places not both")
 938
 939        diff = abs(first - second)
 940        if delta is not None:
 941            if diff <= delta:
 942                return
 943
 944            standardMsg = '%s != %s within %s delta (%s difference)' % (
 945                safe_repr(first),
 946                safe_repr(second),
 947                safe_repr(delta),
 948                safe_repr(diff))
 949        else:
 950            if places is None:
 951                places = 7
 952
 953            if round(diff, places) == 0:
 954                return
 955
 956            standardMsg = '%s != %s within %r places (%s difference)' % (
 957                safe_repr(first),
 958                safe_repr(second),
 959                places,
 960                safe_repr(diff))
 961        msg = self._formatMessage(msg, standardMsg)
 962        raise self.failureException(msg)
 963
 964    def assertNotAlmostEqual(self, first, second, places=None, msg=None,
 965                             delta=None):
 966        """Fail if the two objects are equal as determined by their
 967           difference rounded to the given number of decimal places
 968           (default 7) and comparing to zero, or by comparing that the
 969           difference between the two objects is less than the given delta.
 970
 971           Note that decimal places (from zero) are usually not the same
 972           as significant digits (measured from the most significant digit).
 973
 974           Objects that are equal automatically fail.
 975        """
 976        if delta is not None and places is not None:
 977            raise TypeError("specify delta or places not both")
 978        diff = abs(first - second)
 979        if delta is not None:
 980            if not (first == second) and diff > delta:
 981                return
 982            standardMsg = '%s == %s within %s delta (%s difference)' % (
 983                safe_repr(first),
 984                safe_repr(second),
 985                safe_repr(delta),
 986                safe_repr(diff))
 987        else:
 988            if places is None:
 989                places = 7
 990            if not (first == second) and round(diff, places) != 0:
 991                return
 992            standardMsg = '%s == %s within %r places' % (safe_repr(first),
 993                                                         safe_repr(second),
 994                                                         places)
 995
 996        msg = self._formatMessage(msg, standardMsg)
 997        raise self.failureException(msg)
 998
 999    def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
1000        """An equality assertion for ordered sequences (like lists and tuples).
1001
1002        For the purposes of this function, a valid ordered sequence type is one
1003        which can be indexed, has a length, and has an equality operator.
1004
1005        Args:
1006            seq1: The first sequence to compare.
1007            seq2: The second sequence to compare.
1008            seq_type: The expected datatype of the sequences, or None if no
1009                    datatype should be enforced.
1010            msg: Optional message to use on failure instead of a list of
1011                    differences.
1012        """
1013        if seq_type is not None:
1014            seq_type_name = seq_type.__name__
1015            if not isinstance(seq1, seq_type):
1016                raise self.failureException('First sequence is not a %s: %s'
1017                                        % (seq_type_name, safe_repr(seq1)))
1018            if not isinstance(seq2, seq_type):
1019                raise self.failureException('Second sequence is not a %s: %s'
1020                                        % (seq_type_name, safe_repr(seq2)))
1021        else:
1022            seq_type_name = "sequence"
1023
1024        differing = None
1025        try:
1026            len1 = len(seq1)
1027        except (TypeError, NotImplementedError):
1028            differing = 'First %s has no length.    Non-sequence?' % (
1029                    seq_type_name)
1030
1031        if differing is None:
1032            try:
1033                len2 = len(seq2)
1034            except (TypeError, NotImplementedError):
1035                differing = 'Second %s has no length.    Non-sequence?' % (
1036                        seq_type_name)
1037
1038        if differing is None:
1039            if seq1 == seq2:
1040                return
1041
1042            differing = '%ss differ: %s != %s\n' % (
1043                    (seq_type_name.capitalize(),) +
1044                    _common_shorten_repr(seq1, seq2))
1045
1046            for i in range(min(len1, len2)):
1047                try:
1048                    item1 = seq1[i]
1049                except (TypeError, IndexError, NotImplementedError):
1050                    differing += ('\nUnable to index element %d of first %s\n' %
1051                                 (i, seq_type_name))
1052                    break
1053
1054                try:
1055                    item2 = seq2[i]
1056                except (TypeError, IndexError, NotImplementedError):
1057                    differing += ('\nUnable to index element %d of second %s\n' %
1058                                 (i, seq_type_name))
1059                    break
1060
1061                if item1 != item2:
1062                    differing += ('\nFirst differing element %d:\n%s\n%s\n' %
1063                                 ((i,) + _common_shorten_repr(item1, item2)))
1064                    break
1065            else:
1066                if (len1 == len2 and seq_type is None and
1067                    type(seq1) != type(seq2)):
1068                    # The sequences are the same, but have differing types.
1069                    return
1070
1071            if len1 > len2:
1072                differing += ('\nFirst %s contains %d additional '
1073                             'elements.\n' % (seq_type_name, len1 - len2))
1074                try:
1075                    differing += ('First extra element %d:\n%s\n' %
1076                                  (len2, safe_repr(seq1[len2])))
1077                except (TypeError, IndexError, NotImplementedError):
1078                    differing += ('Unable to index element %d '
1079                                  'of first %s\n' % (len2, seq_type_name))
1080            elif len1 < len2:
1081                differing += ('\nSecond %s contains %d additional '
1082                             'elements.\n' % (seq_type_name, len2 - len1))
1083                try:
1084                    differing += ('First extra element %d:\n%s\n' %
1085                                  (len1, safe_repr(seq2[len1])))
1086                except (TypeError, IndexError, NotImplementedError):
1087                    differing += ('Unable to index element %d '
1088                                  'of second %s\n' % (len1, seq_type_name))
1089        standardMsg = differing
1090        diffMsg = '\n' + '\n'.join(
1091            difflib.ndiff(pprint.pformat(seq1).splitlines(),
1092                          pprint.pformat(seq2).splitlines()))
1093
1094        standardMsg = self._truncateMessage(standardMsg, diffMsg)
1095        msg = self._formatMessage(msg, standardMsg)
1096        self.fail(msg)
1097
1098    def _truncateMessage(self, message, diff):
1099        max_diff = self.maxDiff
1100        if max_diff is None or len(diff) <= max_diff:
1101            return message + diff
1102        return message + (DIFF_OMITTED % len(diff))
1103
1104    def assertListEqual(self, list1, list2, msg=None):
1105        """A list-specific equality assertion.
1106
1107        Args:
1108            list1: The first list to compare.
1109            list2: The second list to compare.
1110            msg: Optional message to use on failure instead of a list of
1111                    differences.
1112
1113        """
1114        self.assertSequenceEqual(list1, list2, msg, seq_type=list)
1115
1116    def assertTupleEqual(self, tuple1, tuple2, msg=None):
1117        """A tuple-specific equality assertion.
1118
1119        Args:
1120            tuple1: The first tuple to compare.
1121            tuple2: The second tuple to compare.
1122            msg: Optional message to use on failure instead of a list of
1123                    differences.
1124        """
1125        self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
1126
1127    def assertSetEqual(self, set1, set2, msg=None):
1128        """A set-specific equality assertion.
1129
1130        Args:
1131            set1: The first set to compare.
1132            set2: The second set to compare.
1133            msg: Optional message to use on failure instead of a list of
1134                    differences.
1135
1136        assertSetEqual uses ducktyping to support different types of sets, and
1137        is optimized for sets specifically (parameters must support a
1138        difference method).
1139        """
1140        try:
1141            difference1 = set1.difference(set2)
1142        except TypeError as e:
1143            self.fail('invalid type when attempting set difference: %s' % e)
1144        except AttributeError as e:
1145            self.fail('first argument does not support set difference: %s' % e)
1146
1147        try:
1148            difference2 = set2.difference(set1)
1149        except TypeError as e:
1150            self.fail('invalid type when attempting set difference: %s' % e)
1151        except AttributeError as e:
1152            self.fail('second argument does not support set difference: %s' % e)
1153
1154        if not (difference1 or difference2):
1155            return
1156
1157        lines = []
1158        if difference1:
1159            lines.append('Items in the first set but not the second:')
1160            for item in difference1:
1161                lines.append(repr(item))
1162        if difference2:
1163            lines.append('Items in the second set but not the first:')
1164            for item in difference2:
1165                lines.append(repr(item))
1166
1167        standardMsg = '\n'.join(lines)
1168        self.fail(self._formatMessage(msg, standardMsg))
1169
1170    def assertIn(self, member, container, msg=None):
1171        """Just like self.assertTrue(a in b), but with a nicer default message."""
1172        if member not in container:
1173            standardMsg = '%s not found in %s' % (safe_repr(member),
1174                                                  safe_repr(container))
1175            self.fail(self._formatMessage(msg, standardMsg))
1176
1177    def assertNotIn(self, member, container, msg=None):
1178        """Just like self.assertTrue(a not in b), but with a nicer default message."""
1179        if member in container:
1180            standardMsg = '%s unexpectedly found in %s' % (safe_repr(member),
1181                                                        safe_repr(container))
1182            self.fail(self._formatMessage(msg, standardMsg))
1183
1184    def assertIs(self, expr1, expr2, msg=None):
1185        """Just like self.assertTrue(a is b), but with a nicer default message."""
1186        if expr1 is not expr2:
1187            standardMsg = '%s is not %s' % (safe_repr(expr1),
1188                                             safe_repr(expr2))
1189            self.fail(self._formatMessage(msg, standardMsg))
1190
1191    def assertIsNot(self, expr1, expr2, msg=None):
1192        """Just like self.assertTrue(a is not b), but with a nicer default message."""
1193        if expr1 is expr2:
1194            standardMsg = 'unexpectedly identical: %s' % (safe_repr(expr1),)
1195            self.fail(self._formatMessage(msg, standardMsg))
1196
1197    def assertDictEqual(self, d1, d2, msg=None):
1198        self.assertIsInstance(d1, dict, 'First argument is not a dictionary')
1199        self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')
1200
1201        if d1 != d2:
1202            standardMsg = '%s != %s' % _common_shorten_repr(d1, d2)
1203            diff = ('\n' + '\n'.join(difflib.ndiff(
1204                           pprint.pformat(d1).splitlines(),
1205                           pprint.pformat(d2).splitlines())))
1206            standardMsg = self._truncateMessage(standardMsg, diff)
1207            self.fail(self._formatMessage(msg, standardMsg))
1208
1209    def assertCountEqual(self, first, second, msg=None):
1210        """Asserts that two iterables have the same elements, the same number of
1211        times, without regard to order.
1212
1213            self.assertEqual(Counter(list(first)),
1214                             Counter(list(second)))
1215
1216         Example:
1217            - [0, 1, 1] and [1, 0, 1] compare equal.
1218            - [0, 0, 1] and [0, 1] compare unequal.
1219
1220        """
1221        first_seq, second_seq = list(first), list(second)
1222        try:
1223            first = collections.Counter(first_seq)
1224            second = collections.Counter(second_seq)
1225        except TypeError:
1226            # Handle case with unhashable elements
1227            differences = _count_diff_all_purpose(first_seq, second_seq)
1228        else:
1229            if first == second:
1230                return
1231            differences = _count_diff_hashable(first_seq, second_seq)
1232
1233        if differences:
1234            standardMsg = 'Element counts were not equal:\n'
1235            lines = ['First has %d, Second has %d:  %r' % diff for diff in differences]
1236            diffMsg = '\n'.join(lines)
1237            standardMsg = self._truncateMessage(standardMsg, diffMsg)
1238            msg = self._formatMessage(msg, standardMsg)
1239            self.fail(msg)
1240
1241    def assertMultiLineEqual(self, first, second, msg=None):
1242        """Assert that two multi-line strings are equal."""
1243        self.assertIsInstance(first, str, "First argument is not a string")
1244        self.assertIsInstance(second, str, "Second argument is not a string")
1245
1246        if first != second:
1247            # Don't use difflib if the strings are too long
1248            if (len(first) > self._diffThreshold or
1249                len(second) > self._diffThreshold):
1250                self._baseAssertEqual(first, second, msg)
1251
1252            # Append \n to both strings if either is missing the \n.
1253            # This allows the final ndiff to show the \n difference. The
1254            # exception here is if the string is empty, in which case no
1255            # \n should be added
1256            first_presplit = first
1257            second_presplit = second
1258            if first and second:
1259                if first[-1] != '\n' or second[-1] != '\n':
1260                    first_presplit += '\n'
1261                    second_presplit += '\n'
1262            elif second and second[-1] != '\n':
1263                second_presplit += '\n'
1264            elif first and first[-1] != '\n':
1265                first_presplit += '\n'
1266
1267            firstlines = first_presplit.splitlines(keepends=True)
1268            secondlines = second_presplit.splitlines(keepends=True)
1269
1270            # Generate the message and diff, then raise the exception
1271            standardMsg = '%s != %s' % _common_shorten_repr(first, second)
1272            diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
1273            standardMsg = self._truncateMessage(standardMsg, diff)
1274            self.fail(self._formatMessage(msg, standardMsg))
1275
1276    def assertLess(self, a, b, msg=None):
1277        """Just like self.assertTrue(a < b), but with a nicer default message."""
1278        if not a < b:
1279            standardMsg = '%s not less than %s' % (safe_repr(a), safe_repr(b))
1280            self.fail(self._formatMessage(msg, standardMsg))
1281
1282    def assertLessEqual(self, a, b, msg=None):
1283        """Just like self.assertTrue(a <= b), but with a nicer default message."""
1284        if not a <= b:
1285            standardMsg = '%s not less than or equal to %s' % (safe_repr(a), safe_repr(b))
1286            self.fail(self._formatMessage(msg, standardMsg))
1287
1288    def assertGreater(self, a, b, msg=None):
1289        """Just like self.assertTrue(a > b), but with a nicer default message."""
1290        if not a > b:
1291            standardMsg = '%s not greater than %s' % (safe_repr(a), safe_repr(b))
1292            self.fail(self._formatMessage(msg, standardMsg))
1293
1294    def assertGreaterEqual(self, a, b, msg=None):
1295        """Just like self.assertTrue(a >= b), but with a nicer default message."""
1296        if not a >= b:
1297            standardMsg = '%s not greater than or equal to %s' % (safe_repr(a), safe_repr(b))
1298            self.fail(self._formatMessage(msg, standardMsg))
1299
1300    def assertIsNone(self, obj, msg=None):
1301        """Same as self.assertTrue(obj is None), with a nicer default message."""
1302        if obj is not None:
1303            standardMsg = '%s is not None' % (safe_repr(obj),)
1304            self.fail(self._formatMessage(msg, standardMsg))
1305
1306    def assertIsNotNone(self, obj, msg=None):
1307        """Included for symmetry with assertIsNone."""
1308        if obj is None:
1309            standardMsg = 'unexpectedly None'
1310            self.fail(self._formatMessage(msg, standardMsg))
1311
1312    def assertIsInstance(self, obj, cls, msg=None):
1313        """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
1314        default message."""
1315        if not isinstance(obj, cls):
1316            standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls)
1317            self.fail(self._formatMessage(msg, standardMsg))
1318
1319    def assertNotIsInstance(self, obj, cls, msg=None):
1320        """Included for symmetry with assertIsInstance."""
1321        if isinstance(obj, cls):
1322            standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls)
1323            self.fail(self._formatMessage(msg, standardMsg))
1324
1325    def assertRaisesRegex(self, expected_exception, expected_regex,
1326                          *args, **kwargs):
1327        """Asserts that the message in a raised exception matches a regex.
1328
1329        Args:
1330            expected_exception: Exception class expected to be raised.
1331            expected_regex: Regex (re.Pattern object or string) expected
1332                    to be found in error message.
1333            args: Function to be called and extra positional args.
1334            kwargs: Extra kwargs.
1335            msg: Optional message used in case of failure. Can only be used
1336                    when assertRaisesRegex is used as a context manager.
1337        """
1338        context = _AssertRaisesContext(expected_exception, self, expected_regex)
1339        return context.handle('assertRaisesRegex', args, kwargs)
1340
1341    def assertWarnsRegex(self, expected_warning, expected_regex,
1342                         *args, **kwargs):
1343        """Asserts that the message in a triggered warning matches a regexp.
1344        Basic functioning is similar to assertWarns() with the addition
1345        that only warnings whose messages also match the regular expression
1346        are considered successful matches.
1347
1348        Args:
1349            expected_warning: Warning class expected to be triggered.
1350            expected_regex: Regex (re.Pattern object or string) expected
1351                    to be found in error message.
1352            args: Function to be called and extra positional args.
1353            kwargs: Extra kwargs.
1354            msg: Optional message used in case of failure. Can only be used
1355                    when assertWarnsRegex is used as a context manager.
1356        """
1357        context = _AssertWarnsContext(expected_warning, self, expected_regex)
1358        return context.handle('assertWarnsRegex', args, kwargs)
1359
1360    def assertRegex(self, text, expected_regex, msg=None):
1361        """Fail the test unless the text matches the regular expression."""
1362        if isinstance(expected_regex, (str, bytes)):
1363            assert expected_regex, "expected_regex must not be empty."
1364            expected_regex = re.compile(expected_regex)
1365        if not expected_regex.search(text):
1366            standardMsg = "Regex didn't match: %r not found in %r" % (
1367                expected_regex.pattern, text)
1368            # _formatMessage ensures the longMessage option is respected
1369            msg = self._formatMessage(msg, standardMsg)
1370            raise self.failureException(msg)
1371
1372    def assertNotRegex(self, text, unexpected_regex, msg=None):
1373        """Fail the test if the text matches the regular expression."""
1374        if isinstance(unexpected_regex, (str, bytes)):
1375            unexpected_regex = re.compile(unexpected_regex)
1376        match = unexpected_regex.search(text)
1377        if match:
1378            standardMsg = 'Regex matched: %r matches %r in %r' % (
1379                text[match.start() : match.end()],
1380                unexpected_regex.pattern,
1381                text)
1382            # _formatMessage ensures the longMessage option is respected
1383            msg = self._formatMessage(msg, standardMsg)
1384            raise self.failureException(msg)

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes:

  • failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
  • longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
  • maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
TestCase(methodName='runTest')
412    def __init__(self, methodName='runTest'):
413        """Create an instance of the class that will use the named test
414           method when executed. Raises a ValueError if the instance does
415           not have a method with the specified name.
416        """
417        self._testMethodName = methodName
418        self._outcome = None
419        self._testMethodDoc = 'No test'
420        try:
421            testMethod = getattr(self, methodName)
422        except AttributeError:
423            if methodName != 'runTest':
424                # we allow instantiation with no explicit method name
425                # but not an *incorrect* or missing method name
426                raise ValueError("no such test method in %s: %s" %
427                      (self.__class__, methodName))
428        else:
429            self._testMethodDoc = testMethod.__doc__
430        self._cleanups = []
431        self._subtest = None
432
433        # Map types to custom assertEqual functions that will compare
434        # instances of said type in more detail to generate a more useful
435        # error message.
436        self._type_equality_funcs = {}
437        self.addTypeEqualityFunc(dict, 'assertDictEqual')
438        self.addTypeEqualityFunc(list, 'assertListEqual')
439        self.addTypeEqualityFunc(tuple, 'assertTupleEqual')
440        self.addTypeEqualityFunc(set, 'assertSetEqual')
441        self.addTypeEqualityFunc(frozenset, 'assertSetEqual')
442        self.addTypeEqualityFunc(str, 'assertMultiLineEqual')

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

failureException = <class 'AssertionError'>
longMessage = True
maxDiff = 640
def addTypeEqualityFunc(self, typeobj, function):
444    def addTypeEqualityFunc(self, typeobj, function):
445        """Add a type specific assertEqual style function to compare a type.
446
447        This method is for use by TestCase subclasses that need to register
448        their own type equality functions to provide nicer error messages.
449
450        Args:
451            typeobj: The data type to call this function on when both values
452                    are of the same type in assertEqual().
453            function: The callable taking two arguments and an optional
454                    msg= argument that raises self.failureException with a
455                    useful error message when the two arguments are not equal.
456        """
457        self._type_equality_funcs[typeobj] = function

Add a type specific assertEqual style function to compare a type.

This method is for use by TestCase subclasses that need to register their own type equality functions to provide nicer error messages.

Arguments:
  • typeobj: The data type to call this function on when both values are of the same type in assertEqual().
  • function: The callable taking two arguments and an optional msg= argument that raises self.failureException with a useful error message when the two arguments are not equal.
def addCleanup(self, function, /, *args, **kwargs):
459    def addCleanup(self, function, /, *args, **kwargs):
460        """Add a function, with arguments, to be called when the test is
461        completed. Functions added are called on a LIFO basis and are
462        called after tearDown on test failure or success.
463
464        Cleanup items are called even if setUp fails (unlike tearDown)."""
465        self._cleanups.append((function, args, kwargs))

Add a function, with arguments, to be called when the test is completed. Functions added are called on a LIFO basis and are called after tearDown on test failure or success.

Cleanup items are called even if setUp fails (unlike tearDown).

def enterContext(self, cm):
467    def enterContext(self, cm):
468        """Enters the supplied context manager.
469
470        If successful, also adds its __exit__ method as a cleanup
471        function and returns the result of the __enter__ method.
472        """
473        return _enter_context(cm, self.addCleanup)

Enters the supplied context manager.

If successful, also adds its __exit__ method as a cleanup function and returns the result of the __enter__ method.

@classmethod
def addClassCleanup(cls, function, /, *args, **kwargs):
475    @classmethod
476    def addClassCleanup(cls, function, /, *args, **kwargs):
477        """Same as addCleanup, except the cleanup items are called even if
478        setUpClass fails (unlike tearDownClass)."""
479        cls._class_cleanups.append((function, args, kwargs))

Same as addCleanup, except the cleanup items are called even if setUpClass fails (unlike tearDownClass).

@classmethod
def enterClassContext(cls, cm):
481    @classmethod
482    def enterClassContext(cls, cm):
483        """Same as enterContext, but class-wide."""
484        return _enter_context(cm, cls.addClassCleanup)

Same as enterContext, but class-wide.

def setUp(self):
486    def setUp(self):
487        "Hook method for setting up the test fixture before exercising it."
488        pass

Hook method for setting up the test fixture before exercising it.

def tearDown(self):
490    def tearDown(self):
491        "Hook method for deconstructing the test fixture after testing it."
492        pass

Hook method for deconstructing the test fixture after testing it.

@classmethod
def setUpClass(cls):
494    @classmethod
495    def setUpClass(cls):
496        "Hook method for setting up class fixture before running tests in the class."

Hook method for setting up class fixture before running tests in the class.

@classmethod
def tearDownClass(cls):
498    @classmethod
499    def tearDownClass(cls):
500        "Hook method for deconstructing the class fixture after running all tests in the class."

Hook method for deconstructing the class fixture after running all tests in the class.

def countTestCases(self):
502    def countTestCases(self):
503        return 1
def defaultTestResult(self):
505    def defaultTestResult(self):
506        return result.TestResult()
def shortDescription(self):
508    def shortDescription(self):
509        """Returns a one-line description of the test, or None if no
510        description has been provided.
511
512        The default implementation of this method returns the first line of
513        the specified test method's docstring.
514        """
515        doc = self._testMethodDoc
516        return doc.strip().split("\n")[0].strip() if doc else None

Returns a one-line description of the test, or None if no description has been provided.

The default implementation of this method returns the first line of the specified test method's docstring.

def id(self):
519    def id(self):
520        return "%s.%s" % (strclass(self.__class__), self._testMethodName)
@contextlib.contextmanager
def subTest(self, msg=<object object>, **params):
538    @contextlib.contextmanager
539    def subTest(self, msg=_subtest_msg_sentinel, **params):
540        """Return a context manager that will return the enclosed block
541        of code in a subtest identified by the optional message and
542        keyword parameters.  A failure in the subtest marks the test
543        case as failed but resumes execution at the end of the enclosed
544        block, allowing further test code to be executed.
545        """
546        if self._outcome is None or not self._outcome.result_supports_subtests:
547            yield
548            return
549        parent = self._subtest
550        if parent is None:
551            params_map = _OrderedChainMap(params)
552        else:
553            params_map = parent.params.new_child(params)
554        self._subtest = _SubTest(self, msg, params_map)
555        try:
556            with self._outcome.testPartExecutor(self._subtest, subTest=True):
557                yield
558            if not self._outcome.success:
559                result = self._outcome.result
560                if result is not None and result.failfast:
561                    raise _ShouldStop
562            elif self._outcome.expectedFailure:
563                # If the test is expecting a failure, we really want to
564                # stop now and register the expected failure.
565                raise _ShouldStop
566        finally:
567            self._subtest = parent

Return a context manager that will return the enclosed block of code in a subtest identified by the optional message and keyword parameters. A failure in the subtest marks the test case as failed but resumes execution at the end of the enclosed block, allowing further test code to be executed.

def run(self, result=None):
617    def run(self, result=None):
618        if result is None:
619            result = self.defaultTestResult()
620            startTestRun = getattr(result, 'startTestRun', None)
621            stopTestRun = getattr(result, 'stopTestRun', None)
622            if startTestRun is not None:
623                startTestRun()
624        else:
625            stopTestRun = None
626
627        result.startTest(self)
628        try:
629            testMethod = getattr(self, self._testMethodName)
630            if (getattr(self.__class__, "__unittest_skip__", False) or
631                getattr(testMethod, "__unittest_skip__", False)):
632                # If the class or method was skipped.
633                skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
634                            or getattr(testMethod, '__unittest_skip_why__', ''))
635                _addSkip(result, self, skip_why)
636                return result
637
638            expecting_failure = (
639                getattr(self, "__unittest_expecting_failure__", False) or
640                getattr(testMethod, "__unittest_expecting_failure__", False)
641            )
642            outcome = _Outcome(result)
643            start_time = time.perf_counter()
644            try:
645                self._outcome = outcome
646
647                with outcome.testPartExecutor(self):
648                    self._callSetUp()
649                if outcome.success:
650                    outcome.expecting_failure = expecting_failure
651                    with outcome.testPartExecutor(self):
652                        self._callTestMethod(testMethod)
653                    outcome.expecting_failure = False
654                    with outcome.testPartExecutor(self):
655                        self._callTearDown()
656                self.doCleanups()
657                self._addDuration(result, (time.perf_counter() - start_time))
658
659                if outcome.success:
660                    if expecting_failure:
661                        if outcome.expectedFailure:
662                            self._addExpectedFailure(result, outcome.expectedFailure)
663                        else:
664                            self._addUnexpectedSuccess(result)
665                    else:
666                        result.addSuccess(self)
667                return result
668            finally:
669                # explicitly break reference cycle:
670                # outcome.expectedFailure -> frame -> outcome -> outcome.expectedFailure
671                outcome.expectedFailure = None
672                outcome = None
673
674                # clear the outcome, no more needed
675                self._outcome = None
676
677        finally:
678            result.stopTest(self)
679            if stopTestRun is not None:
680                stopTestRun()
def doCleanups(self):
682    def doCleanups(self):
683        """Execute all cleanup functions. Normally called for you after
684        tearDown."""
685        outcome = self._outcome or _Outcome()
686        while self._cleanups:
687            function, args, kwargs = self._cleanups.pop()
688            with outcome.testPartExecutor(self):
689                self._callCleanup(function, *args, **kwargs)
690
691        # return this for backwards compatibility
692        # even though we no longer use it internally
693        return outcome.success

Execute all cleanup functions. Normally called for you after tearDown.

@classmethod
def doClassCleanups(cls):
695    @classmethod
696    def doClassCleanups(cls):
697        """Execute all class cleanup functions. Normally called for you after
698        tearDownClass."""
699        cls.tearDown_exceptions = []
700        while cls._class_cleanups:
701            function, args, kwargs = cls._class_cleanups.pop()
702            try:
703                function(*args, **kwargs)
704            except Exception:
705                cls.tearDown_exceptions.append(sys.exc_info())

Execute all class cleanup functions. Normally called for you after tearDownClass.

def debug(self):
710    def debug(self):
711        """Run the test without collecting errors in a TestResult"""
712        testMethod = getattr(self, self._testMethodName)
713        if (getattr(self.__class__, "__unittest_skip__", False) or
714            getattr(testMethod, "__unittest_skip__", False)):
715            # If the class or method was skipped.
716            skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
717                        or getattr(testMethod, '__unittest_skip_why__', ''))
718            raise SkipTest(skip_why)
719
720        self._callSetUp()
721        self._callTestMethod(testMethod)
722        self._callTearDown()
723        while self._cleanups:
724            function, args, kwargs = self._cleanups.pop()
725            self._callCleanup(function, *args, **kwargs)

Run the test without collecting errors in a TestResult

def skipTest(self, reason):
727    def skipTest(self, reason):
728        """Skip this test."""
729        raise SkipTest(reason)

Skip this test.

def fail(self, msg=None):
731    def fail(self, msg=None):
732        """Fail immediately, with the given message."""
733        raise self.failureException(msg)

Fail immediately, with the given message.

def assertFalse(self, expr, msg=None):
735    def assertFalse(self, expr, msg=None):
736        """Check that the expression is false."""
737        if expr:
738            msg = self._formatMessage(msg, "%s is not false" % safe_repr(expr))
739            raise self.failureException(msg)

Check that the expression is false.

def assertTrue(self, expr, msg=None):
741    def assertTrue(self, expr, msg=None):
742        """Check that the expression is true."""
743        if not expr:
744            msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
745            raise self.failureException(msg)

Check that the expression is true.

def assertRaises(self, expected_exception, *args, **kwargs):
768    def assertRaises(self, expected_exception, *args, **kwargs):
769        """Fail unless an exception of class expected_exception is raised
770           by the callable when invoked with specified positional and
771           keyword arguments. If a different type of exception is
772           raised, it will not be caught, and the test case will be
773           deemed to have suffered an error, exactly as for an
774           unexpected exception.
775
776           If called with the callable and arguments omitted, will return a
777           context object used like this::
778
779                with self.assertRaises(SomeException):
780                    do_something()
781
782           An optional keyword argument 'msg' can be provided when assertRaises
783           is used as a context object.
784
785           The context manager keeps a reference to the exception as
786           the 'exception' attribute. This allows you to inspect the
787           exception after the assertion::
788
789               with self.assertRaises(SomeException) as cm:
790                   do_something()
791               the_exception = cm.exception
792               self.assertEqual(the_exception.error_code, 3)
793        """
794        context = _AssertRaisesContext(expected_exception, self)
795        try:
796            return context.handle('assertRaises', args, kwargs)
797        finally:
798            # bpo-23890: manually break a reference cycle
799            context = None

Fail unless an exception of class expected_exception is raised by the callable when invoked with specified positional and keyword arguments. If a different type of exception is raised, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

If called with the callable and arguments omitted, will return a context object used like this::

 with self.assertRaises(SomeException):
     do_something()

An optional keyword argument 'msg' can be provided when assertRaises is used as a context object.

The context manager keeps a reference to the exception as the 'exception' attribute. This allows you to inspect the exception after the assertion::

with self.assertRaises(SomeException) as cm:
    do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)
def assertWarns(self, expected_warning, *args, **kwargs):
801    def assertWarns(self, expected_warning, *args, **kwargs):
802        """Fail unless a warning of class warnClass is triggered
803           by the callable when invoked with specified positional and
804           keyword arguments.  If a different type of warning is
805           triggered, it will not be handled: depending on the other
806           warning filtering rules in effect, it might be silenced, printed
807           out, or raised as an exception.
808
809           If called with the callable and arguments omitted, will return a
810           context object used like this::
811
812                with self.assertWarns(SomeWarning):
813                    do_something()
814
815           An optional keyword argument 'msg' can be provided when assertWarns
816           is used as a context object.
817
818           The context manager keeps a reference to the first matching
819           warning as the 'warning' attribute; similarly, the 'filename'
820           and 'lineno' attributes give you information about the line
821           of Python code from which the warning was triggered.
822           This allows you to inspect the warning after the assertion::
823
824               with self.assertWarns(SomeWarning) as cm:
825                   do_something()
826               the_warning = cm.warning
827               self.assertEqual(the_warning.some_attribute, 147)
828        """
829        context = _AssertWarnsContext(expected_warning, self)
830        return context.handle('assertWarns', args, kwargs)

Fail unless a warning of class warnClass is triggered by the callable when invoked with specified positional and keyword arguments. If a different type of warning is triggered, it will not be handled: depending on the other warning filtering rules in effect, it might be silenced, printed out, or raised as an exception.

If called with the callable and arguments omitted, will return a context object used like this::

 with self.assertWarns(SomeWarning):
     do_something()

An optional keyword argument 'msg' can be provided when assertWarns is used as a context object.

The context manager keeps a reference to the first matching warning as the 'warning' attribute; similarly, the 'filename' and 'lineno' attributes give you information about the line of Python code from which the warning was triggered. This allows you to inspect the warning after the assertion::

with self.assertWarns(SomeWarning) as cm:
    do_something()
the_warning = cm.warning
self.assertEqual(the_warning.some_attribute, 147)
def assertLogs(self, logger=None, level=None):
837    def assertLogs(self, logger=None, level=None):
838        """Fail unless a log message of level *level* or higher is emitted
839        on *logger_name* or its children.  If omitted, *level* defaults to
840        INFO and *logger* defaults to the root logger.
841
842        This method must be used as a context manager, and will yield
843        a recording object with two attributes: `output` and `records`.
844        At the end of the context manager, the `output` attribute will
845        be a list of the matching formatted log messages and the
846        `records` attribute will be a list of the corresponding LogRecord
847        objects.
848
849        Example::
850
851            with self.assertLogs('foo', level='INFO') as cm:
852                logging.getLogger('foo').info('first message')
853                logging.getLogger('foo.bar').error('second message')
854            self.assertEqual(cm.output, ['INFO:foo:first message',
855                                         'ERROR:foo.bar:second message'])
856        """
857        # Lazy import to avoid importing logging if it is not needed.
858        from ._log import _AssertLogsContext
859        return _AssertLogsContext(self, logger, level, no_logs=False)

Fail unless a log message of level level or higher is emitted on logger_name or its children. If omitted, level defaults to INFO and logger defaults to the root logger.

This method must be used as a context manager, and will yield a recording object with two attributes: output and records. At the end of the context manager, the output attribute will be a list of the matching formatted log messages and the records attribute will be a list of the corresponding LogRecord objects.

Example::

with self.assertLogs('foo', level='INFO') as cm:
    logging.getLogger('foo').info('first message')
    logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
                             'ERROR:foo.bar:second message'])
def assertNoLogs(self, logger=None, level=None):
861    def assertNoLogs(self, logger=None, level=None):
862        """ Fail unless no log messages of level *level* or higher are emitted
863        on *logger_name* or its children.
864
865        This method must be used as a context manager.
866        """
867        from ._log import _AssertLogsContext
868        return _AssertLogsContext(self, logger, level, no_logs=True)

Fail unless no log messages of level level or higher are emitted on logger_name or its children.

This method must be used as a context manager.

def assertEqual(self, first, second, msg=None):
903    def assertEqual(self, first, second, msg=None):
904        """Fail if the two objects are unequal as determined by the '=='
905           operator.
906        """
907        assertion_func = self._getAssertEqualityFunc(first, second)
908        assertion_func(first, second, msg=msg)

Fail if the two objects are unequal as determined by the '==' operator.

def assertNotEqual(self, first, second, msg=None):
910    def assertNotEqual(self, first, second, msg=None):
911        """Fail if the two objects are equal as determined by the '!='
912           operator.
913        """
914        if not first != second:
915            msg = self._formatMessage(msg, '%s == %s' % (safe_repr(first),
916                                                          safe_repr(second)))
917            raise self.failureException(msg)

Fail if the two objects are equal as determined by the '!=' operator.

def assertAlmostEqual(self, first, second, places=None, msg=None, delta=None):
919    def assertAlmostEqual(self, first, second, places=None, msg=None,
920                          delta=None):
921        """Fail if the two objects are unequal as determined by their
922           difference rounded to the given number of decimal places
923           (default 7) and comparing to zero, or by comparing that the
924           difference between the two objects is more than the given
925           delta.
926
927           Note that decimal places (from zero) are usually not the same
928           as significant digits (measured from the most significant digit).
929
930           If the two objects compare equal then they will automatically
931           compare almost equal.
932        """
933        if first == second:
934            # shortcut
935            return
936        if delta is not None and places is not None:
937            raise TypeError("specify delta or places not both")
938
939        diff = abs(first - second)
940        if delta is not None:
941            if diff <= delta:
942                return
943
944            standardMsg = '%s != %s within %s delta (%s difference)' % (
945                safe_repr(first),
946                safe_repr(second),
947                safe_repr(delta),
948                safe_repr(diff))
949        else:
950            if places is None:
951                places = 7
952
953            if round(diff, places) == 0:
954                return
955
956            standardMsg = '%s != %s within %r places (%s difference)' % (
957                safe_repr(first),
958                safe_repr(second),
959                places,
960                safe_repr(diff))
961        msg = self._formatMessage(msg, standardMsg)
962        raise self.failureException(msg)

Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is more than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

If the two objects compare equal then they will automatically compare almost equal.

def assertNotAlmostEqual(self, first, second, places=None, msg=None, delta=None):
964    def assertNotAlmostEqual(self, first, second, places=None, msg=None,
965                             delta=None):
966        """Fail if the two objects are equal as determined by their
967           difference rounded to the given number of decimal places
968           (default 7) and comparing to zero, or by comparing that the
969           difference between the two objects is less than the given delta.
970
971           Note that decimal places (from zero) are usually not the same
972           as significant digits (measured from the most significant digit).
973
974           Objects that are equal automatically fail.
975        """
976        if delta is not None and places is not None:
977            raise TypeError("specify delta or places not both")
978        diff = abs(first - second)
979        if delta is not None:
980            if not (first == second) and diff > delta:
981                return
982            standardMsg = '%s == %s within %s delta (%s difference)' % (
983                safe_repr(first),
984                safe_repr(second),
985                safe_repr(delta),
986                safe_repr(diff))
987        else:
988            if places is None:
989                places = 7
990            if not (first == second) and round(diff, places) != 0:
991                return
992            standardMsg = '%s == %s within %r places' % (safe_repr(first),
993                                                         safe_repr(second),
994                                                         places)
995
996        msg = self._formatMessage(msg, standardMsg)
997        raise self.failureException(msg)

Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is less than the given delta.

Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).

Objects that are equal automatically fail.

def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
 999    def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):
1000        """An equality assertion for ordered sequences (like lists and tuples).
1001
1002        For the purposes of this function, a valid ordered sequence type is one
1003        which can be indexed, has a length, and has an equality operator.
1004
1005        Args:
1006            seq1: The first sequence to compare.
1007            seq2: The second sequence to compare.
1008            seq_type: The expected datatype of the sequences, or None if no
1009                    datatype should be enforced.
1010            msg: Optional message to use on failure instead of a list of
1011                    differences.
1012        """
1013        if seq_type is not None:
1014            seq_type_name = seq_type.__name__
1015            if not isinstance(seq1, seq_type):
1016                raise self.failureException('First sequence is not a %s: %s'
1017                                        % (seq_type_name, safe_repr(seq1)))
1018            if not isinstance(seq2, seq_type):
1019                raise self.failureException('Second sequence is not a %s: %s'
1020                                        % (seq_type_name, safe_repr(seq2)))
1021        else:
1022            seq_type_name = "sequence"
1023
1024        differing = None
1025        try:
1026            len1 = len(seq1)
1027        except (TypeError, NotImplementedError):
1028            differing = 'First %s has no length.    Non-sequence?' % (
1029                    seq_type_name)
1030
1031        if differing is None:
1032            try:
1033                len2 = len(seq2)
1034            except (TypeError, NotImplementedError):
1035                differing = 'Second %s has no length.    Non-sequence?' % (
1036                        seq_type_name)
1037
1038        if differing is None:
1039            if seq1 == seq2:
1040                return
1041
1042            differing = '%ss differ: %s != %s\n' % (
1043                    (seq_type_name.capitalize(),) +
1044                    _common_shorten_repr(seq1, seq2))
1045
1046            for i in range(min(len1, len2)):
1047                try:
1048                    item1 = seq1[i]
1049                except (TypeError, IndexError, NotImplementedError):
1050                    differing += ('\nUnable to index element %d of first %s\n' %
1051                                 (i, seq_type_name))
1052                    break
1053
1054                try:
1055                    item2 = seq2[i]
1056                except (TypeError, IndexError, NotImplementedError):
1057                    differing += ('\nUnable to index element %d of second %s\n' %
1058                                 (i, seq_type_name))
1059                    break
1060
1061                if item1 != item2:
1062                    differing += ('\nFirst differing element %d:\n%s\n%s\n' %
1063                                 ((i,) + _common_shorten_repr(item1, item2)))
1064                    break
1065            else:
1066                if (len1 == len2 and seq_type is None and
1067                    type(seq1) != type(seq2)):
1068                    # The sequences are the same, but have differing types.
1069                    return
1070
1071            if len1 > len2:
1072                differing += ('\nFirst %s contains %d additional '
1073                             'elements.\n' % (seq_type_name, len1 - len2))
1074                try:
1075                    differing += ('First extra element %d:\n%s\n' %
1076                                  (len2, safe_repr(seq1[len2])))
1077                except (TypeError, IndexError, NotImplementedError):
1078                    differing += ('Unable to index element %d '
1079                                  'of first %s\n' % (len2, seq_type_name))
1080            elif len1 < len2:
1081                differing += ('\nSecond %s contains %d additional '
1082                             'elements.\n' % (seq_type_name, len2 - len1))
1083                try:
1084                    differing += ('First extra element %d:\n%s\n' %
1085                                  (len1, safe_repr(seq2[len1])))
1086                except (TypeError, IndexError, NotImplementedError):
1087                    differing += ('Unable to index element %d '
1088                                  'of second %s\n' % (len1, seq_type_name))
1089        standardMsg = differing
1090        diffMsg = '\n' + '\n'.join(
1091            difflib.ndiff(pprint.pformat(seq1).splitlines(),
1092                          pprint.pformat(seq2).splitlines()))
1093
1094        standardMsg = self._truncateMessage(standardMsg, diffMsg)
1095        msg = self._formatMessage(msg, standardMsg)
1096        self.fail(msg)

An equality assertion for ordered sequences (like lists and tuples).

For the purposes of this function, a valid ordered sequence type is one which can be indexed, has a length, and has an equality operator.

Arguments:
  • seq1: The first sequence to compare.
  • seq2: The second sequence to compare.
  • seq_type: The expected datatype of the sequences, or None if no datatype should be enforced.
  • msg: Optional message to use on failure instead of a list of differences.
def assertListEqual(self, list1, list2, msg=None):
1104    def assertListEqual(self, list1, list2, msg=None):
1105        """A list-specific equality assertion.
1106
1107        Args:
1108            list1: The first list to compare.
1109            list2: The second list to compare.
1110            msg: Optional message to use on failure instead of a list of
1111                    differences.
1112
1113        """
1114        self.assertSequenceEqual(list1, list2, msg, seq_type=list)

A list-specific equality assertion.

Arguments:
  • list1: The first list to compare.
  • list2: The second list to compare.
  • msg: Optional message to use on failure instead of a list of differences.
def assertTupleEqual(self, tuple1, tuple2, msg=None):
1116    def assertTupleEqual(self, tuple1, tuple2, msg=None):
1117        """A tuple-specific equality assertion.
1118
1119        Args:
1120            tuple1: The first tuple to compare.
1121            tuple2: The second tuple to compare.
1122            msg: Optional message to use on failure instead of a list of
1123                    differences.
1124        """
1125        self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)

A tuple-specific equality assertion.

Arguments:
  • tuple1: The first tuple to compare.
  • tuple2: The second tuple to compare.
  • msg: Optional message to use on failure instead of a list of differences.
def assertSetEqual(self, set1, set2, msg=None):
1127    def assertSetEqual(self, set1, set2, msg=None):
1128        """A set-specific equality assertion.
1129
1130        Args:
1131            set1: The first set to compare.
1132            set2: The second set to compare.
1133            msg: Optional message to use on failure instead of a list of
1134                    differences.
1135
1136        assertSetEqual uses ducktyping to support different types of sets, and
1137        is optimized for sets specifically (parameters must support a
1138        difference method).
1139        """
1140        try:
1141            difference1 = set1.difference(set2)
1142        except TypeError as e:
1143            self.fail('invalid type when attempting set difference: %s' % e)
1144        except AttributeError as e:
1145            self.fail('first argument does not support set difference: %s' % e)
1146
1147        try:
1148            difference2 = set2.difference(set1)
1149        except TypeError as e:
1150            self.fail('invalid type when attempting set difference: %s' % e)
1151        except AttributeError as e:
1152            self.fail('second argument does not support set difference: %s' % e)
1153
1154        if not (difference1 or difference2):
1155            return
1156
1157        lines = []
1158        if difference1:
1159            lines.append('Items in the first set but not the second:')
1160            for item in difference1:
1161                lines.append(repr(item))
1162        if difference2:
1163            lines.append('Items in the second set but not the first:')
1164            for item in difference2:
1165                lines.append(repr(item))
1166
1167        standardMsg = '\n'.join(lines)
1168        self.fail(self._formatMessage(msg, standardMsg))

A set-specific equality assertion.

Arguments:
  • set1: The first set to compare.
  • set2: The second set to compare.
  • msg: Optional message to use on failure instead of a list of differences.

assertSetEqual uses ducktyping to support different types of sets, and is optimized for sets specifically (parameters must support a difference method).

def assertIn(self, member, container, msg=None):
1170    def assertIn(self, member, container, msg=None):
1171        """Just like self.assertTrue(a in b), but with a nicer default message."""
1172        if member not in container:
1173            standardMsg = '%s not found in %s' % (safe_repr(member),
1174                                                  safe_repr(container))
1175            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a in b), but with a nicer default message.

def assertNotIn(self, member, container, msg=None):
1177    def assertNotIn(self, member, container, msg=None):
1178        """Just like self.assertTrue(a not in b), but with a nicer default message."""
1179        if member in container:
1180            standardMsg = '%s unexpectedly found in %s' % (safe_repr(member),
1181                                                        safe_repr(container))
1182            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a not in b), but with a nicer default message.

def assertIs(self, expr1, expr2, msg=None):
1184    def assertIs(self, expr1, expr2, msg=None):
1185        """Just like self.assertTrue(a is b), but with a nicer default message."""
1186        if expr1 is not expr2:
1187            standardMsg = '%s is not %s' % (safe_repr(expr1),
1188                                             safe_repr(expr2))
1189            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a is b), but with a nicer default message.

def assertIsNot(self, expr1, expr2, msg=None):
1191    def assertIsNot(self, expr1, expr2, msg=None):
1192        """Just like self.assertTrue(a is not b), but with a nicer default message."""
1193        if expr1 is expr2:
1194            standardMsg = 'unexpectedly identical: %s' % (safe_repr(expr1),)
1195            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a is not b), but with a nicer default message.

def assertDictEqual(self, d1, d2, msg=None):
1197    def assertDictEqual(self, d1, d2, msg=None):
1198        self.assertIsInstance(d1, dict, 'First argument is not a dictionary')
1199        self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')
1200
1201        if d1 != d2:
1202            standardMsg = '%s != %s' % _common_shorten_repr(d1, d2)
1203            diff = ('\n' + '\n'.join(difflib.ndiff(
1204                           pprint.pformat(d1).splitlines(),
1205                           pprint.pformat(d2).splitlines())))
1206            standardMsg = self._truncateMessage(standardMsg, diff)
1207            self.fail(self._formatMessage(msg, standardMsg))
def assertCountEqual(self, first, second, msg=None):
1209    def assertCountEqual(self, first, second, msg=None):
1210        """Asserts that two iterables have the same elements, the same number of
1211        times, without regard to order.
1212
1213            self.assertEqual(Counter(list(first)),
1214                             Counter(list(second)))
1215
1216         Example:
1217            - [0, 1, 1] and [1, 0, 1] compare equal.
1218            - [0, 0, 1] and [0, 1] compare unequal.
1219
1220        """
1221        first_seq, second_seq = list(first), list(second)
1222        try:
1223            first = collections.Counter(first_seq)
1224            second = collections.Counter(second_seq)
1225        except TypeError:
1226            # Handle case with unhashable elements
1227            differences = _count_diff_all_purpose(first_seq, second_seq)
1228        else:
1229            if first == second:
1230                return
1231            differences = _count_diff_hashable(first_seq, second_seq)
1232
1233        if differences:
1234            standardMsg = 'Element counts were not equal:\n'
1235            lines = ['First has %d, Second has %d:  %r' % diff for diff in differences]
1236            diffMsg = '\n'.join(lines)
1237            standardMsg = self._truncateMessage(standardMsg, diffMsg)
1238            msg = self._formatMessage(msg, standardMsg)
1239            self.fail(msg)

Asserts that two iterables have the same elements, the same number of times, without regard to order.

self.assertEqual(Counter(list(first)),
                 Counter(list(second)))

Example: - [0, 1, 1] and [1, 0, 1] compare equal. - [0, 0, 1] and [0, 1] compare unequal.

def assertMultiLineEqual(self, first, second, msg=None):
1241    def assertMultiLineEqual(self, first, second, msg=None):
1242        """Assert that two multi-line strings are equal."""
1243        self.assertIsInstance(first, str, "First argument is not a string")
1244        self.assertIsInstance(second, str, "Second argument is not a string")
1245
1246        if first != second:
1247            # Don't use difflib if the strings are too long
1248            if (len(first) > self._diffThreshold or
1249                len(second) > self._diffThreshold):
1250                self._baseAssertEqual(first, second, msg)
1251
1252            # Append \n to both strings if either is missing the \n.
1253            # This allows the final ndiff to show the \n difference. The
1254            # exception here is if the string is empty, in which case no
1255            # \n should be added
1256            first_presplit = first
1257            second_presplit = second
1258            if first and second:
1259                if first[-1] != '\n' or second[-1] != '\n':
1260                    first_presplit += '\n'
1261                    second_presplit += '\n'
1262            elif second and second[-1] != '\n':
1263                second_presplit += '\n'
1264            elif first and first[-1] != '\n':
1265                first_presplit += '\n'
1266
1267            firstlines = first_presplit.splitlines(keepends=True)
1268            secondlines = second_presplit.splitlines(keepends=True)
1269
1270            # Generate the message and diff, then raise the exception
1271            standardMsg = '%s != %s' % _common_shorten_repr(first, second)
1272            diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
1273            standardMsg = self._truncateMessage(standardMsg, diff)
1274            self.fail(self._formatMessage(msg, standardMsg))

Assert that two multi-line strings are equal.

def assertLess(self, a, b, msg=None):
1276    def assertLess(self, a, b, msg=None):
1277        """Just like self.assertTrue(a < b), but with a nicer default message."""
1278        if not a < b:
1279            standardMsg = '%s not less than %s' % (safe_repr(a), safe_repr(b))
1280            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a < b), but with a nicer default message.

def assertLessEqual(self, a, b, msg=None):
1282    def assertLessEqual(self, a, b, msg=None):
1283        """Just like self.assertTrue(a <= b), but with a nicer default message."""
1284        if not a <= b:
1285            standardMsg = '%s not less than or equal to %s' % (safe_repr(a), safe_repr(b))
1286            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a <= b), but with a nicer default message.

def assertGreater(self, a, b, msg=None):
1288    def assertGreater(self, a, b, msg=None):
1289        """Just like self.assertTrue(a > b), but with a nicer default message."""
1290        if not a > b:
1291            standardMsg = '%s not greater than %s' % (safe_repr(a), safe_repr(b))
1292            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a > b), but with a nicer default message.

def assertGreaterEqual(self, a, b, msg=None):
1294    def assertGreaterEqual(self, a, b, msg=None):
1295        """Just like self.assertTrue(a >= b), but with a nicer default message."""
1296        if not a >= b:
1297            standardMsg = '%s not greater than or equal to %s' % (safe_repr(a), safe_repr(b))
1298            self.fail(self._formatMessage(msg, standardMsg))

Just like self.assertTrue(a >= b), but with a nicer default message.

def assertIsNone(self, obj, msg=None):
1300    def assertIsNone(self, obj, msg=None):
1301        """Same as self.assertTrue(obj is None), with a nicer default message."""
1302        if obj is not None:
1303            standardMsg = '%s is not None' % (safe_repr(obj),)
1304            self.fail(self._formatMessage(msg, standardMsg))

Same as self.assertTrue(obj is None), with a nicer default message.

def assertIsNotNone(self, obj, msg=None):
1306    def assertIsNotNone(self, obj, msg=None):
1307        """Included for symmetry with assertIsNone."""
1308        if obj is None:
1309            standardMsg = 'unexpectedly None'
1310            self.fail(self._formatMessage(msg, standardMsg))

Included for symmetry with assertIsNone.

def assertIsInstance(self, obj, cls, msg=None):
1312    def assertIsInstance(self, obj, cls, msg=None):
1313        """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
1314        default message."""
1315        if not isinstance(obj, cls):
1316            standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls)
1317            self.fail(self._formatMessage(msg, standardMsg))

Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.

def assertNotIsInstance(self, obj, cls, msg=None):
1319    def assertNotIsInstance(self, obj, cls, msg=None):
1320        """Included for symmetry with assertIsInstance."""
1321        if isinstance(obj, cls):
1322            standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls)
1323            self.fail(self._formatMessage(msg, standardMsg))

Included for symmetry with assertIsInstance.

def assertRaisesRegex(self, expected_exception, expected_regex, *args, **kwargs):
1325    def assertRaisesRegex(self, expected_exception, expected_regex,
1326                          *args, **kwargs):
1327        """Asserts that the message in a raised exception matches a regex.
1328
1329        Args:
1330            expected_exception: Exception class expected to be raised.
1331            expected_regex: Regex (re.Pattern object or string) expected
1332                    to be found in error message.
1333            args: Function to be called and extra positional args.
1334            kwargs: Extra kwargs.
1335            msg: Optional message used in case of failure. Can only be used
1336                    when assertRaisesRegex is used as a context manager.
1337        """
1338        context = _AssertRaisesContext(expected_exception, self, expected_regex)
1339        return context.handle('assertRaisesRegex', args, kwargs)

Asserts that the message in a raised exception matches a regex.

Arguments:
  • expected_exception: Exception class expected to be raised.
  • expected_regex: Regex (re.Pattern object or string) expected to be found in error message.
  • args: Function to be called and extra positional args.
  • kwargs: Extra kwargs.
  • msg: Optional message used in case of failure. Can only be used when assertRaisesRegex is used as a context manager.
def assertWarnsRegex(self, expected_warning, expected_regex, *args, **kwargs):
1341    def assertWarnsRegex(self, expected_warning, expected_regex,
1342                         *args, **kwargs):
1343        """Asserts that the message in a triggered warning matches a regexp.
1344        Basic functioning is similar to assertWarns() with the addition
1345        that only warnings whose messages also match the regular expression
1346        are considered successful matches.
1347
1348        Args:
1349            expected_warning: Warning class expected to be triggered.
1350            expected_regex: Regex (re.Pattern object or string) expected
1351                    to be found in error message.
1352            args: Function to be called and extra positional args.
1353            kwargs: Extra kwargs.
1354            msg: Optional message used in case of failure. Can only be used
1355                    when assertWarnsRegex is used as a context manager.
1356        """
1357        context = _AssertWarnsContext(expected_warning, self, expected_regex)
1358        return context.handle('assertWarnsRegex', args, kwargs)

Asserts that the message in a triggered warning matches a regexp. Basic functioning is similar to assertWarns() with the addition that only warnings whose messages also match the regular expression are considered successful matches.

Arguments:
  • expected_warning: Warning class expected to be triggered.
  • expected_regex: Regex (re.Pattern object or string) expected to be found in error message.
  • args: Function to be called and extra positional args.
  • kwargs: Extra kwargs.
  • msg: Optional message used in case of failure. Can only be used when assertWarnsRegex is used as a context manager.
def assertRegex(self, text, expected_regex, msg=None):
1360    def assertRegex(self, text, expected_regex, msg=None):
1361        """Fail the test unless the text matches the regular expression."""
1362        if isinstance(expected_regex, (str, bytes)):
1363            assert expected_regex, "expected_regex must not be empty."
1364            expected_regex = re.compile(expected_regex)
1365        if not expected_regex.search(text):
1366            standardMsg = "Regex didn't match: %r not found in %r" % (
1367                expected_regex.pattern, text)
1368            # _formatMessage ensures the longMessage option is respected
1369            msg = self._formatMessage(msg, standardMsg)
1370            raise self.failureException(msg)

Fail the test unless the text matches the regular expression.

def assertNotRegex(self, text, unexpected_regex, msg=None):
1372    def assertNotRegex(self, text, unexpected_regex, msg=None):
1373        """Fail the test if the text matches the regular expression."""
1374        if isinstance(unexpected_regex, (str, bytes)):
1375            unexpected_regex = re.compile(unexpected_regex)
1376        match = unexpected_regex.search(text)
1377        if match:
1378            standardMsg = 'Regex matched: %r matches %r in %r' % (
1379                text[match.start() : match.end()],
1380                unexpected_regex.pattern,
1381                text)
1382            # _formatMessage ensures the longMessage option is respected
1383            msg = self._formatMessage(msg, standardMsg)
1384            raise self.failureException(msg)

Fail the test if the text matches the regular expression.

class IsolatedAsyncioTestCase(unittest.TestCase):
 11class IsolatedAsyncioTestCase(TestCase):
 12    # Names intentionally have a long prefix
 13    # to reduce a chance of clashing with user-defined attributes
 14    # from inherited test case
 15    #
 16    # The class doesn't call loop.run_until_complete(self.setUp()) and family
 17    # but uses a different approach:
 18    # 1. create a long-running task that reads self.setUp()
 19    #    awaitable from queue along with a future
 20    # 2. await the awaitable object passing in and set the result
 21    #    into the future object
 22    # 3. Outer code puts the awaitable and the future object into a queue
 23    #    with waiting for the future
 24    # The trick is necessary because every run_until_complete() call
 25    # creates a new task with embedded ContextVar context.
 26    # To share contextvars between setUp(), test and tearDown() we need to execute
 27    # them inside the same task.
 28
 29    # Note: the test case modifies event loop policy if the policy was not instantiated
 30    # yet, unless loop_factory=asyncio.EventLoop is set.
 31    # asyncio.get_event_loop_policy() creates a default policy on demand but never
 32    # returns None
 33    # I believe this is not an issue in user level tests but python itself for testing
 34    # should reset a policy in every test module
 35    # by calling asyncio.set_event_loop_policy(None) in tearDownModule()
 36    # or set loop_factory=asyncio.EventLoop
 37
 38    loop_factory = None
 39
 40    def __init__(self, methodName='runTest'):
 41        super().__init__(methodName)
 42        self._asyncioRunner = None
 43        self._asyncioTestContext = contextvars.copy_context()
 44
 45    async def asyncSetUp(self):
 46        pass
 47
 48    async def asyncTearDown(self):
 49        pass
 50
 51    def addAsyncCleanup(self, func, /, *args, **kwargs):
 52        # A trivial trampoline to addCleanup()
 53        # the function exists because it has a different semantics
 54        # and signature:
 55        # addCleanup() accepts regular functions
 56        # but addAsyncCleanup() accepts coroutines
 57        #
 58        # We intentionally don't add inspect.iscoroutinefunction() check
 59        # for func argument because there is no way
 60        # to check for async function reliably:
 61        # 1. It can be "async def func()" itself
 62        # 2. Class can implement "async def __call__()" method
 63        # 3. Regular "def func()" that returns awaitable object
 64        self.addCleanup(*(func, *args), **kwargs)
 65
 66    async def enterAsyncContext(self, cm):
 67        """Enters the supplied asynchronous context manager.
 68
 69        If successful, also adds its __aexit__ method as a cleanup
 70        function and returns the result of the __aenter__ method.
 71        """
 72        # We look up the special methods on the type to match the with
 73        # statement.
 74        cls = type(cm)
 75        try:
 76            enter = cls.__aenter__
 77            exit = cls.__aexit__
 78        except AttributeError:
 79            raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
 80                            f"not support the asynchronous context manager protocol"
 81                           ) from None
 82        result = await enter(cm)
 83        self.addAsyncCleanup(exit, cm, None, None, None)
 84        return result
 85
 86    def _callSetUp(self):
 87        # Force loop to be initialized and set as the current loop
 88        # so that setUp functions can use get_event_loop() and get the
 89        # correct loop instance.
 90        self._asyncioRunner.get_loop()
 91        self._asyncioTestContext.run(self.setUp)
 92        self._callAsync(self.asyncSetUp)
 93
 94    def _callTestMethod(self, method):
 95        if self._callMaybeAsync(method) is not None:
 96            warnings.warn(f'It is deprecated to return a value that is not None from a '
 97                          f'test case ({method})', DeprecationWarning, stacklevel=4)
 98
 99    def _callTearDown(self):
100        self._callAsync(self.asyncTearDown)
101        self._asyncioTestContext.run(self.tearDown)
102
103    def _callCleanup(self, function, *args, **kwargs):
104        self._callMaybeAsync(function, *args, **kwargs)
105
106    def _callAsync(self, func, /, *args, **kwargs):
107        assert self._asyncioRunner is not None, 'asyncio runner is not initialized'
108        assert inspect.iscoroutinefunction(func), f'{func!r} is not an async function'
109        return self._asyncioRunner.run(
110            func(*args, **kwargs),
111            context=self._asyncioTestContext
112        )
113
114    def _callMaybeAsync(self, func, /, *args, **kwargs):
115        assert self._asyncioRunner is not None, 'asyncio runner is not initialized'
116        if inspect.iscoroutinefunction(func):
117            return self._asyncioRunner.run(
118                func(*args, **kwargs),
119                context=self._asyncioTestContext,
120            )
121        else:
122            return self._asyncioTestContext.run(func, *args, **kwargs)
123
124    def _setupAsyncioRunner(self):
125        assert self._asyncioRunner is None, 'asyncio runner is already initialized'
126        runner = asyncio.Runner(debug=True, loop_factory=self.loop_factory)
127        self._asyncioRunner = runner
128
129    def _tearDownAsyncioRunner(self):
130        runner = self._asyncioRunner
131        runner.close()
132
133    def run(self, result=None):
134        self._setupAsyncioRunner()
135        try:
136            return super().run(result)
137        finally:
138            self._tearDownAsyncioRunner()
139
140    def debug(self):
141        self._setupAsyncioRunner()
142        super().debug()
143        self._tearDownAsyncioRunner()
144
145    def __del__(self):
146        if self._asyncioRunner is not None:
147            self._tearDownAsyncioRunner()

A class whose instances are single test cases.

By default, the test code itself should be placed in a method named 'runTest'.

If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.

When subclassing TestCase, you can set these attributes:

  • failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
  • longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
  • maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.
IsolatedAsyncioTestCase(methodName='runTest')
40    def __init__(self, methodName='runTest'):
41        super().__init__(methodName)
42        self._asyncioRunner = None
43        self._asyncioTestContext = contextvars.copy_context()

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

loop_factory = None
async def asyncSetUp(self):
45    async def asyncSetUp(self):
46        pass
async def asyncTearDown(self):
48    async def asyncTearDown(self):
49        pass
def addAsyncCleanup(self, func, /, *args, **kwargs):
51    def addAsyncCleanup(self, func, /, *args, **kwargs):
52        # A trivial trampoline to addCleanup()
53        # the function exists because it has a different semantics
54        # and signature:
55        # addCleanup() accepts regular functions
56        # but addAsyncCleanup() accepts coroutines
57        #
58        # We intentionally don't add inspect.iscoroutinefunction() check
59        # for func argument because there is no way
60        # to check for async function reliably:
61        # 1. It can be "async def func()" itself
62        # 2. Class can implement "async def __call__()" method
63        # 3. Regular "def func()" that returns awaitable object
64        self.addCleanup(*(func, *args), **kwargs)
async def enterAsyncContext(self, cm):
66    async def enterAsyncContext(self, cm):
67        """Enters the supplied asynchronous context manager.
68
69        If successful, also adds its __aexit__ method as a cleanup
70        function and returns the result of the __aenter__ method.
71        """
72        # We look up the special methods on the type to match the with
73        # statement.
74        cls = type(cm)
75        try:
76            enter = cls.__aenter__
77            exit = cls.__aexit__
78        except AttributeError:
79            raise TypeError(f"'{cls.__module__}.{cls.__qualname__}' object does "
80                            f"not support the asynchronous context manager protocol"
81                           ) from None
82        result = await enter(cm)
83        self.addAsyncCleanup(exit, cm, None, None, None)
84        return result

Enters the supplied asynchronous context manager.

If successful, also adds its __aexit__ method as a cleanup function and returns the result of the __aenter__ method.

def run(self, result=None):
133    def run(self, result=None):
134        self._setupAsyncioRunner()
135        try:
136            return super().run(result)
137        finally:
138            self._tearDownAsyncioRunner()
def debug(self):
140    def debug(self):
141        self._setupAsyncioRunner()
142        super().debug()
143        self._tearDownAsyncioRunner()

Run the test without collecting errors in a TestResult

class TestSuite(unittest.suite.BaseTestSuite):
 93class TestSuite(BaseTestSuite):
 94    """A test suite is a composite test consisting of a number of TestCases.
 95
 96    For use, create an instance of TestSuite, then add test case instances.
 97    When all tests have been added, the suite can be passed to a test
 98    runner, such as TextTestRunner. It will run the individual test cases
 99    in the order in which they were added, aggregating the results. When
100    subclassing, do not forget to call the base class constructor.
101    """
102
103    def run(self, result, debug=False):
104        topLevel = False
105        if getattr(result, '_testRunEntered', False) is False:
106            result._testRunEntered = topLevel = True
107
108        for index, test in enumerate(self):
109            if result.shouldStop:
110                break
111
112            if _isnotsuite(test):
113                self._tearDownPreviousClass(test, result)
114                self._handleModuleFixture(test, result)
115                self._handleClassSetUp(test, result)
116                result._previousTestClass = test.__class__
117
118                if (getattr(test.__class__, '_classSetupFailed', False) or
119                    getattr(result, '_moduleSetUpFailed', False)):
120                    continue
121
122            if not debug:
123                test(result)
124            else:
125                test.debug()
126
127            if self._cleanup:
128                self._removeTestAtIndex(index)
129
130        if topLevel:
131            self._tearDownPreviousClass(None, result)
132            self._handleModuleTearDown(result)
133            result._testRunEntered = False
134        return result
135
136    def debug(self):
137        """Run the tests without collecting errors in a TestResult"""
138        debug = _DebugResult()
139        self.run(debug, True)
140
141    ################################
142
143    def _handleClassSetUp(self, test, result):
144        previousClass = getattr(result, '_previousTestClass', None)
145        currentClass = test.__class__
146        if currentClass == previousClass:
147            return
148        if result._moduleSetUpFailed:
149            return
150        if getattr(currentClass, "__unittest_skip__", False):
151            return
152
153        failed = False
154        try:
155            currentClass._classSetupFailed = False
156        except TypeError:
157            # test may actually be a function
158            # so its class will be a builtin-type
159            pass
160
161        setUpClass = getattr(currentClass, 'setUpClass', None)
162        doClassCleanups = getattr(currentClass, 'doClassCleanups', None)
163        if setUpClass is not None:
164            _call_if_exists(result, '_setupStdout')
165            try:
166                try:
167                    setUpClass()
168                except Exception as e:
169                    if isinstance(result, _DebugResult):
170                        raise
171                    failed = True
172                    try:
173                        currentClass._classSetupFailed = True
174                    except TypeError:
175                        pass
176                    className = util.strclass(currentClass)
177                    self._createClassOrModuleLevelException(result, e,
178                                                            'setUpClass',
179                                                            className)
180                if failed and doClassCleanups is not None:
181                    doClassCleanups()
182                    for exc_info in currentClass.tearDown_exceptions:
183                        self._createClassOrModuleLevelException(
184                                result, exc_info[1], 'setUpClass', className,
185                                info=exc_info)
186            finally:
187                _call_if_exists(result, '_restoreStdout')
188
189    def _get_previous_module(self, result):
190        previousModule = None
191        previousClass = getattr(result, '_previousTestClass', None)
192        if previousClass is not None:
193            previousModule = previousClass.__module__
194        return previousModule
195
196
197    def _handleModuleFixture(self, test, result):
198        previousModule = self._get_previous_module(result)
199        currentModule = test.__class__.__module__
200        if currentModule == previousModule:
201            return
202
203        self._handleModuleTearDown(result)
204
205
206        result._moduleSetUpFailed = False
207        try:
208            module = sys.modules[currentModule]
209        except KeyError:
210            return
211        setUpModule = getattr(module, 'setUpModule', None)
212        if setUpModule is not None:
213            _call_if_exists(result, '_setupStdout')
214            try:
215                try:
216                    setUpModule()
217                except Exception as e:
218                    if isinstance(result, _DebugResult):
219                        raise
220                    result._moduleSetUpFailed = True
221                    self._createClassOrModuleLevelException(result, e,
222                                                            'setUpModule',
223                                                            currentModule)
224                if result._moduleSetUpFailed:
225                    try:
226                        case.doModuleCleanups()
227                    except Exception as e:
228                        self._createClassOrModuleLevelException(result, e,
229                                                                'setUpModule',
230                                                                currentModule)
231            finally:
232                _call_if_exists(result, '_restoreStdout')
233
234    def _createClassOrModuleLevelException(self, result, exc, method_name,
235                                           parent, info=None):
236        errorName = f'{method_name} ({parent})'
237        self._addClassOrModuleLevelException(result, exc, errorName, info)
238
239    def _addClassOrModuleLevelException(self, result, exception, errorName,
240                                        info=None):
241        error = _ErrorHolder(errorName)
242        addSkip = getattr(result, 'addSkip', None)
243        if addSkip is not None and isinstance(exception, case.SkipTest):
244            addSkip(error, str(exception))
245        else:
246            if not info:
247                result.addError(error, sys.exc_info())
248            else:
249                result.addError(error, info)
250
251    def _handleModuleTearDown(self, result):
252        previousModule = self._get_previous_module(result)
253        if previousModule is None:
254            return
255        if result._moduleSetUpFailed:
256            return
257
258        try:
259            module = sys.modules[previousModule]
260        except KeyError:
261            return
262
263        _call_if_exists(result, '_setupStdout')
264        try:
265            tearDownModule = getattr(module, 'tearDownModule', None)
266            if tearDownModule is not None:
267                try:
268                    tearDownModule()
269                except Exception as e:
270                    if isinstance(result, _DebugResult):
271                        raise
272                    self._createClassOrModuleLevelException(result, e,
273                                                            'tearDownModule',
274                                                            previousModule)
275            try:
276                case.doModuleCleanups()
277            except Exception as e:
278                if isinstance(result, _DebugResult):
279                    raise
280                self._createClassOrModuleLevelException(result, e,
281                                                        'tearDownModule',
282                                                        previousModule)
283        finally:
284            _call_if_exists(result, '_restoreStdout')
285
286    def _tearDownPreviousClass(self, test, result):
287        previousClass = getattr(result, '_previousTestClass', None)
288        currentClass = test.__class__
289        if currentClass == previousClass or previousClass is None:
290            return
291        if getattr(previousClass, '_classSetupFailed', False):
292            return
293        if getattr(result, '_moduleSetUpFailed', False):
294            return
295        if getattr(previousClass, "__unittest_skip__", False):
296            return
297
298        tearDownClass = getattr(previousClass, 'tearDownClass', None)
299        doClassCleanups = getattr(previousClass, 'doClassCleanups', None)
300        if tearDownClass is None and doClassCleanups is None:
301            return
302
303        _call_if_exists(result, '_setupStdout')
304        try:
305            if tearDownClass is not None:
306                try:
307                    tearDownClass()
308                except Exception as e:
309                    if isinstance(result, _DebugResult):
310                        raise
311                    className = util.strclass(previousClass)
312                    self._createClassOrModuleLevelException(result, e,
313                                                            'tearDownClass',
314                                                            className)
315            if doClassCleanups is not None:
316                doClassCleanups()
317                for exc_info in previousClass.tearDown_exceptions:
318                    if isinstance(result, _DebugResult):
319                        raise exc_info[1]
320                    className = util.strclass(previousClass)
321                    self._createClassOrModuleLevelException(result, exc_info[1],
322                                                            'tearDownClass',
323                                                            className,
324                                                            info=exc_info)
325        finally:
326            _call_if_exists(result, '_restoreStdout')

A test suite is a composite test consisting of a number of TestCases.

For use, create an instance of TestSuite, then add test case instances. When all tests have been added, the suite can be passed to a test runner, such as TextTestRunner. It will run the individual test cases in the order in which they were added, aggregating the results. When subclassing, do not forget to call the base class constructor.

def run(self, result, debug=False):
103    def run(self, result, debug=False):
104        topLevel = False
105        if getattr(result, '_testRunEntered', False) is False:
106            result._testRunEntered = topLevel = True
107
108        for index, test in enumerate(self):
109            if result.shouldStop:
110                break
111
112            if _isnotsuite(test):
113                self._tearDownPreviousClass(test, result)
114                self._handleModuleFixture(test, result)
115                self._handleClassSetUp(test, result)
116                result._previousTestClass = test.__class__
117
118                if (getattr(test.__class__, '_classSetupFailed', False) or
119                    getattr(result, '_moduleSetUpFailed', False)):
120                    continue
121
122            if not debug:
123                test(result)
124            else:
125                test.debug()
126
127            if self._cleanup:
128                self._removeTestAtIndex(index)
129
130        if topLevel:
131            self._tearDownPreviousClass(None, result)
132            self._handleModuleTearDown(result)
133            result._testRunEntered = False
134        return result
def debug(self):
136    def debug(self):
137        """Run the tests without collecting errors in a TestResult"""
138        debug = _DebugResult()
139        self.run(debug, True)

Run the tests without collecting errors in a TestResult

Inherited Members
unittest.suite.BaseTestSuite
BaseTestSuite
countTestCases
addTest
addTests
class TextTestRunner:
165class TextTestRunner(object):
166    """A test runner class that displays results in textual form.
167
168    It prints out the names of tests as they are run, errors as they
169    occur, and a summary of the results at the end of the test run.
170    """
171    resultclass = TextTestResult
172
173    def __init__(self, stream=None, descriptions=True, verbosity=1,
174                 failfast=False, buffer=False, resultclass=None, warnings=None,
175                 *, tb_locals=False, durations=None):
176        """Construct a TextTestRunner.
177
178        Subclasses should accept **kwargs to ensure compatibility as the
179        interface changes.
180        """
181        if stream is None:
182            stream = sys.stderr
183        self.stream = _WritelnDecorator(stream)
184        self.descriptions = descriptions
185        self.verbosity = verbosity
186        self.failfast = failfast
187        self.buffer = buffer
188        self.tb_locals = tb_locals
189        self.durations = durations
190        self.warnings = warnings
191        if resultclass is not None:
192            self.resultclass = resultclass
193
194    def _makeResult(self):
195        try:
196            return self.resultclass(self.stream, self.descriptions,
197                                    self.verbosity, durations=self.durations)
198        except TypeError:
199            # didn't accept the durations argument
200            return self.resultclass(self.stream, self.descriptions,
201                                    self.verbosity)
202
203    def _printDurations(self, result):
204        if not result.collectedDurations:
205            return
206        ls = sorted(result.collectedDurations, key=lambda x: x[1],
207                    reverse=True)
208        if self.durations > 0:
209            ls = ls[:self.durations]
210        self.stream.writeln("Slowest test durations")
211        if hasattr(result, 'separator2'):
212            self.stream.writeln(result.separator2)
213        hidden = False
214        for test, elapsed in ls:
215            if self.verbosity < 2 and elapsed < 0.001:
216                hidden = True
217                continue
218            self.stream.writeln("%-10s %s" % ("%.3fs" % elapsed, test))
219        if hidden:
220            self.stream.writeln("\n(durations < 0.001s were hidden; "
221                                "use -v to show these durations)")
222        else:
223            self.stream.writeln("")
224
225    def run(self, test):
226        "Run the given test case or test suite."
227        result = self._makeResult()
228        registerResult(result)
229        result.failfast = self.failfast
230        result.buffer = self.buffer
231        result.tb_locals = self.tb_locals
232        with warnings.catch_warnings():
233            if self.warnings:
234                # if self.warnings is set, use it to filter all the warnings
235                warnings.simplefilter(self.warnings)
236            startTime = time.perf_counter()
237            startTestRun = getattr(result, 'startTestRun', None)
238            if startTestRun is not None:
239                startTestRun()
240            try:
241                test(result)
242            finally:
243                stopTestRun = getattr(result, 'stopTestRun', None)
244                if stopTestRun is not None:
245                    stopTestRun()
246            stopTime = time.perf_counter()
247        timeTaken = stopTime - startTime
248        result.printErrors()
249        if self.durations is not None:
250            self._printDurations(result)
251
252        if hasattr(result, 'separator2'):
253            self.stream.writeln(result.separator2)
254
255        run = result.testsRun
256        self.stream.writeln("Ran %d test%s in %.3fs" %
257                            (run, run != 1 and "s" or "", timeTaken))
258        self.stream.writeln()
259
260        expectedFails = unexpectedSuccesses = skipped = 0
261        try:
262            results = map(len, (result.expectedFailures,
263                                result.unexpectedSuccesses,
264                                result.skipped))
265        except AttributeError:
266            pass
267        else:
268            expectedFails, unexpectedSuccesses, skipped = results
269
270        infos = []
271        if not result.wasSuccessful():
272            self.stream.write("FAILED")
273            failed, errored = len(result.failures), len(result.errors)
274            if failed:
275                infos.append("failures=%d" % failed)
276            if errored:
277                infos.append("errors=%d" % errored)
278        elif run == 0 and not skipped:
279            self.stream.write("NO TESTS RAN")
280        else:
281            self.stream.write("OK")
282        if skipped:
283            infos.append("skipped=%d" % skipped)
284        if expectedFails:
285            infos.append("expected failures=%d" % expectedFails)
286        if unexpectedSuccesses:
287            infos.append("unexpected successes=%d" % unexpectedSuccesses)
288        if infos:
289            self.stream.writeln(" (%s)" % (", ".join(infos),))
290        else:
291            self.stream.write("\n")
292        self.stream.flush()
293        return result

A test runner class that displays results in textual form.

It prints out the names of tests as they are run, errors as they occur, and a summary of the results at the end of the test run.

TextTestRunner( stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, warnings=None, *, tb_locals=False, durations=None)
173    def __init__(self, stream=None, descriptions=True, verbosity=1,
174                 failfast=False, buffer=False, resultclass=None, warnings=None,
175                 *, tb_locals=False, durations=None):
176        """Construct a TextTestRunner.
177
178        Subclasses should accept **kwargs to ensure compatibility as the
179        interface changes.
180        """
181        if stream is None:
182            stream = sys.stderr
183        self.stream = _WritelnDecorator(stream)
184        self.descriptions = descriptions
185        self.verbosity = verbosity
186        self.failfast = failfast
187        self.buffer = buffer
188        self.tb_locals = tb_locals
189        self.durations = durations
190        self.warnings = warnings
191        if resultclass is not None:
192            self.resultclass = resultclass

Construct a TextTestRunner.

Subclasses should accept **kwargs to ensure compatibility as the interface changes.

resultclass = <class 'TextTestResult'>
stream
descriptions
verbosity
failfast
buffer
tb_locals
durations
warnings
def run(self, test):
225    def run(self, test):
226        "Run the given test case or test suite."
227        result = self._makeResult()
228        registerResult(result)
229        result.failfast = self.failfast
230        result.buffer = self.buffer
231        result.tb_locals = self.tb_locals
232        with warnings.catch_warnings():
233            if self.warnings:
234                # if self.warnings is set, use it to filter all the warnings
235                warnings.simplefilter(self.warnings)
236            startTime = time.perf_counter()
237            startTestRun = getattr(result, 'startTestRun', None)
238            if startTestRun is not None:
239                startTestRun()
240            try:
241                test(result)
242            finally:
243                stopTestRun = getattr(result, 'stopTestRun', None)
244                if stopTestRun is not None:
245                    stopTestRun()
246            stopTime = time.perf_counter()
247        timeTaken = stopTime - startTime
248        result.printErrors()
249        if self.durations is not None:
250            self._printDurations(result)
251
252        if hasattr(result, 'separator2'):
253            self.stream.writeln(result.separator2)
254
255        run = result.testsRun
256        self.stream.writeln("Ran %d test%s in %.3fs" %
257                            (run, run != 1 and "s" or "", timeTaken))
258        self.stream.writeln()
259
260        expectedFails = unexpectedSuccesses = skipped = 0
261        try:
262            results = map(len, (result.expectedFailures,
263                                result.unexpectedSuccesses,
264                                result.skipped))
265        except AttributeError:
266            pass
267        else:
268            expectedFails, unexpectedSuccesses, skipped = results
269
270        infos = []
271        if not result.wasSuccessful():
272            self.stream.write("FAILED")
273            failed, errored = len(result.failures), len(result.errors)
274            if failed:
275                infos.append("failures=%d" % failed)
276            if errored:
277                infos.append("errors=%d" % errored)
278        elif run == 0 and not skipped:
279            self.stream.write("NO TESTS RAN")
280        else:
281            self.stream.write("OK")
282        if skipped:
283            infos.append("skipped=%d" % skipped)
284        if expectedFails:
285            infos.append("expected failures=%d" % expectedFails)
286        if unexpectedSuccesses:
287            infos.append("unexpected successes=%d" % unexpectedSuccesses)
288        if infos:
289            self.stream.writeln(" (%s)" % (", ".join(infos),))
290        else:
291            self.stream.write("\n")
292        self.stream.flush()
293        return result

Run the given test case or test suite.

class TestLoader:
 64class TestLoader(object):
 65    """
 66    This class is responsible for loading tests according to various criteria
 67    and returning them wrapped in a TestSuite
 68    """
 69    testMethodPrefix = 'test'
 70    sortTestMethodsUsing = staticmethod(util.three_way_cmp)
 71    testNamePatterns = None
 72    suiteClass = suite.TestSuite
 73    _top_level_dir = None
 74
 75    def __init__(self):
 76        super(TestLoader, self).__init__()
 77        self.errors = []
 78        # Tracks packages which we have called into via load_tests, to
 79        # avoid infinite re-entrancy.
 80        self._loading_packages = set()
 81
 82    def loadTestsFromTestCase(self, testCaseClass):
 83        """Return a suite of all test cases contained in testCaseClass"""
 84        if issubclass(testCaseClass, suite.TestSuite):
 85            raise TypeError("Test cases should not be derived from "
 86                            "TestSuite. Maybe you meant to derive from "
 87                            "TestCase?")
 88        if testCaseClass in (case.TestCase, case.FunctionTestCase):
 89            # We don't load any tests from base types that should not be loaded.
 90            testCaseNames = []
 91        else:
 92            testCaseNames = self.getTestCaseNames(testCaseClass)
 93            if not testCaseNames and hasattr(testCaseClass, 'runTest'):
 94                testCaseNames = ['runTest']
 95        loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
 96        return loaded_suite
 97
 98    def loadTestsFromModule(self, module, *, pattern=None):
 99        """Return a suite of all test cases contained in the given module"""
100        tests = []
101        for name in dir(module):
102            obj = getattr(module, name)
103            if (
104                isinstance(obj, type)
105                and issubclass(obj, case.TestCase)
106                and obj not in (case.TestCase, case.FunctionTestCase)
107            ):
108                tests.append(self.loadTestsFromTestCase(obj))
109
110        load_tests = getattr(module, 'load_tests', None)
111        tests = self.suiteClass(tests)
112        if load_tests is not None:
113            try:
114                return load_tests(self, tests, pattern)
115            except Exception as e:
116                error_case, error_message = _make_failed_load_tests(
117                    module.__name__, e, self.suiteClass)
118                self.errors.append(error_message)
119                return error_case
120        return tests
121
122    def loadTestsFromName(self, name, module=None):
123        """Return a suite of all test cases given a string specifier.
124
125        The name may resolve either to a module, a test case class, a
126        test method within a test case class, or a callable object which
127        returns a TestCase or TestSuite instance.
128
129        The method optionally resolves the names relative to a given module.
130        """
131        parts = name.split('.')
132        error_case, error_message = None, None
133        if module is None:
134            parts_copy = parts[:]
135            while parts_copy:
136                try:
137                    module_name = '.'.join(parts_copy)
138                    module = __import__(module_name)
139                    break
140                except ImportError:
141                    next_attribute = parts_copy.pop()
142                    # Last error so we can give it to the user if needed.
143                    error_case, error_message = _make_failed_import_test(
144                        next_attribute, self.suiteClass)
145                    if not parts_copy:
146                        # Even the top level import failed: report that error.
147                        self.errors.append(error_message)
148                        return error_case
149            parts = parts[1:]
150        obj = module
151        for part in parts:
152            try:
153                parent, obj = obj, getattr(obj, part)
154            except AttributeError as e:
155                # We can't traverse some part of the name.
156                if (getattr(obj, '__path__', None) is not None
157                    and error_case is not None):
158                    # This is a package (no __path__ per importlib docs), and we
159                    # encountered an error importing something. We cannot tell
160                    # the difference between package.WrongNameTestClass and
161                    # package.wrong_module_name so we just report the
162                    # ImportError - it is more informative.
163                    self.errors.append(error_message)
164                    return error_case
165                else:
166                    # Otherwise, we signal that an AttributeError has occurred.
167                    error_case, error_message = _make_failed_test(
168                        part, e, self.suiteClass,
169                        'Failed to access attribute:\n%s' % (
170                            traceback.format_exc(),))
171                    self.errors.append(error_message)
172                    return error_case
173
174        if isinstance(obj, types.ModuleType):
175            return self.loadTestsFromModule(obj)
176        elif (
177            isinstance(obj, type)
178            and issubclass(obj, case.TestCase)
179            and obj not in (case.TestCase, case.FunctionTestCase)
180        ):
181            return self.loadTestsFromTestCase(obj)
182        elif (isinstance(obj, types.FunctionType) and
183              isinstance(parent, type) and
184              issubclass(parent, case.TestCase)):
185            name = parts[-1]
186            inst = parent(name)
187            # static methods follow a different path
188            if not isinstance(getattr(inst, name), types.FunctionType):
189                return self.suiteClass([inst])
190        elif isinstance(obj, suite.TestSuite):
191            return obj
192        if callable(obj):
193            test = obj()
194            if isinstance(test, suite.TestSuite):
195                return test
196            elif isinstance(test, case.TestCase):
197                return self.suiteClass([test])
198            else:
199                raise TypeError("calling %s returned %s, not a test" %
200                                (obj, test))
201        else:
202            raise TypeError("don't know how to make test from: %s" % obj)
203
204    def loadTestsFromNames(self, names, module=None):
205        """Return a suite of all test cases found using the given sequence
206        of string specifiers. See 'loadTestsFromName()'.
207        """
208        suites = [self.loadTestsFromName(name, module) for name in names]
209        return self.suiteClass(suites)
210
211    def getTestCaseNames(self, testCaseClass):
212        """Return a sorted sequence of method names found within testCaseClass
213        """
214        def shouldIncludeMethod(attrname):
215            if not attrname.startswith(self.testMethodPrefix):
216                return False
217            testFunc = getattr(testCaseClass, attrname)
218            if not callable(testFunc):
219                return False
220            fullName = f'%s.%s.%s' % (
221                testCaseClass.__module__, testCaseClass.__qualname__, attrname
222            )
223            return self.testNamePatterns is None or \
224                any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
225        testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
226        if self.sortTestMethodsUsing:
227            testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
228        return testFnNames
229
230    def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
231        """Find and return all test modules from the specified start
232        directory, recursing into subdirectories to find them and return all
233        tests found within them. Only test files that match the pattern will
234        be loaded. (Using shell style pattern matching.)
235
236        All test modules must be importable from the top level of the project.
237        If the start directory is not the top level directory then the top
238        level directory must be specified separately.
239
240        If a test package name (directory with '__init__.py') matches the
241        pattern then the package will be checked for a 'load_tests' function. If
242        this exists then it will be called with (loader, tests, pattern) unless
243        the package has already had load_tests called from the same discovery
244        invocation, in which case the package module object is not scanned for
245        tests - this ensures that when a package uses discover to further
246        discover child tests that infinite recursion does not happen.
247
248        If load_tests exists then discovery does *not* recurse into the package,
249        load_tests is responsible for loading all tests in the package.
250
251        The pattern is deliberately not stored as a loader attribute so that
252        packages can continue discovery themselves. top_level_dir is stored so
253        load_tests does not need to pass this argument in to loader.discover().
254
255        Paths are sorted before being imported to ensure reproducible execution
256        order even on filesystems with non-alphabetical ordering like ext3/4.
257        """
258        original_top_level_dir = self._top_level_dir
259        set_implicit_top = False
260        if top_level_dir is None and self._top_level_dir is not None:
261            # make top_level_dir optional if called from load_tests in a package
262            top_level_dir = self._top_level_dir
263        elif top_level_dir is None:
264            set_implicit_top = True
265            top_level_dir = start_dir
266
267        top_level_dir = os.path.abspath(top_level_dir)
268
269        if not top_level_dir in sys.path:
270            # all test modules must be importable from the top level directory
271            # should we *unconditionally* put the start directory in first
272            # in sys.path to minimise likelihood of conflicts between installed
273            # modules and development versions?
274            sys.path.insert(0, top_level_dir)
275        self._top_level_dir = top_level_dir
276
277        is_not_importable = False
278        if os.path.isdir(os.path.abspath(start_dir)):
279            start_dir = os.path.abspath(start_dir)
280            if start_dir != top_level_dir:
281                is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
282        else:
283            # support for discovery from dotted module names
284            try:
285                __import__(start_dir)
286            except ImportError:
287                is_not_importable = True
288            else:
289                the_module = sys.modules[start_dir]
290                top_part = start_dir.split('.')[0]
291                try:
292                    start_dir = os.path.abspath(
293                        os.path.dirname((the_module.__file__)))
294                except AttributeError:
295                    if the_module.__name__ in sys.builtin_module_names:
296                        # builtin module
297                        raise TypeError('Can not use builtin modules '
298                                        'as dotted module names') from None
299                    else:
300                        raise TypeError(
301                            f"don't know how to discover from {the_module!r}"
302                            ) from None
303
304                if set_implicit_top:
305                    self._top_level_dir = self._get_directory_containing_module(top_part)
306                    sys.path.remove(top_level_dir)
307
308        if is_not_importable:
309            raise ImportError('Start directory is not importable: %r' % start_dir)
310
311        tests = list(self._find_tests(start_dir, pattern))
312        self._top_level_dir = original_top_level_dir
313        return self.suiteClass(tests)
314
315    def _get_directory_containing_module(self, module_name):
316        module = sys.modules[module_name]
317        full_path = os.path.abspath(module.__file__)
318
319        if os.path.basename(full_path).lower().startswith('__init__.py'):
320            return os.path.dirname(os.path.dirname(full_path))
321        else:
322            # here we have been given a module rather than a package - so
323            # all we can do is search the *same* directory the module is in
324            # should an exception be raised instead
325            return os.path.dirname(full_path)
326
327    def _get_name_from_path(self, path):
328        if path == self._top_level_dir:
329            return '.'
330        path = _splitext(os.path.normpath(path))
331
332        _relpath = os.path.relpath(path, self._top_level_dir)
333        assert not os.path.isabs(_relpath), "Path must be within the project"
334        assert not _relpath.startswith('..'), "Path must be within the project"
335
336        name = _relpath.replace(os.path.sep, '.')
337        return name
338
339    def _get_module_from_name(self, name):
340        __import__(name)
341        return sys.modules[name]
342
343    def _match_path(self, path, full_path, pattern):
344        # override this method to use alternative matching strategy
345        return fnmatch(path, pattern)
346
347    def _find_tests(self, start_dir, pattern):
348        """Used by discovery. Yields test suites it loads."""
349        # Handle the __init__ in this package
350        name = self._get_name_from_path(start_dir)
351        # name is '.' when start_dir == top_level_dir (and top_level_dir is by
352        # definition not a package).
353        if name != '.' and name not in self._loading_packages:
354            # name is in self._loading_packages while we have called into
355            # loadTestsFromModule with name.
356            tests, should_recurse = self._find_test_path(start_dir, pattern)
357            if tests is not None:
358                yield tests
359            if not should_recurse:
360                # Either an error occurred, or load_tests was used by the
361                # package.
362                return
363        # Handle the contents.
364        paths = sorted(os.listdir(start_dir))
365        for path in paths:
366            full_path = os.path.join(start_dir, path)
367            tests, should_recurse = self._find_test_path(full_path, pattern)
368            if tests is not None:
369                yield tests
370            if should_recurse:
371                # we found a package that didn't use load_tests.
372                name = self._get_name_from_path(full_path)
373                self._loading_packages.add(name)
374                try:
375                    yield from self._find_tests(full_path, pattern)
376                finally:
377                    self._loading_packages.discard(name)
378
379    def _find_test_path(self, full_path, pattern):
380        """Used by discovery.
381
382        Loads tests from a single file, or a directories' __init__.py when
383        passed the directory.
384
385        Returns a tuple (None_or_tests_from_file, should_recurse).
386        """
387        basename = os.path.basename(full_path)
388        if os.path.isfile(full_path):
389            if not VALID_MODULE_NAME.match(basename):
390                # valid Python identifiers only
391                return None, False
392            if not self._match_path(basename, full_path, pattern):
393                return None, False
394            # if the test file matches, load it
395            name = self._get_name_from_path(full_path)
396            try:
397                module = self._get_module_from_name(name)
398            except case.SkipTest as e:
399                return _make_skipped_test(name, e, self.suiteClass), False
400            except:
401                error_case, error_message = \
402                    _make_failed_import_test(name, self.suiteClass)
403                self.errors.append(error_message)
404                return error_case, False
405            else:
406                mod_file = os.path.abspath(
407                    getattr(module, '__file__', full_path))
408                realpath = _splitext(
409                    os.path.realpath(mod_file))
410                fullpath_noext = _splitext(
411                    os.path.realpath(full_path))
412                if realpath.lower() != fullpath_noext.lower():
413                    module_dir = os.path.dirname(realpath)
414                    mod_name = _splitext(
415                        os.path.basename(full_path))
416                    expected_dir = os.path.dirname(full_path)
417                    msg = ("%r module incorrectly imported from %r. Expected "
418                           "%r. Is this module globally installed?")
419                    raise ImportError(
420                        msg % (mod_name, module_dir, expected_dir))
421                return self.loadTestsFromModule(module, pattern=pattern), False
422        elif os.path.isdir(full_path):
423            if not os.path.isfile(os.path.join(full_path, '__init__.py')):
424                return None, False
425
426            load_tests = None
427            tests = None
428            name = self._get_name_from_path(full_path)
429            try:
430                package = self._get_module_from_name(name)
431            except case.SkipTest as e:
432                return _make_skipped_test(name, e, self.suiteClass), False
433            except:
434                error_case, error_message = \
435                    _make_failed_import_test(name, self.suiteClass)
436                self.errors.append(error_message)
437                return error_case, False
438            else:
439                load_tests = getattr(package, 'load_tests', None)
440                # Mark this package as being in load_tests (possibly ;))
441                self._loading_packages.add(name)
442                try:
443                    tests = self.loadTestsFromModule(package, pattern=pattern)
444                    if load_tests is not None:
445                        # loadTestsFromModule(package) has loaded tests for us.
446                        return tests, False
447                    return tests, True
448                finally:
449                    self._loading_packages.discard(name)
450        else:
451            return None, False

This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite

testMethodPrefix = 'test'
def sortTestMethodsUsing(x, y):
116def three_way_cmp(x, y):
117    """Return -1 if x < y, 0 if x == y and 1 if x > y"""
118    return (x > y) - (x < y)

Return -1 if x < y, 0 if x == y and 1 if x > y

testNamePatterns = None
suiteClass = <class 'TestSuite'>
errors
def loadTestsFromTestCase(self, testCaseClass):
82    def loadTestsFromTestCase(self, testCaseClass):
83        """Return a suite of all test cases contained in testCaseClass"""
84        if issubclass(testCaseClass, suite.TestSuite):
85            raise TypeError("Test cases should not be derived from "
86                            "TestSuite. Maybe you meant to derive from "
87                            "TestCase?")
88        if testCaseClass in (case.TestCase, case.FunctionTestCase):
89            # We don't load any tests from base types that should not be loaded.
90            testCaseNames = []
91        else:
92            testCaseNames = self.getTestCaseNames(testCaseClass)
93            if not testCaseNames and hasattr(testCaseClass, 'runTest'):
94                testCaseNames = ['runTest']
95        loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
96        return loaded_suite

Return a suite of all test cases contained in testCaseClass

def loadTestsFromModule(self, module, *, pattern=None):
 98    def loadTestsFromModule(self, module, *, pattern=None):
 99        """Return a suite of all test cases contained in the given module"""
100        tests = []
101        for name in dir(module):
102            obj = getattr(module, name)
103            if (
104                isinstance(obj, type)
105                and issubclass(obj, case.TestCase)
106                and obj not in (case.TestCase, case.FunctionTestCase)
107            ):
108                tests.append(self.loadTestsFromTestCase(obj))
109
110        load_tests = getattr(module, 'load_tests', None)
111        tests = self.suiteClass(tests)
112        if load_tests is not None:
113            try:
114                return load_tests(self, tests, pattern)
115            except Exception as e:
116                error_case, error_message = _make_failed_load_tests(
117                    module.__name__, e, self.suiteClass)
118                self.errors.append(error_message)
119                return error_case
120        return tests

Return a suite of all test cases contained in the given module

def loadTestsFromName(self, name, module=None):
122    def loadTestsFromName(self, name, module=None):
123        """Return a suite of all test cases given a string specifier.
124
125        The name may resolve either to a module, a test case class, a
126        test method within a test case class, or a callable object which
127        returns a TestCase or TestSuite instance.
128
129        The method optionally resolves the names relative to a given module.
130        """
131        parts = name.split('.')
132        error_case, error_message = None, None
133        if module is None:
134            parts_copy = parts[:]
135            while parts_copy:
136                try:
137                    module_name = '.'.join(parts_copy)
138                    module = __import__(module_name)
139                    break
140                except ImportError:
141                    next_attribute = parts_copy.pop()
142                    # Last error so we can give it to the user if needed.
143                    error_case, error_message = _make_failed_import_test(
144                        next_attribute, self.suiteClass)
145                    if not parts_copy:
146                        # Even the top level import failed: report that error.
147                        self.errors.append(error_message)
148                        return error_case
149            parts = parts[1:]
150        obj = module
151        for part in parts:
152            try:
153                parent, obj = obj, getattr(obj, part)
154            except AttributeError as e:
155                # We can't traverse some part of the name.
156                if (getattr(obj, '__path__', None) is not None
157                    and error_case is not None):
158                    # This is a package (no __path__ per importlib docs), and we
159                    # encountered an error importing something. We cannot tell
160                    # the difference between package.WrongNameTestClass and
161                    # package.wrong_module_name so we just report the
162                    # ImportError - it is more informative.
163                    self.errors.append(error_message)
164                    return error_case
165                else:
166                    # Otherwise, we signal that an AttributeError has occurred.
167                    error_case, error_message = _make_failed_test(
168                        part, e, self.suiteClass,
169                        'Failed to access attribute:\n%s' % (
170                            traceback.format_exc(),))
171                    self.errors.append(error_message)
172                    return error_case
173
174        if isinstance(obj, types.ModuleType):
175            return self.loadTestsFromModule(obj)
176        elif (
177            isinstance(obj, type)
178            and issubclass(obj, case.TestCase)
179            and obj not in (case.TestCase, case.FunctionTestCase)
180        ):
181            return self.loadTestsFromTestCase(obj)
182        elif (isinstance(obj, types.FunctionType) and
183              isinstance(parent, type) and
184              issubclass(parent, case.TestCase)):
185            name = parts[-1]
186            inst = parent(name)
187            # static methods follow a different path
188            if not isinstance(getattr(inst, name), types.FunctionType):
189                return self.suiteClass([inst])
190        elif isinstance(obj, suite.TestSuite):
191            return obj
192        if callable(obj):
193            test = obj()
194            if isinstance(test, suite.TestSuite):
195                return test
196            elif isinstance(test, case.TestCase):
197                return self.suiteClass([test])
198            else:
199                raise TypeError("calling %s returned %s, not a test" %
200                                (obj, test))
201        else:
202            raise TypeError("don't know how to make test from: %s" % obj)

Return a suite of all test cases given a string specifier.

The name may resolve either to a module, a test case class, a test method within a test case class, or a callable object which returns a TestCase or TestSuite instance.

The method optionally resolves the names relative to a given module.

def loadTestsFromNames(self, names, module=None):
204    def loadTestsFromNames(self, names, module=None):
205        """Return a suite of all test cases found using the given sequence
206        of string specifiers. See 'loadTestsFromName()'.
207        """
208        suites = [self.loadTestsFromName(name, module) for name in names]
209        return self.suiteClass(suites)

Return a suite of all test cases found using the given sequence of string specifiers. See 'loadTestsFromName()'.

def getTestCaseNames(self, testCaseClass):
211    def getTestCaseNames(self, testCaseClass):
212        """Return a sorted sequence of method names found within testCaseClass
213        """
214        def shouldIncludeMethod(attrname):
215            if not attrname.startswith(self.testMethodPrefix):
216                return False
217            testFunc = getattr(testCaseClass, attrname)
218            if not callable(testFunc):
219                return False
220            fullName = f'%s.%s.%s' % (
221                testCaseClass.__module__, testCaseClass.__qualname__, attrname
222            )
223            return self.testNamePatterns is None or \
224                any(fnmatchcase(fullName, pattern) for pattern in self.testNamePatterns)
225        testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
226        if self.sortTestMethodsUsing:
227            testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
228        return testFnNames

Return a sorted sequence of method names found within testCaseClass

def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
230    def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
231        """Find and return all test modules from the specified start
232        directory, recursing into subdirectories to find them and return all
233        tests found within them. Only test files that match the pattern will
234        be loaded. (Using shell style pattern matching.)
235
236        All test modules must be importable from the top level of the project.
237        If the start directory is not the top level directory then the top
238        level directory must be specified separately.
239
240        If a test package name (directory with '__init__.py') matches the
241        pattern then the package will be checked for a 'load_tests' function. If
242        this exists then it will be called with (loader, tests, pattern) unless
243        the package has already had load_tests called from the same discovery
244        invocation, in which case the package module object is not scanned for
245        tests - this ensures that when a package uses discover to further
246        discover child tests that infinite recursion does not happen.
247
248        If load_tests exists then discovery does *not* recurse into the package,
249        load_tests is responsible for loading all tests in the package.
250
251        The pattern is deliberately not stored as a loader attribute so that
252        packages can continue discovery themselves. top_level_dir is stored so
253        load_tests does not need to pass this argument in to loader.discover().
254
255        Paths are sorted before being imported to ensure reproducible execution
256        order even on filesystems with non-alphabetical ordering like ext3/4.
257        """
258        original_top_level_dir = self._top_level_dir
259        set_implicit_top = False
260        if top_level_dir is None and self._top_level_dir is not None:
261            # make top_level_dir optional if called from load_tests in a package
262            top_level_dir = self._top_level_dir
263        elif top_level_dir is None:
264            set_implicit_top = True
265            top_level_dir = start_dir
266
267        top_level_dir = os.path.abspath(top_level_dir)
268
269        if not top_level_dir in sys.path:
270            # all test modules must be importable from the top level directory
271            # should we *unconditionally* put the start directory in first
272            # in sys.path to minimise likelihood of conflicts between installed
273            # modules and development versions?
274            sys.path.insert(0, top_level_dir)
275        self._top_level_dir = top_level_dir
276
277        is_not_importable = False
278        if os.path.isdir(os.path.abspath(start_dir)):
279            start_dir = os.path.abspath(start_dir)
280            if start_dir != top_level_dir:
281                is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
282        else:
283            # support for discovery from dotted module names
284            try:
285                __import__(start_dir)
286            except ImportError:
287                is_not_importable = True
288            else:
289                the_module = sys.modules[start_dir]
290                top_part = start_dir.split('.')[0]
291                try:
292                    start_dir = os.path.abspath(
293                        os.path.dirname((the_module.__file__)))
294                except AttributeError:
295                    if the_module.__name__ in sys.builtin_module_names:
296                        # builtin module
297                        raise TypeError('Can not use builtin modules '
298                                        'as dotted module names') from None
299                    else:
300                        raise TypeError(
301                            f"don't know how to discover from {the_module!r}"
302                            ) from None
303
304                if set_implicit_top:
305                    self._top_level_dir = self._get_directory_containing_module(top_part)
306                    sys.path.remove(top_level_dir)
307
308        if is_not_importable:
309            raise ImportError('Start directory is not importable: %r' % start_dir)
310
311        tests = list(self._find_tests(start_dir, pattern))
312        self._top_level_dir = original_top_level_dir
313        return self.suiteClass(tests)

Find and return all test modules from the specified start directory, recursing into subdirectories to find them and return all tests found within them. Only test files that match the pattern will be loaded. (Using shell style pattern matching.)

All test modules must be importable from the top level of the project. If the start directory is not the top level directory then the top level directory must be specified separately.

If a test package name (directory with '__init__.py') matches the pattern then the package will be checked for a 'load_tests' function. If this exists then it will be called with (loader, tests, pattern) unless the package has already had load_tests called from the same discovery invocation, in which case the package module object is not scanned for tests - this ensures that when a package uses discover to further discover child tests that infinite recursion does not happen.

If load_tests exists then discovery does *not* recurse into the package, load_tests is responsible for loading all tests in the package.

The pattern is deliberately not stored as a loader attribute so that packages can continue discovery themselves. top_level_dir is stored so load_tests does not need to pass this argument in to loader.discover().

Paths are sorted before being imported to ensure reproducible execution order even on filesystems with non-alphabetical ordering like ext3/4.

class FunctionTestCase(unittest.TestCase):
1388class FunctionTestCase(TestCase):
1389    """A test case that wraps a test function.
1390
1391    This is useful for slipping pre-existing test functions into the
1392    unittest framework. Optionally, set-up and tidy-up functions can be
1393    supplied. As with TestCase, the tidy-up ('tearDown') function will
1394    always be called if the set-up ('setUp') function ran successfully.
1395    """
1396
1397    def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
1398        super(FunctionTestCase, self).__init__()
1399        self._setUpFunc = setUp
1400        self._tearDownFunc = tearDown
1401        self._testFunc = testFunc
1402        self._description = description
1403
1404    def setUp(self):
1405        if self._setUpFunc is not None:
1406            self._setUpFunc()
1407
1408    def tearDown(self):
1409        if self._tearDownFunc is not None:
1410            self._tearDownFunc()
1411
1412    def runTest(self):
1413        self._testFunc()
1414
1415    def id(self):
1416        return self._testFunc.__name__
1417
1418    def __eq__(self, other):
1419        if not isinstance(other, self.__class__):
1420            return NotImplemented
1421
1422        return self._setUpFunc == other._setUpFunc and \
1423               self._tearDownFunc == other._tearDownFunc and \
1424               self._testFunc == other._testFunc and \
1425               self._description == other._description
1426
1427    def __hash__(self):
1428        return hash((type(self), self._setUpFunc, self._tearDownFunc,
1429                     self._testFunc, self._description))
1430
1431    def __str__(self):
1432        return "%s (%s)" % (strclass(self.__class__),
1433                            self._testFunc.__name__)
1434
1435    def __repr__(self):
1436        return "<%s tec=%s>" % (strclass(self.__class__),
1437                                     self._testFunc)
1438
1439    def shortDescription(self):
1440        if self._description is not None:
1441            return self._description
1442        doc = self._testFunc.__doc__
1443        return doc and doc.split("\n")[0].strip() or None

A test case that wraps a test function.

This is useful for slipping pre-existing test functions into the unittest framework. Optionally, set-up and tidy-up functions can be supplied. As with TestCase, the tidy-up ('tearDown') function will always be called if the set-up ('setUp') function ran successfully.

FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)
1397    def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
1398        super(FunctionTestCase, self).__init__()
1399        self._setUpFunc = setUp
1400        self._tearDownFunc = tearDown
1401        self._testFunc = testFunc
1402        self._description = description

Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name.

def setUp(self):
1404    def setUp(self):
1405        if self._setUpFunc is not None:
1406            self._setUpFunc()

Hook method for setting up the test fixture before exercising it.

def tearDown(self):
1408    def tearDown(self):
1409        if self._tearDownFunc is not None:
1410            self._tearDownFunc()

Hook method for deconstructing the test fixture after testing it.

def runTest(self):
1412    def runTest(self):
1413        self._testFunc()
def id(self):
1415    def id(self):
1416        return self._testFunc.__name__
def shortDescription(self):
1439    def shortDescription(self):
1440        if self._description is not None:
1441            return self._description
1442        doc = self._testFunc.__doc__
1443        return doc and doc.split("\n")[0].strip() or None

Returns a one-line description of the test, or None if no description has been provided.

The default implementation of this method returns the first line of the specified test method's docstring.

main = <class 'unittest.main.TestProgram'>
defaultTestLoader = <TestLoader object>
class SkipTest(builtins.Exception):
27class SkipTest(Exception):
28    """
29    Raise this exception in a test to skip it.
30
31    Usually you can use TestCase.skipTest() or one of the skipping decorators
32    instead of raising this directly.
33    """

Raise this exception in a test to skip it.

Usually you can use TestCase.skipTest() or one of the skipping decorators instead of raising this directly.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
add_note
args
def skip(reason):
149def skip(reason):
150    """
151    Unconditionally skip a test.
152    """
153    def decorator(test_item):
154        if not isinstance(test_item, type):
155            @functools.wraps(test_item)
156            def skip_wrapper(*args, **kwargs):
157                raise SkipTest(reason)
158            test_item = skip_wrapper
159
160        test_item.__unittest_skip__ = True
161        test_item.__unittest_skip_why__ = reason
162        return test_item
163    if isinstance(reason, types.FunctionType):
164        test_item = reason
165        reason = ''
166        return decorator(test_item)
167    return decorator

Unconditionally skip a test.

def skipIf(condition, reason):
169def skipIf(condition, reason):
170    """
171    Skip a test if the condition is true.
172    """
173    if condition:
174        return skip(reason)
175    return _id

Skip a test if the condition is true.

def skipUnless(condition, reason):
177def skipUnless(condition, reason):
178    """
179    Skip a test unless the condition is true.
180    """
181    if not condition:
182        return skip(reason)
183    return _id

Skip a test unless the condition is true.

def expectedFailure(test_item):
185def expectedFailure(test_item):
186    test_item.__unittest_expecting_failure__ = True
187    return test_item
class TextTestResult(unittest.TestResult):
 31class TextTestResult(result.TestResult):
 32    """A test result class that can print formatted text results to a stream.
 33
 34    Used by TextTestRunner.
 35    """
 36    separator1 = '=' * 70
 37    separator2 = '-' * 70
 38
 39    def __init__(self, stream, descriptions, verbosity, *, durations=None):
 40        """Construct a TextTestResult. Subclasses should accept **kwargs
 41        to ensure compatibility as the interface changes."""
 42        super(TextTestResult, self).__init__(stream, descriptions, verbosity)
 43        self.stream = stream
 44        self.showAll = verbosity > 1
 45        self.dots = verbosity == 1
 46        self.descriptions = descriptions
 47        self._newline = True
 48        self.durations = durations
 49
 50    def getDescription(self, test):
 51        doc_first_line = test.shortDescription()
 52        if self.descriptions and doc_first_line:
 53            return '\n'.join((str(test), doc_first_line))
 54        else:
 55            return str(test)
 56
 57    def startTest(self, test):
 58        super(TextTestResult, self).startTest(test)
 59        if self.showAll:
 60            self.stream.write(self.getDescription(test))
 61            self.stream.write(" ... ")
 62            self.stream.flush()
 63            self._newline = False
 64
 65    def _write_status(self, test, status):
 66        is_subtest = isinstance(test, _SubTest)
 67        if is_subtest or self._newline:
 68            if not self._newline:
 69                self.stream.writeln()
 70            if is_subtest:
 71                self.stream.write("  ")
 72            self.stream.write(self.getDescription(test))
 73            self.stream.write(" ... ")
 74        self.stream.writeln(status)
 75        self.stream.flush()
 76        self._newline = True
 77
 78    def addSubTest(self, test, subtest, err):
 79        if err is not None:
 80            if self.showAll:
 81                if issubclass(err[0], subtest.failureException):
 82                    self._write_status(subtest, "FAIL")
 83                else:
 84                    self._write_status(subtest, "ERROR")
 85            elif self.dots:
 86                if issubclass(err[0], subtest.failureException):
 87                    self.stream.write('F')
 88                else:
 89                    self.stream.write('E')
 90                self.stream.flush()
 91        super(TextTestResult, self).addSubTest(test, subtest, err)
 92
 93    def addSuccess(self, test):
 94        super(TextTestResult, self).addSuccess(test)
 95        if self.showAll:
 96            self._write_status(test, "ok")
 97        elif self.dots:
 98            self.stream.write('.')
 99            self.stream.flush()
100
101    def addError(self, test, err):
102        super(TextTestResult, self).addError(test, err)
103        if self.showAll:
104            self._write_status(test, "ERROR")
105        elif self.dots:
106            self.stream.write('E')
107            self.stream.flush()
108
109    def addFailure(self, test, err):
110        super(TextTestResult, self).addFailure(test, err)
111        if self.showAll:
112            self._write_status(test, "FAIL")
113        elif self.dots:
114            self.stream.write('F')
115            self.stream.flush()
116
117    def addSkip(self, test, reason):
118        super(TextTestResult, self).addSkip(test, reason)
119        if self.showAll:
120            self._write_status(test, "skipped {0!r}".format(reason))
121        elif self.dots:
122            self.stream.write("s")
123            self.stream.flush()
124
125    def addExpectedFailure(self, test, err):
126        super(TextTestResult, self).addExpectedFailure(test, err)
127        if self.showAll:
128            self.stream.writeln("expected failure")
129            self.stream.flush()
130        elif self.dots:
131            self.stream.write("x")
132            self.stream.flush()
133
134    def addUnexpectedSuccess(self, test):
135        super(TextTestResult, self).addUnexpectedSuccess(test)
136        if self.showAll:
137            self.stream.writeln("unexpected success")
138            self.stream.flush()
139        elif self.dots:
140            self.stream.write("u")
141            self.stream.flush()
142
143    def printErrors(self):
144        if self.dots or self.showAll:
145            self.stream.writeln()
146            self.stream.flush()
147        self.printErrorList('ERROR', self.errors)
148        self.printErrorList('FAIL', self.failures)
149        unexpectedSuccesses = getattr(self, 'unexpectedSuccesses', ())
150        if unexpectedSuccesses:
151            self.stream.writeln(self.separator1)
152            for test in unexpectedSuccesses:
153                self.stream.writeln(f"UNEXPECTED SUCCESS: {self.getDescription(test)}")
154            self.stream.flush()
155
156    def printErrorList(self, flavour, errors):
157        for test, err in errors:
158            self.stream.writeln(self.separator1)
159            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
160            self.stream.writeln(self.separator2)
161            self.stream.writeln("%s" % err)
162            self.stream.flush()

A test result class that can print formatted text results to a stream.

Used by TextTestRunner.

TextTestResult(stream, descriptions, verbosity, *, durations=None)
39    def __init__(self, stream, descriptions, verbosity, *, durations=None):
40        """Construct a TextTestResult. Subclasses should accept **kwargs
41        to ensure compatibility as the interface changes."""
42        super(TextTestResult, self).__init__(stream, descriptions, verbosity)
43        self.stream = stream
44        self.showAll = verbosity > 1
45        self.dots = verbosity == 1
46        self.descriptions = descriptions
47        self._newline = True
48        self.durations = durations

Construct a TextTestResult. Subclasses should accept **kwargs to ensure compatibility as the interface changes.

separator1 = '======================================================================'
separator2 = '----------------------------------------------------------------------'
stream
showAll
dots
descriptions
durations
def getDescription(self, test):
50    def getDescription(self, test):
51        doc_first_line = test.shortDescription()
52        if self.descriptions and doc_first_line:
53            return '\n'.join((str(test), doc_first_line))
54        else:
55            return str(test)
def startTest(self, test):
57    def startTest(self, test):
58        super(TextTestResult, self).startTest(test)
59        if self.showAll:
60            self.stream.write(self.getDescription(test))
61            self.stream.write(" ... ")
62            self.stream.flush()
63            self._newline = False

Called when the given test is about to be run

def addSubTest(self, test, subtest, err):
78    def addSubTest(self, test, subtest, err):
79        if err is not None:
80            if self.showAll:
81                if issubclass(err[0], subtest.failureException):
82                    self._write_status(subtest, "FAIL")
83                else:
84                    self._write_status(subtest, "ERROR")
85            elif self.dots:
86                if issubclass(err[0], subtest.failureException):
87                    self.stream.write('F')
88                else:
89                    self.stream.write('E')
90                self.stream.flush()
91        super(TextTestResult, self).addSubTest(test, subtest, err)

Called at the end of a subtest. 'err' is None if the subtest ended successfully, otherwise it's a tuple of values as returned by sys.exc_info().

def addSuccess(self, test):
93    def addSuccess(self, test):
94        super(TextTestResult, self).addSuccess(test)
95        if self.showAll:
96            self._write_status(test, "ok")
97        elif self.dots:
98            self.stream.write('.')
99            self.stream.flush()

Called when a test has completed successfully

def addError(self, test, err):
101    def addError(self, test, err):
102        super(TextTestResult, self).addError(test, err)
103        if self.showAll:
104            self._write_status(test, "ERROR")
105        elif self.dots:
106            self.stream.write('E')
107            self.stream.flush()

Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().

def addFailure(self, test, err):
109    def addFailure(self, test, err):
110        super(TextTestResult, self).addFailure(test, err)
111        if self.showAll:
112            self._write_status(test, "FAIL")
113        elif self.dots:
114            self.stream.write('F')
115            self.stream.flush()

Called when an error has occurred. 'err' is a tuple of values as returned by sys.exc_info().

def addSkip(self, test, reason):
117    def addSkip(self, test, reason):
118        super(TextTestResult, self).addSkip(test, reason)
119        if self.showAll:
120            self._write_status(test, "skipped {0!r}".format(reason))
121        elif self.dots:
122            self.stream.write("s")
123            self.stream.flush()

Called when a test is skipped.

def addExpectedFailure(self, test, err):
125    def addExpectedFailure(self, test, err):
126        super(TextTestResult, self).addExpectedFailure(test, err)
127        if self.showAll:
128            self.stream.writeln("expected failure")
129            self.stream.flush()
130        elif self.dots:
131            self.stream.write("x")
132            self.stream.flush()

Called when an expected failure/error occurred.

def addUnexpectedSuccess(self, test):
134    def addUnexpectedSuccess(self, test):
135        super(TextTestResult, self).addUnexpectedSuccess(test)
136        if self.showAll:
137            self.stream.writeln("unexpected success")
138            self.stream.flush()
139        elif self.dots:
140            self.stream.write("u")
141            self.stream.flush()

Called when a test was expected to fail, but succeed.

def printErrors(self):
143    def printErrors(self):
144        if self.dots or self.showAll:
145            self.stream.writeln()
146            self.stream.flush()
147        self.printErrorList('ERROR', self.errors)
148        self.printErrorList('FAIL', self.failures)
149        unexpectedSuccesses = getattr(self, 'unexpectedSuccesses', ())
150        if unexpectedSuccesses:
151            self.stream.writeln(self.separator1)
152            for test in unexpectedSuccesses:
153                self.stream.writeln(f"UNEXPECTED SUCCESS: {self.getDescription(test)}")
154            self.stream.flush()

Called by TestRunner after test run

def printErrorList(self, flavour, errors):
156    def printErrorList(self, flavour, errors):
157        for test, err in errors:
158            self.stream.writeln(self.separator1)
159            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
160            self.stream.writeln(self.separator2)
161            self.stream.writeln("%s" % err)
162            self.stream.flush()
def installHandler():
50def installHandler():
51    global _interrupt_handler
52    if _interrupt_handler is None:
53        default_handler = signal.getsignal(signal.SIGINT)
54        _interrupt_handler = _InterruptHandler(default_handler)
55        signal.signal(signal.SIGINT, _interrupt_handler)
def registerResult(result):
43def registerResult(result):
44    _results[result] = 1
def removeResult(result):
46def removeResult(result):
47    return bool(_results.pop(result, None))
def removeHandler(method=None):
58def removeHandler(method=None):
59    if method is not None:
60        @wraps(method)
61        def inner(*args, **kwargs):
62            initial = signal.getsignal(signal.SIGINT)
63            removeHandler()
64            try:
65                return method(*args, **kwargs)
66            finally:
67                signal.signal(signal.SIGINT, initial)
68        return inner
69
70    global _interrupt_handler
71    if _interrupt_handler is not None:
72        signal.signal(signal.SIGINT, _interrupt_handler.original_handler)
def addModuleCleanup(function, /, *args, **kwargs):
123def addModuleCleanup(function, /, *args, **kwargs):
124    """Same as addCleanup, except the cleanup items are called even if
125    setUpModule fails (unlike tearDownModule)."""
126    _module_cleanups.append((function, args, kwargs))

Same as addCleanup, except the cleanup items are called even if setUpModule fails (unlike tearDownModule).

def doModuleCleanups():
133def doModuleCleanups():
134    """Execute all module cleanup functions. Normally called for you after
135    tearDownModule."""
136    exceptions = []
137    while _module_cleanups:
138        function, args, kwargs = _module_cleanups.pop()
139        try:
140            function(*args, **kwargs)
141        except Exception as exc:
142            exceptions.append(exc)
143    if exceptions:
144        # Swallows all but first exception. If a multi-exception handler
145        # gets written we should use that here instead.
146        raise exceptions[0]

Execute all module cleanup functions. Normally called for you after tearDownModule.

def enterModuleContext(cm):
128def enterModuleContext(cm):
129    """Same as enterContext, but module-wide."""
130    return _enter_context(cm, addModuleCleanup)

Same as enterContext, but module-wide.