1
:mod:`unittest` --- Unit testing framework
2
==========================================
5
:synopsis: Unit testing framework for Python.
6
.. moduleauthor:: Steve Purcell <stephen_purcell@yahoo.com>
7
.. sectionauthor:: Steve Purcell <stephen_purcell@yahoo.com>
8
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
.. sectionauthor:: Raymond Hettinger <python@rcn.com>
11
(If you are already familiar with the basic concepts of testing, you might want
12
to skip to :ref:`the list of assert methods <assert-methods>`.)
14
The :mod:`unittest` unit testing framework was originally inspired by JUnit
15
and has a similar flavor as major unit testing frameworks in other
16
languages. It supports test automation, sharing of setup and shutdown code
17
for tests, aggregation of tests into collections, and independence of the
18
tests from the reporting framework.
20
To achieve this, :mod:`unittest` supports some important concepts in an
24
A :dfn:`test fixture` represents the preparation needed to perform one or more
25
tests, and any associate cleanup actions. This may involve, for example,
26
creating temporary or proxy databases, directories, or starting a server
30
A :dfn:`test case` is the individual unit of testing. It checks for a specific
31
response to a particular set of inputs. :mod:`unittest` provides a base class,
32
:class:`TestCase`, which may be used to create new test cases.
35
A :dfn:`test suite` is a collection of test cases, test suites, or both. It is
36
used to aggregate tests that should be executed together.
39
A :dfn:`test runner` is a component which orchestrates the execution of tests
40
and provides the outcome to the user. The runner may use a graphical interface,
41
a textual interface, or return a special value to indicate the results of
48
Another test-support module with a very different flavor.
50
`Simple Smalltalk Testing: With Patterns <http://www.XProgramming.com/testfram.htm>`_
51
Kent Beck's original paper on testing frameworks using the pattern shared
54
`Nose <http://code.google.com/p/python-nose/>`_ and `py.test <http://pytest.org>`_
55
Third-party unittest frameworks with a lighter-weight syntax for writing
56
tests. For example, ``assert func(10) == 42``.
58
`The Python Testing Tools Taxonomy <http://wiki.python.org/moin/PythonTestingToolsTaxonomy>`_
59
An extensive list of Python testing tools including functional testing
60
frameworks and mock object libraries.
62
`Testing in Python Mailing List <http://lists.idyll.org/listinfo/testing-in-python>`_
63
A special-interest-group for discussion of testing, and testing tools,
66
The script :file:`Tools/unittestgui/unittestgui.py` in the Python source distribution is
67
a GUI tool for test discovery and execution. This is intended largely for ease of use
68
for those new to unit testing. For production environments it is
69
recommended that tests be driven by a continuous integration system such as
70
`Buildbot <http://buildbot.net/trac>`_, `Jenkins <http://jenkins-ci.org>`_
71
or `Hudson <http://hudson-ci.org/>`_.
74
.. _unittest-minimal-example:
79
The :mod:`unittest` module provides a rich set of tools for constructing and
80
running tests. This section demonstrates that a small subset of the tools
81
suffice to meet the needs of most users.
83
Here is a short script to test three functions from the :mod:`random` module::
88
class TestSequenceFunctions(unittest.TestCase):
91
self.seq = list(range(10))
93
def test_shuffle(self):
94
# make sure the shuffled sequence does not lose any elements
95
random.shuffle(self.seq)
97
self.assertEqual(self.seq, list(range(10)))
99
# should raise an exception for an immutable sequence
100
self.assertRaises(TypeError, random.shuffle, (1,2,3))
102
def test_choice(self):
103
element = random.choice(self.seq)
104
self.assertTrue(element in self.seq)
106
def test_sample(self):
107
with self.assertRaises(ValueError):
108
random.sample(self.seq, 20)
109
for element in random.sample(self.seq, 5):
110
self.assertTrue(element in self.seq)
112
if __name__ == '__main__':
115
A testcase is created by subclassing :class:`unittest.TestCase`. The three
116
individual tests are defined with methods whose names start with the letters
117
``test``. This naming convention informs the test runner about which methods
120
The crux of each test is a call to :meth:`~TestCase.assertEqual` to check for an
121
expected result; :meth:`~TestCase.assertTrue` to verify a condition; or
122
:meth:`~TestCase.assertRaises` to verify that an expected exception gets raised.
123
These methods are used instead of the :keyword:`assert` statement so the test
124
runner can accumulate all test results and produce a report.
126
When a :meth:`~TestCase.setUp` method is defined, the test runner will run that
127
method prior to each test. Likewise, if a :meth:`~TestCase.tearDown` method is
128
defined, the test runner will invoke that method after each test. In the
129
example, :meth:`~TestCase.setUp` was used to create a fresh sequence for each
132
The final block shows a simple way to run the tests. :func:`unittest.main`
133
provides a command-line interface to the test script. When run from the command
134
line, the above script produces an output that looks like this::
137
----------------------------------------------------------------------
138
Ran 3 tests in 0.000s
142
Passing the ``-v`` option to your test script will instruct :func:`unittest.main`
143
to enable a higher level of verbosity, and produce the following output::
145
test_choice (__main__.TestSequenceFunctions) ... ok
146
test_sample (__main__.TestSequenceFunctions) ... ok
147
test_shuffle (__main__.TestSequenceFunctions) ... ok
149
----------------------------------------------------------------------
150
Ran 3 tests in 0.110s
154
The above examples show the most commonly used :mod:`unittest` features which
155
are sufficient to meet many everyday testing needs. The remainder of the
156
documentation explores the full feature set from first principles.
159
.. _unittest-command-line-interface:
161
Command-Line Interface
162
----------------------
164
The unittest module can be used from the command line to run tests from
165
modules, classes or even individual test methods::
167
python -m unittest test_module1 test_module2
168
python -m unittest test_module.TestClass
169
python -m unittest test_module.TestClass.test_method
171
You can pass in a list with any combination of module names, and fully
172
qualified class or method names.
174
Test modules can be specified by file path as well::
176
python -m unittest tests/test_something.py
178
This allows you to use the shell filename completion to specify the test module.
179
The file specified must still be importable as a module. The path is converted
180
to a module name by removing the '.py' and converting path separators into '.'.
181
If you want to execute a test file that isn't importable as a module you should
182
execute the file directly instead.
184
You can run tests with more detail (higher verbosity) by passing in the -v flag::
186
python -m unittest -v test_module
188
When executed without arguments :ref:`unittest-test-discovery` is started::
192
For a list of all the command-line options::
194
python -m unittest -h
196
.. versionchanged:: 3.2
197
In earlier versions it was only possible to run individual test methods and
198
not modules or classes.
204
:program:`unittest` supports these command-line options:
206
.. program:: unittest
208
.. cmdoption:: -b, --buffer
210
The standard output and standard error streams are buffered during the test
211
run. Output during a passing test is discarded. Output is echoed normally
212
on test fail or error and is added to the failure messages.
214
.. cmdoption:: -c, --catch
216
Control-C during the test run waits for the current test to end and then
217
reports all the results so far. A second control-C raises the normal
218
:exc:`KeyboardInterrupt` exception.
220
See `Signal Handling`_ for the functions that provide this functionality.
222
.. cmdoption:: -f, --failfast
224
Stop the test run on the first error or failure.
226
.. versionadded:: 3.2
227
The command-line options ``-b``, ``-c`` and ``-f`` were added.
229
The command line can also be used for test discovery, for running all of the
230
tests in a project or just a subset.
233
.. _unittest-test-discovery:
238
.. versionadded:: 3.2
240
Unittest supports simple test discovery. In order to be compatible with test
241
discovery, all of the test files must be :ref:`modules <tut-modules>` or
242
:ref:`packages <tut-packages>` importable from the top-level directory of
243
the project (this means that their filenames must be valid
244
:ref:`identifiers <identifiers>`).
246
Test discovery is implemented in :meth:`TestLoader.discover`, but can also be
247
used from the command line. The basic command-line usage is::
250
python -m unittest discover
254
As a shortcut, ``python -m unittest`` is the equivalent of
255
``python -m unittest discover``. If you want to pass arguments to test
256
discovery the ``discover`` sub-command must be used explicitly.
258
The ``discover`` sub-command has the following options:
260
.. program:: unittest discover
262
.. cmdoption:: -v, --verbose
266
.. cmdoption:: -s, --start-directory directory
268
Directory to start discovery (``.`` default)
270
.. cmdoption:: -p, --pattern pattern
272
Pattern to match test files (``test*.py`` default)
274
.. cmdoption:: -t, --top-level-directory directory
276
Top level directory of project (defaults to start directory)
278
The :option:`-s`, :option:`-p`, and :option:`-t` options can be passed in
279
as positional arguments in that order. The following two command lines
282
python -m unittest discover -s project_directory -p '*_test.py'
283
python -m unittest discover project_directory '*_test.py'
285
As well as being a path it is possible to pass a package name, for example
286
``myproject.subpackage.test``, as the start directory. The package name you
287
supply will then be imported and its location on the filesystem will be used
288
as the start directory.
292
Test discovery loads tests by importing them. Once test discovery has found
293
all the test files from the start directory you specify it turns the paths
294
into package names to import. For example :file:`foo/bar/baz.py` will be
295
imported as ``foo.bar.baz``.
297
If you have a package installed globally and attempt test discovery on
298
a different copy of the package then the import *could* happen from the
299
wrong place. If this happens test discovery will warn you and exit.
301
If you supply the start directory as a package name rather than a
302
path to a directory then discover assumes that whichever location it
303
imports from is the location you intended, so you will not get the
306
Test modules and packages can customize test loading and discovery by through
307
the `load_tests protocol`_.
310
.. _organizing-tests:
315
The basic building blocks of unit testing are :dfn:`test cases` --- single
316
scenarios that must be set up and checked for correctness. In :mod:`unittest`,
317
test cases are represented by :class:`unittest.TestCase` instances.
318
To make your own test cases you must write subclasses of
319
:class:`TestCase` or use :class:`FunctionTestCase`.
321
The testing code of a :class:`TestCase` instance should be entirely self
322
contained, such that it can be run either in isolation or in arbitrary
323
combination with any number of other test cases.
325
The simplest :class:`TestCase` subclass will simply implement a test method
326
(i.e. a method whose name starts with ``test``) in order to perform specific
331
class DefaultWidgetSizeTestCase(unittest.TestCase):
332
def test_default_widget_size(self):
333
widget = Widget('The widget')
334
self.assertEqual(widget.size(), (50, 50))
336
Note that in order to test something, we use one of the :meth:`assert\*`
337
methods provided by the :class:`TestCase` base class. If the test fails, an
338
exception will be raised, and :mod:`unittest` will identify the test case as a
339
:dfn:`failure`. Any other exceptions will be treated as :dfn:`errors`.
341
Tests can be numerous, and their set-up can be repetitive. Luckily, we
342
can factor out set-up code by implementing a method called
343
:meth:`~TestCase.setUp`, which the testing framework will automatically
344
call for every single test we run::
348
class SimpleWidgetTestCase(unittest.TestCase):
350
self.widget = Widget('The widget')
352
def test_default_widget_size(self):
353
self.assertEqual(self.widget.size(), (50,50),
354
'incorrect default size')
356
def test_widget_resize(self):
357
self.widget.resize(100,150)
358
self.assertEqual(self.widget.size(), (100,150),
359
'wrong size after resize')
362
The order in which the various tests will be run is determined
363
by sorting the test method names with respect to the built-in
364
ordering for strings.
366
If the :meth:`~TestCase.setUp` method raises an exception while the test is
367
running, the framework will consider the test to have suffered an error, and
368
the test method will not be executed.
370
Similarly, we can provide a :meth:`~TestCase.tearDown` method that tidies up
371
after the test method has been run::
375
class SimpleWidgetTestCase(unittest.TestCase):
377
self.widget = Widget('The widget')
380
self.widget.dispose()
382
If :meth:`~TestCase.setUp` succeeded, :meth:`~TestCase.tearDown` will be
383
run whether the test method succeeded or not.
385
Such a working environment for the testing code is called a :dfn:`fixture`.
387
Test case instances are grouped together according to the features they test.
388
:mod:`unittest` provides a mechanism for this: the :dfn:`test suite`,
389
represented by :mod:`unittest`'s :class:`TestSuite` class. In most cases,
390
calling :func:`unittest.main` will do the right thing and collect all the
391
module's test cases for you, and then execute them.
393
However, should you want to customize the building of your test suite,
394
you can do it yourself::
397
suite = unittest.TestSuite()
398
suite.addTest(WidgetTestCase('test_default_size'))
399
suite.addTest(WidgetTestCase('test_resize'))
402
You can place the definitions of test cases and test suites in the same modules
403
as the code they are to test (such as :file:`widget.py`), but there are several
404
advantages to placing the test code in a separate module, such as
405
:file:`test_widget.py`:
407
* The test module can be run standalone from the command line.
409
* The test code can more easily be separated from shipped code.
411
* There is less temptation to change test code to fit the code it tests without
414
* Test code should be modified much less frequently than the code it tests.
416
* Tested code can be refactored more easily.
418
* Tests for modules written in C must be in separate modules anyway, so why not
421
* If the testing strategy changes, there is no need to change the source code.
424
.. _legacy-unit-tests:
426
Re-using old test code
427
----------------------
429
Some users will find that they have existing test code that they would like to
430
run from :mod:`unittest`, without converting every old test function to a
431
:class:`TestCase` subclass.
433
For this reason, :mod:`unittest` provides a :class:`FunctionTestCase` class.
434
This subclass of :class:`TestCase` can be used to wrap an existing test
435
function. Set-up and tear-down functions can also be provided.
437
Given the following test function::
440
something = makeSomething()
441
assert something.name is not None
444
one can create an equivalent test case instance as follows, with optional
445
set-up and tear-down methods::
447
testcase = unittest.FunctionTestCase(testSomething,
448
setUp=makeSomethingDB,
449
tearDown=deleteSomethingDB)
453
Even though :class:`FunctionTestCase` can be used to quickly convert an
454
existing test base over to a :mod:`unittest`\ -based system, this approach is
455
not recommended. Taking the time to set up proper :class:`TestCase`
456
subclasses will make future test refactorings infinitely easier.
458
In some cases, the existing tests may have been written using the :mod:`doctest`
459
module. If so, :mod:`doctest` provides a :class:`DocTestSuite` class that can
460
automatically build :class:`unittest.TestSuite` instances from the existing
461
:mod:`doctest`\ -based tests.
464
.. _unittest-skipping:
466
Skipping tests and expected failures
467
------------------------------------
469
.. versionadded:: 3.1
471
Unittest supports skipping individual test methods and even whole classes of
472
tests. In addition, it supports marking a test as a "expected failure," a test
473
that is broken and will fail, but shouldn't be counted as a failure on a
476
Skipping a test is simply a matter of using the :func:`skip` :term:`decorator`
477
or one of its conditional variants.
479
Basic skipping looks like this::
481
class MyTestCase(unittest.TestCase):
483
@unittest.skip("demonstrating skipping")
484
def test_nothing(self):
485
self.fail("shouldn't happen")
487
@unittest.skipIf(mylib.__version__ < (1, 3),
488
"not supported in this library version")
489
def test_format(self):
490
# Tests that work for only a certain version of the library.
493
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
494
def test_windows_support(self):
495
# windows specific testing code
498
This is the output of running the example above in verbose mode::
500
test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'
501
test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
502
test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'
504
----------------------------------------------------------------------
505
Ran 3 tests in 0.005s
509
Classes can be skipped just like methods::
511
@unittest.skip("showing class skipping")
512
class MySkippedTestCase(unittest.TestCase):
513
def test_not_run(self):
516
:meth:`TestCase.setUp` can also skip the test. This is useful when a resource
517
that needs to be set up is not available.
519
Expected failures use the :func:`expectedFailure` decorator. ::
521
class ExpectedFailureTestCase(unittest.TestCase):
522
@unittest.expectedFailure
524
self.assertEqual(1, 0, "broken")
526
It's easy to roll your own skipping decorators by making a decorator that calls
527
:func:`skip` on the test when it wants it to be skipped. This decorator skips
528
the test unless the passed object has a certain attribute::
530
def skipUnlessHasattr(obj, attr):
531
if hasattr(obj, attr):
532
return lambda func: func
533
return unittest.skip("{!r} doesn't have {!r}".format(obj, attr))
535
The following decorators implement test skipping and expected failures:
537
.. decorator:: skip(reason)
539
Unconditionally skip the decorated test. *reason* should describe why the
540
test is being skipped.
542
.. decorator:: skipIf(condition, reason)
544
Skip the decorated test if *condition* is true.
546
.. decorator:: skipUnless(condition, reason)
548
Skip the decorated test unless *condition* is true.
550
.. decorator:: expectedFailure
552
Mark the test as an expected failure. If the test fails when run, the test
553
is not counted as a failure.
555
.. exception:: SkipTest(reason)
557
This exception is raised to skip a test.
559
Usually you can use :meth:`TestCase.skipTest` or one of the skipping
560
decorators instead of raising this directly.
562
Skipped tests will not have :meth:`setUp` or :meth:`tearDown` run around them.
563
Skipped classes will not have :meth:`setUpClass` or :meth:`tearDownClass` run.
568
Distinguishing test iterations using subtests
569
---------------------------------------------
571
.. versionadded:: 3.4
573
When some of your tests differ only by a some very small differences, for
574
instance some parameters, unittest allows you to distinguish them inside
575
the body of a test method using the :meth:`~TestCase.subTest` context manager.
577
For example, the following test::
579
class NumbersTest(unittest.TestCase):
583
Test that numbers between 0 and 5 are all even.
585
for i in range(0, 6):
586
with self.subTest(i=i):
587
self.assertEqual(i % 2, 0)
589
will produce the following output::
591
======================================================================
592
FAIL: test_even (__main__.NumbersTest) (i=1)
593
----------------------------------------------------------------------
594
Traceback (most recent call last):
595
File "subtests.py", line 32, in test_even
596
self.assertEqual(i % 2, 0)
597
AssertionError: 1 != 0
599
======================================================================
600
FAIL: test_even (__main__.NumbersTest) (i=3)
601
----------------------------------------------------------------------
602
Traceback (most recent call last):
603
File "subtests.py", line 32, in test_even
604
self.assertEqual(i % 2, 0)
605
AssertionError: 1 != 0
607
======================================================================
608
FAIL: test_even (__main__.NumbersTest) (i=5)
609
----------------------------------------------------------------------
610
Traceback (most recent call last):
611
File "subtests.py", line 32, in test_even
612
self.assertEqual(i % 2, 0)
613
AssertionError: 1 != 0
615
Without using a subtest, execution would stop after the first failure,
616
and the error would be less easy to diagnose because the value of ``i``
617
wouldn't be displayed::
619
======================================================================
620
FAIL: test_even (__main__.NumbersTest)
621
----------------------------------------------------------------------
622
Traceback (most recent call last):
623
File "subtests.py", line 32, in test_even
624
self.assertEqual(i % 2, 0)
625
AssertionError: 1 != 0
628
.. _unittest-contents:
630
Classes and functions
631
---------------------
633
This section describes in depth the API of :mod:`unittest`.
636
.. _testcase-objects:
641
.. class:: TestCase(methodName='runTest')
643
Instances of the :class:`TestCase` class represent the logical test units
644
in the :mod:`unittest` universe. This class is intended to be used as a base
645
class, with specific tests being implemented by concrete subclasses. This class
646
implements the interface needed by the test runner to allow it to drive the
647
tests, and methods that the test code can use to check for and report various
650
Each instance of :class:`TestCase` will run a single base method: the method
651
named *methodName*. However, the standard implementation of the default
652
*methodName*, ``runTest()``, will run every method starting with ``test``
653
as an individual test, and count successes and failures accordingly.
654
Therefore, in most uses of :class:`TestCase`, you will neither change
655
the *methodName* nor reimplement the default ``runTest()`` method.
657
.. versionchanged:: 3.2
658
:class:`TestCase` can be instantiated successfully without providing a
659
*methodName*. This makes it easier to experiment with :class:`TestCase`
660
from the interactive interpreter.
662
:class:`TestCase` instances provide three groups of methods: one group used
663
to run the test, another used by the test implementation to check conditions
664
and report failures, and some inquiry methods allowing information about the
665
test itself to be gathered.
667
Methods in the first group (running the test) are:
671
Method called to prepare the test fixture. This is called immediately
672
before calling the test method; any exception raised by this method will
673
be considered an error rather than a test failure. The default
674
implementation does nothing.
677
.. method:: tearDown()
679
Method called immediately after the test method has been called and the
680
result recorded. This is called even if the test method raised an
681
exception, so the implementation in subclasses may need to be particularly
682
careful about checking internal state. Any exception raised by this
683
method will be considered an error rather than a test failure. This
684
method will only be called if the :meth:`setUp` succeeds, regardless of
685
the outcome of the test method. The default implementation does nothing.
688
.. method:: setUpClass()
690
A class method called before tests in an individual class run.
691
``setUpClass`` is called with the class as the only argument
692
and must be decorated as a :func:`classmethod`::
698
See `Class and Module Fixtures`_ for more details.
700
.. versionadded:: 3.2
703
.. method:: tearDownClass()
705
A class method called after tests in an individual class have run.
706
``tearDownClass`` is called with the class as the only argument
707
and must be decorated as a :meth:`classmethod`::
710
def tearDownClass(cls):
713
See `Class and Module Fixtures`_ for more details.
715
.. versionadded:: 3.2
718
.. method:: run(result=None)
720
Run the test, collecting the result into the :class:`TestResult` object
721
passed as *result*. If *result* is omitted or ``None``, a temporary
722
result object is created (by calling the :meth:`defaultTestResult`
723
method) and used. The result object is returned to :meth:`run`'s
726
The same effect may be had by simply calling the :class:`TestCase`
729
.. versionchanged:: 3.3
730
Previous versions of ``run`` did not return the result. Neither did
733
.. method:: skipTest(reason)
735
Calling this during a test method or :meth:`setUp` skips the current
736
test. See :ref:`unittest-skipping` for more information.
738
.. versionadded:: 3.1
741
.. method:: subTest(msg=None, **params)
743
Return a context manager which executes the enclosed code block as a
744
subtest. *msg* and *params* are optional, arbitrary values which are
745
displayed whenever a subtest fails, allowing you to identify them
748
A test case can contain any number of subtest declarations, and
749
they can be arbitrarily nested.
751
See :ref:`subtests` for more information.
753
.. versionadded:: 3.4
758
Run the test without collecting the result. This allows exceptions raised
759
by the test to be propagated to the caller, and can be used to support
760
running tests under a debugger.
764
The :class:`TestCase` class provides a number of methods to check for and
765
report failures, such as:
767
+-----------------------------------------+-----------------------------+---------------+
768
| Method | Checks that | New in |
769
+=========================================+=============================+===============+
770
| :meth:`assertEqual(a, b) | ``a == b`` | |
771
| <TestCase.assertEqual>` | | |
772
+-----------------------------------------+-----------------------------+---------------+
773
| :meth:`assertNotEqual(a, b) | ``a != b`` | |
774
| <TestCase.assertNotEqual>` | | |
775
+-----------------------------------------+-----------------------------+---------------+
776
| :meth:`assertTrue(x) | ``bool(x) is True`` | |
777
| <TestCase.assertTrue>` | | |
778
+-----------------------------------------+-----------------------------+---------------+
779
| :meth:`assertFalse(x) | ``bool(x) is False`` | |
780
| <TestCase.assertFalse>` | | |
781
+-----------------------------------------+-----------------------------+---------------+
782
| :meth:`assertIs(a, b) | ``a is b`` | 3.1 |
783
| <TestCase.assertIs>` | | |
784
+-----------------------------------------+-----------------------------+---------------+
785
| :meth:`assertIsNot(a, b) | ``a is not b`` | 3.1 |
786
| <TestCase.assertIsNot>` | | |
787
+-----------------------------------------+-----------------------------+---------------+
788
| :meth:`assertIsNone(x) | ``x is None`` | 3.1 |
789
| <TestCase.assertIsNone>` | | |
790
+-----------------------------------------+-----------------------------+---------------+
791
| :meth:`assertIsNotNone(x) | ``x is not None`` | 3.1 |
792
| <TestCase.assertIsNotNone>` | | |
793
+-----------------------------------------+-----------------------------+---------------+
794
| :meth:`assertIn(a, b) | ``a in b`` | 3.1 |
795
| <TestCase.assertIn>` | | |
796
+-----------------------------------------+-----------------------------+---------------+
797
| :meth:`assertNotIn(a, b) | ``a not in b`` | 3.1 |
798
| <TestCase.assertNotIn>` | | |
799
+-----------------------------------------+-----------------------------+---------------+
800
| :meth:`assertIsInstance(a, b) | ``isinstance(a, b)`` | 3.2 |
801
| <TestCase.assertIsInstance>` | | |
802
+-----------------------------------------+-----------------------------+---------------+
803
| :meth:`assertNotIsInstance(a, b) | ``not isinstance(a, b)`` | 3.2 |
804
| <TestCase.assertNotIsInstance>` | | |
805
+-----------------------------------------+-----------------------------+---------------+
807
All the assert methods accept a *msg* argument that, if specified, is used
808
as the error message on failure (see also :data:`longMessage`).
809
Note that the *msg* keyword argument can be passed to :meth:`assertRaises`,
810
:meth:`assertRaisesRegex`, :meth:`assertWarns`, :meth:`assertWarnsRegex`
811
only when they are used as a context manager.
813
.. method:: assertEqual(first, second, msg=None)
815
Test that *first* and *second* are equal. If the values do not
816
compare equal, the test will fail.
818
In addition, if *first* and *second* are the exact same type and one of
819
list, tuple, dict, set, frozenset or str or any type that a subclass
820
registers with :meth:`addTypeEqualityFunc` the type-specific equality
821
function will be called in order to generate a more useful default
822
error message (see also the :ref:`list of type-specific methods
823
<type-specific-methods>`).
825
.. versionchanged:: 3.1
826
Added the automatic calling of type-specific equality function.
828
.. versionchanged:: 3.2
829
:meth:`assertMultiLineEqual` added as the default type equality
830
function for comparing strings.
833
.. method:: assertNotEqual(first, second, msg=None)
835
Test that *first* and *second* are not equal. If the values do
836
compare equal, the test will fail.
838
.. method:: assertTrue(expr, msg=None)
839
assertFalse(expr, msg=None)
841
Test that *expr* is true (or false).
843
Note that this is equivalent to ``bool(expr) is True`` and not to ``expr
844
is True`` (use ``assertIs(expr, True)`` for the latter). This method
845
should also be avoided when more specific methods are available (e.g.
846
``assertEqual(a, b)`` instead of ``assertTrue(a == b)``), because they
847
provide a better error message in case of failure.
850
.. method:: assertIs(first, second, msg=None)
851
assertIsNot(first, second, msg=None)
853
Test that *first* and *second* evaluate (or don't evaluate) to the
856
.. versionadded:: 3.1
859
.. method:: assertIsNone(expr, msg=None)
860
assertIsNotNone(expr, msg=None)
862
Test that *expr* is (or is not) None.
864
.. versionadded:: 3.1
867
.. method:: assertIn(first, second, msg=None)
868
assertNotIn(first, second, msg=None)
870
Test that *first* is (or is not) in *second*.
872
.. versionadded:: 3.1
875
.. method:: assertIsInstance(obj, cls, msg=None)
876
assertNotIsInstance(obj, cls, msg=None)
878
Test that *obj* is (or is not) an instance of *cls* (which can be a
879
class or a tuple of classes, as supported by :func:`isinstance`).
880
To check for the exact type, use :func:`assertIs(type(obj), cls) <assertIs>`.
882
.. versionadded:: 3.2
886
It is also possible to check the production of exceptions, warnings and
887
log messages using the following methods:
889
+---------------------------------------------------------+--------------------------------------+------------+
890
| Method | Checks that | New in |
891
+=========================================================+======================================+============+
892
| :meth:`assertRaises(exc, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises *exc* | |
893
| <TestCase.assertRaises>` | | |
894
+---------------------------------------------------------+--------------------------------------+------------+
895
| :meth:`assertRaisesRegex(exc, r, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises *exc* | 3.1 |
896
| <TestCase.assertRaisesRegex>` | and the message matches regex *r* | |
897
+---------------------------------------------------------+--------------------------------------+------------+
898
| :meth:`assertWarns(warn, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises *warn* | 3.2 |
899
| <TestCase.assertWarns>` | | |
900
+---------------------------------------------------------+--------------------------------------+------------+
901
| :meth:`assertWarnsRegex(warn, r, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises *warn* | 3.2 |
902
| <TestCase.assertWarnsRegex>` | and the message matches regex *r* | |
903
+---------------------------------------------------------+--------------------------------------+------------+
904
| :meth:`assertLogs(logger, level)` | The ``with`` block logs on *logger* | 3.4 |
905
| <TestCase.assertWarns>` | with minimum *level* | |
906
+---------------------------------------------------------+--------------------------------------+------------+
908
.. method:: assertRaises(exception, callable, *args, **kwds)
909
assertRaises(exception, msg=None)
911
Test that an exception is raised when *callable* is called with any
912
positional or keyword arguments that are also passed to
913
:meth:`assertRaises`. The test passes if *exception* is raised, is an
914
error if another exception is raised, or fails if no exception is raised.
915
To catch any of a group of exceptions, a tuple containing the exception
916
classes may be passed as *exception*.
918
If only the *exception* and possibly the *msg* arguments are given,
919
return a context manager so that the code under test can be written
920
inline rather than as a function::
922
with self.assertRaises(SomeException):
925
When used as a context manager, :meth:`assertRaises` accepts the
926
additional keyword argument *msg*.
928
The context manager will store the caught exception object in its
929
:attr:`exception` attribute. This can be useful if the intention
930
is to perform additional checks on the exception raised::
932
with self.assertRaises(SomeException) as cm:
935
the_exception = cm.exception
936
self.assertEqual(the_exception.error_code, 3)
938
.. versionchanged:: 3.1
939
Added the ability to use :meth:`assertRaises` as a context manager.
941
.. versionchanged:: 3.2
942
Added the :attr:`exception` attribute.
944
.. versionchanged:: 3.3
945
Added the *msg* keyword argument when used as a context manager.
948
.. method:: assertRaisesRegex(exception, regex, callable, *args, **kwds)
949
assertRaisesRegex(exception, regex, msg=None)
951
Like :meth:`assertRaises` but also tests that *regex* matches
952
on the string representation of the raised exception. *regex* may be
953
a regular expression object or a string containing a regular expression
954
suitable for use by :func:`re.search`. Examples::
956
self.assertRaisesRegex(ValueError, "invalid literal for.*XYZ'$",
961
with self.assertRaisesRegex(ValueError, 'literal'):
964
.. versionadded:: 3.1
965
under the name ``assertRaisesRegexp``.
967
.. versionchanged:: 3.2
968
Renamed to :meth:`assertRaisesRegex`.
970
.. versionchanged:: 3.3
971
Added the *msg* keyword argument when used as a context manager.
974
.. method:: assertWarns(warning, callable, *args, **kwds)
975
assertWarns(warning, msg=None)
977
Test that a warning is triggered when *callable* is called with any
978
positional or keyword arguments that are also passed to
979
:meth:`assertWarns`. The test passes if *warning* is triggered and
980
fails if it isn't. Any exception is an error.
981
To catch any of a group of warnings, a tuple containing the warning
982
classes may be passed as *warnings*.
984
If only the *warning* and possibly the *msg* arguments are given,
985
return a context manager so that the code under test can be written
986
inline rather than as a function::
988
with self.assertWarns(SomeWarning):
991
When used as a context manager, :meth:`assertWarns` accepts the
992
additional keyword argument *msg*.
994
The context manager will store the caught warning object in its
995
:attr:`warning` attribute, and the source line which triggered the
996
warnings in the :attr:`filename` and :attr:`lineno` attributes.
997
This can be useful if the intention is to perform additional checks
998
on the warning caught::
1000
with self.assertWarns(SomeWarning) as cm:
1003
self.assertIn('myfile.py', cm.filename)
1004
self.assertEqual(320, cm.lineno)
1006
This method works regardless of the warning filters in place when it
1009
.. versionadded:: 3.2
1011
.. versionchanged:: 3.3
1012
Added the *msg* keyword argument when used as a context manager.
1015
.. method:: assertWarnsRegex(warning, regex, callable, *args, **kwds)
1016
assertWarnsRegex(warning, regex, msg=None)
1018
Like :meth:`assertWarns` but also tests that *regex* matches on the
1019
message of the triggered warning. *regex* may be a regular expression
1020
object or a string containing a regular expression suitable for use
1021
by :func:`re.search`. Example::
1023
self.assertWarnsRegex(DeprecationWarning,
1024
r'legacy_function\(\) is deprecated',
1025
legacy_function, 'XYZ')
1029
with self.assertWarnsRegex(RuntimeWarning, 'unsafe frobnicating'):
1030
frobnicate('/etc/passwd')
1032
.. versionadded:: 3.2
1034
.. versionchanged:: 3.3
1035
Added the *msg* keyword argument when used as a context manager.
1037
.. method:: assertLogs(logger=None, level=None)
1039
A context manager to test that at least one message is logged on
1040
the *logger* or one of its children, with at least the given
1043
If given, *logger* should be a :class:`logging.Logger` object or a
1044
:class:`str` giving the name of a logger. The default is the root
1045
logger, which will catch all messages.
1047
If given, *level* should be either a numeric logging level or
1048
its string equivalent (for example either ``"ERROR"`` or
1049
:attr:`logging.ERROR`). The default is :attr:`logging.INFO`.
1051
The test passes if at least one message emitted inside the ``with``
1052
block matches the *logger* and *level* conditions, otherwise it fails.
1054
The object returned by the context manager is a recording helper
1055
which keeps tracks of the matching log messages. It has two
1058
.. attribute:: records
1060
A list of :class:`logging.LogRecord` objects of the matching
1063
.. attribute:: output
1065
A list of :class:`str` objects with the formatted output of
1070
with self.assertLogs('foo', level='INFO') as cm:
1071
logging.getLogger('foo').info('first message')
1072
logging.getLogger('foo.bar').error('second message')
1073
self.assertEqual(cm.output, ['INFO:foo:first message',
1074
'ERROR:foo.bar:second message'])
1076
.. versionadded:: 3.4
1079
There are also other methods used to perform more specific checks, such as:
1081
+---------------------------------------+--------------------------------+--------------+
1082
| Method | Checks that | New in |
1083
+=======================================+================================+==============+
1084
| :meth:`assertAlmostEqual(a, b) | ``round(a-b, 7) == 0`` | |
1085
| <TestCase.assertAlmostEqual>` | | |
1086
+---------------------------------------+--------------------------------+--------------+
1087
| :meth:`assertNotAlmostEqual(a, b) | ``round(a-b, 7) != 0`` | |
1088
| <TestCase.assertNotAlmostEqual>` | | |
1089
+---------------------------------------+--------------------------------+--------------+
1090
| :meth:`assertGreater(a, b) | ``a > b`` | 3.1 |
1091
| <TestCase.assertGreater>` | | |
1092
+---------------------------------------+--------------------------------+--------------+
1093
| :meth:`assertGreaterEqual(a, b) | ``a >= b`` | 3.1 |
1094
| <TestCase.assertGreaterEqual>` | | |
1095
+---------------------------------------+--------------------------------+--------------+
1096
| :meth:`assertLess(a, b) | ``a < b`` | 3.1 |
1097
| <TestCase.assertLess>` | | |
1098
+---------------------------------------+--------------------------------+--------------+
1099
| :meth:`assertLessEqual(a, b) | ``a <= b`` | 3.1 |
1100
| <TestCase.assertLessEqual>` | | |
1101
+---------------------------------------+--------------------------------+--------------+
1102
| :meth:`assertRegex(s, r) | ``r.search(s)`` | 3.1 |
1103
| <TestCase.assertRegex>` | | |
1104
+---------------------------------------+--------------------------------+--------------+
1105
| :meth:`assertNotRegex(s, r) | ``not r.search(s)`` | 3.2 |
1106
| <TestCase.assertNotRegex>` | | |
1107
+---------------------------------------+--------------------------------+--------------+
1108
| :meth:`assertCountEqual(a, b) | *a* and *b* have the same | 3.2 |
1109
| <TestCase.assertCountEqual>` | elements in the same number, | |
1110
| | regardless of their order | |
1111
+---------------------------------------+--------------------------------+--------------+
1114
.. method:: assertAlmostEqual(first, second, places=7, msg=None, delta=None)
1115
assertNotAlmostEqual(first, second, places=7, msg=None, delta=None)
1117
Test that *first* and *second* are approximately (or not approximately)
1118
equal by computing the difference, rounding to the given number of
1119
decimal *places* (default 7), and comparing to zero. Note that these
1120
methods round the values to the given number of *decimal places* (i.e.
1121
like the :func:`round` function) and not *significant digits*.
1123
If *delta* is supplied instead of *places* then the difference
1124
between *first* and *second* must be less or equal to (or greater than) *delta*.
1126
Supplying both *delta* and *places* raises a ``TypeError``.
1128
.. versionchanged:: 3.2
1129
:meth:`assertAlmostEqual` automatically considers almost equal objects
1130
that compare equal. :meth:`assertNotAlmostEqual` automatically fails
1131
if the objects compare equal. Added the *delta* keyword argument.
1134
.. method:: assertGreater(first, second, msg=None)
1135
assertGreaterEqual(first, second, msg=None)
1136
assertLess(first, second, msg=None)
1137
assertLessEqual(first, second, msg=None)
1139
Test that *first* is respectively >, >=, < or <= than *second* depending
1140
on the method name. If not, the test will fail::
1142
>>> self.assertGreaterEqual(3, 4)
1143
AssertionError: "3" unexpectedly not greater than or equal to "4"
1145
.. versionadded:: 3.1
1148
.. method:: assertRegex(text, regex, msg=None)
1149
assertNotRegex(text, regex, msg=None)
1151
Test that a *regex* search matches (or does not match) *text*. In case
1152
of failure, the error message will include the pattern and the *text* (or
1153
the pattern and the part of *text* that unexpectedly matched). *regex*
1154
may be a regular expression object or a string containing a regular
1155
expression suitable for use by :func:`re.search`.
1157
.. versionadded:: 3.1
1158
under the name ``assertRegexpMatches``.
1159
.. versionchanged:: 3.2
1160
The method ``assertRegexpMatches()`` has been renamed to
1161
:meth:`.assertRegex`.
1162
.. versionadded:: 3.2
1163
:meth:`.assertNotRegex`.
1166
.. method:: assertCountEqual(first, second, msg=None)
1168
Test that sequence *first* contains the same elements as *second*,
1169
regardless of their order. When they don't, an error message listing the
1170
differences between the sequences will be generated.
1172
Duplicate elements are *not* ignored when comparing *first* and
1173
*second*. It verifies whether each element has the same count in both
1174
sequences. Equivalent to:
1175
``assertEqual(Counter(list(first)), Counter(list(second)))``
1176
but works with sequences of unhashable objects as well.
1178
.. versionadded:: 3.2
1181
.. _type-specific-methods:
1183
The :meth:`assertEqual` method dispatches the equality check for objects of
1184
the same type to different type-specific methods. These methods are already
1185
implemented for most of the built-in types, but it's also possible to
1186
register new methods using :meth:`addTypeEqualityFunc`:
1188
.. method:: addTypeEqualityFunc(typeobj, function)
1190
Registers a type-specific method called by :meth:`assertEqual` to check
1191
if two objects of exactly the same *typeobj* (not subclasses) compare
1192
equal. *function* must take two positional arguments and a third msg=None
1193
keyword argument just as :meth:`assertEqual` does. It must raise
1194
:data:`self.failureException(msg) <failureException>` when inequality
1195
between the first two parameters is detected -- possibly providing useful
1196
information and explaining the inequalities in details in the error
1199
.. versionadded:: 3.1
1201
The list of type-specific methods automatically used by
1202
:meth:`~TestCase.assertEqual` are summarized in the following table. Note
1203
that it's usually not necessary to invoke these methods directly.
1205
+-----------------------------------------+-----------------------------+--------------+
1206
| Method | Used to compare | New in |
1207
+=========================================+=============================+==============+
1208
| :meth:`assertMultiLineEqual(a, b) | strings | 3.1 |
1209
| <TestCase.assertMultiLineEqual>` | | |
1210
+-----------------------------------------+-----------------------------+--------------+
1211
| :meth:`assertSequenceEqual(a, b) | sequences | 3.1 |
1212
| <TestCase.assertSequenceEqual>` | | |
1213
+-----------------------------------------+-----------------------------+--------------+
1214
| :meth:`assertListEqual(a, b) | lists | 3.1 |
1215
| <TestCase.assertListEqual>` | | |
1216
+-----------------------------------------+-----------------------------+--------------+
1217
| :meth:`assertTupleEqual(a, b) | tuples | 3.1 |
1218
| <TestCase.assertTupleEqual>` | | |
1219
+-----------------------------------------+-----------------------------+--------------+
1220
| :meth:`assertSetEqual(a, b) | sets or frozensets | 3.1 |
1221
| <TestCase.assertSetEqual>` | | |
1222
+-----------------------------------------+-----------------------------+--------------+
1223
| :meth:`assertDictEqual(a, b) | dicts | 3.1 |
1224
| <TestCase.assertDictEqual>` | | |
1225
+-----------------------------------------+-----------------------------+--------------+
1229
.. method:: assertMultiLineEqual(first, second, msg=None)
1231
Test that the multiline string *first* is equal to the string *second*.
1232
When not equal a diff of the two strings highlighting the differences
1233
will be included in the error message. This method is used by default
1234
when comparing strings with :meth:`assertEqual`.
1236
.. versionadded:: 3.1
1239
.. method:: assertSequenceEqual(first, second, msg=None, seq_type=None)
1241
Tests that two sequences are equal. If a *seq_type* is supplied, both
1242
*first* and *second* must be instances of *seq_type* or a failure will
1243
be raised. If the sequences are different an error message is
1244
constructed that shows the difference between the two.
1246
This method is not called directly by :meth:`assertEqual`, but
1247
it's used to implement :meth:`assertListEqual` and
1248
:meth:`assertTupleEqual`.
1250
.. versionadded:: 3.1
1253
.. method:: assertListEqual(first, second, msg=None)
1254
assertTupleEqual(first, second, msg=None)
1256
Tests that two lists or tuples are equal. If not, an error message is
1257
constructed that shows only the differences between the two. An error
1258
is also raised if either of the parameters are of the wrong type.
1259
These methods are used by default when comparing lists or tuples with
1260
:meth:`assertEqual`.
1262
.. versionadded:: 3.1
1265
.. method:: assertSetEqual(first, second, msg=None)
1267
Tests that two sets are equal. If not, an error message is constructed
1268
that lists the differences between the sets. This method is used by
1269
default when comparing sets or frozensets with :meth:`assertEqual`.
1271
Fails if either of *first* or *second* does not have a :meth:`set.difference`
1274
.. versionadded:: 3.1
1277
.. method:: assertDictEqual(first, second, msg=None)
1279
Test that two dictionaries are equal. If not, an error message is
1280
constructed that shows the differences in the dictionaries. This
1281
method will be used by default to compare dictionaries in
1282
calls to :meth:`assertEqual`.
1284
.. versionadded:: 3.1
1288
.. _other-methods-and-attrs:
1290
Finally the :class:`TestCase` provides the following methods and attributes:
1293
.. method:: fail(msg=None)
1295
Signals a test failure unconditionally, with *msg* or ``None`` for
1299
.. attribute:: failureException
1301
This class attribute gives the exception raised by the test method. If a
1302
test framework needs to use a specialized exception, possibly to carry
1303
additional information, it must subclass this exception in order to "play
1304
fair" with the framework. The initial value of this attribute is
1305
:exc:`AssertionError`.
1308
.. attribute:: longMessage
1310
If set to ``True`` then any explicit failure message you pass in to the
1311
:ref:`assert methods <assert-methods>` will be appended to the end of the
1312
normal failure message. The normal messages contain useful information
1313
about the objects involved, for example the message from assertEqual
1314
shows you the repr of the two unequal objects. Setting this attribute
1315
to ``True`` allows you to have a custom error message in addition to the
1318
This attribute defaults to ``True``. If set to False then a custom message
1319
passed to an assert method will silence the normal message.
1321
The class setting can be overridden in individual tests by assigning an
1322
instance attribute to ``True`` or ``False`` before calling the assert methods.
1324
.. versionadded:: 3.1
1327
.. attribute:: maxDiff
1329
This attribute controls the maximum length of diffs output by assert
1330
methods that report diffs on failure. It defaults to 80*8 characters.
1331
Assert methods affected by this attribute are
1332
:meth:`assertSequenceEqual` (including all the sequence comparison
1333
methods that delegate to it), :meth:`assertDictEqual` and
1334
:meth:`assertMultiLineEqual`.
1336
Setting ``maxDiff`` to None means that there is no maximum length of
1339
.. versionadded:: 3.2
1342
Testing frameworks can use the following methods to collect information on
1346
.. method:: countTestCases()
1348
Return the number of tests represented by this test object. For
1349
:class:`TestCase` instances, this will always be ``1``.
1352
.. method:: defaultTestResult()
1354
Return an instance of the test result class that should be used for this
1355
test case class (if no other result instance is provided to the
1356
:meth:`run` method).
1358
For :class:`TestCase` instances, this will always be an instance of
1359
:class:`TestResult`; subclasses of :class:`TestCase` should override this
1365
Return a string identifying the specific test case. This is usually the
1366
full name of the test method, including the module and class name.
1369
.. method:: shortDescription()
1371
Returns a description of the test, or ``None`` if no description
1372
has been provided. The default implementation of this method
1373
returns the first line of the test method's docstring, if available,
1376
.. versionchanged:: 3.1
1377
In 3.1 this was changed to add the test name to the short description
1378
even in the presence of a docstring. This caused compatibility issues
1379
with unittest extensions and adding the test name was moved to the
1380
:class:`TextTestResult` in Python 3.2.
1383
.. method:: addCleanup(function, *args, **kwargs)
1385
Add a function to be called after :meth:`tearDown` to cleanup resources
1386
used during the test. Functions will be called in reverse order to the
1387
order they are added (LIFO). They are called with any arguments and
1388
keyword arguments passed into :meth:`addCleanup` when they are
1391
If :meth:`setUp` fails, meaning that :meth:`tearDown` is not called,
1392
then any cleanup functions added will still be called.
1394
.. versionadded:: 3.1
1397
.. method:: doCleanups()
1399
This method is called unconditionally after :meth:`tearDown`, or
1400
after :meth:`setUp` if :meth:`setUp` raises an exception.
1402
It is responsible for calling all the cleanup functions added by
1403
:meth:`addCleanup`. If you need cleanup functions to be called
1404
*prior* to :meth:`tearDown` then you can call :meth:`doCleanups`
1407
:meth:`doCleanups` pops methods off the stack of cleanup
1408
functions one at a time, so it can be called at any time.
1410
.. versionadded:: 3.1
1413
.. class:: FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)
1415
This class implements the portion of the :class:`TestCase` interface which
1416
allows the test runner to drive the test, but does not provide the methods
1417
which test code can use to check and report errors. This is used to create
1418
test cases using legacy test code, allowing it to be integrated into a
1419
:mod:`unittest`-based test framework.
1422
.. _deprecated-aliases:
1427
For historical reasons, some of the :class:`TestCase` methods had one or more
1428
aliases that are now deprecated. The following table lists the correct names
1429
along with their deprecated aliases:
1431
============================== ====================== ======================
1432
Method Name Deprecated alias Deprecated alias
1433
============================== ====================== ======================
1434
:meth:`.assertEqual` failUnlessEqual assertEquals
1435
:meth:`.assertNotEqual` failIfEqual assertNotEquals
1436
:meth:`.assertTrue` failUnless assert\_
1437
:meth:`.assertFalse` failIf
1438
:meth:`.assertRaises` failUnlessRaises
1439
:meth:`.assertAlmostEqual` failUnlessAlmostEqual assertAlmostEquals
1440
:meth:`.assertNotAlmostEqual` failIfAlmostEqual assertNotAlmostEquals
1441
:meth:`.assertRegex` assertRegexpMatches
1442
:meth:`.assertRaisesRegex` assertRaisesRegexp
1443
============================== ====================== ======================
1446
the fail* aliases listed in the second column.
1448
the assert* aliases listed in the third column.
1450
``assertRegexpMatches`` and ``assertRaisesRegexp`` have been renamed to
1451
:meth:`.assertRegex` and :meth:`.assertRaisesRegex`
1454
.. _testsuite-objects:
1459
.. class:: TestSuite(tests=())
1461
This class represents an aggregation of individual tests cases and test suites.
1462
The class presents the interface needed by the test runner to allow it to be run
1463
as any other test case. Running a :class:`TestSuite` instance is the same as
1464
iterating over the suite, running each test individually.
1466
If *tests* is given, it must be an iterable of individual test cases or other
1467
test suites that will be used to build the suite initially. Additional methods
1468
are provided to add test cases and suites to the collection later on.
1470
:class:`TestSuite` objects behave much like :class:`TestCase` objects, except
1471
they do not actually implement a test. Instead, they are used to aggregate
1472
tests into groups of tests that should be run together. Some additional
1473
methods are available to add tests to :class:`TestSuite` instances:
1476
.. method:: TestSuite.addTest(test)
1478
Add a :class:`TestCase` or :class:`TestSuite` to the suite.
1481
.. method:: TestSuite.addTests(tests)
1483
Add all the tests from an iterable of :class:`TestCase` and :class:`TestSuite`
1484
instances to this test suite.
1486
This is equivalent to iterating over *tests*, calling :meth:`addTest` for
1489
:class:`TestSuite` shares the following methods with :class:`TestCase`:
1492
.. method:: run(result)
1494
Run the tests associated with this suite, collecting the result into the
1495
test result object passed as *result*. Note that unlike
1496
:meth:`TestCase.run`, :meth:`TestSuite.run` requires the result object to
1502
Run the tests associated with this suite without collecting the
1503
result. This allows exceptions raised by the test to be propagated to the
1504
caller and can be used to support running tests under a debugger.
1507
.. method:: countTestCases()
1509
Return the number of tests represented by this test object, including all
1510
individual tests and sub-suites.
1513
.. method:: __iter__()
1515
Tests grouped by a :class:`TestSuite` are always accessed by iteration.
1516
Subclasses can lazily provide tests by overriding :meth:`__iter__`. Note
1517
that this method may be called several times on a single suite (for
1518
example when counting tests or comparing for equality) so the tests
1519
returned by repeated iterations before :meth:`TestSuite.run` must be the
1520
same for each call iteration. After :meth:`TestSuite.run`, callers should
1521
not rely on the tests returned by this method unless the caller uses a
1522
subclass that overrides :meth:`TestSuite._removeTestAtIndex` to preserve
1525
.. versionchanged:: 3.2
1526
In earlier versions the :class:`TestSuite` accessed tests directly rather
1527
than through iteration, so overriding :meth:`__iter__` wasn't sufficient
1528
for providing tests.
1530
.. versionchanged:: 3.4
1531
In earlier versions the :class:`TestSuite` held references to each
1532
:class:`TestCase` after :meth:`TestSuite.run`. Subclasses can restore
1533
that behavior by overriding :meth:`TestSuite._removeTestAtIndex`.
1535
In the typical usage of a :class:`TestSuite` object, the :meth:`run` method
1536
is invoked by a :class:`TestRunner` rather than by the end-user test harness.
1539
Loading and running tests
1540
~~~~~~~~~~~~~~~~~~~~~~~~~
1542
.. class:: TestLoader()
1544
The :class:`TestLoader` class is used to create test suites from classes and
1545
modules. Normally, there is no need to create an instance of this class; the
1546
:mod:`unittest` module provides an instance that can be shared as
1547
:data:`unittest.defaultTestLoader`. Using a subclass or instance, however,
1548
allows customization of some configurable properties.
1550
:class:`TestLoader` objects have the following methods:
1553
.. method:: loadTestsFromTestCase(testCaseClass)
1555
Return a suite of all tests cases contained in the :class:`TestCase`\ -derived
1556
:class:`testCaseClass`.
1559
.. method:: loadTestsFromModule(module)
1561
Return a suite of all tests cases contained in the given module. This
1562
method searches *module* for classes derived from :class:`TestCase` and
1563
creates an instance of the class for each test method defined for the
1568
While using a hierarchy of :class:`TestCase`\ -derived classes can be
1569
convenient in sharing fixtures and helper functions, defining test
1570
methods on base classes that are not intended to be instantiated
1571
directly does not play well with this method. Doing so, however, can
1572
be useful when the fixtures are different and defined in subclasses.
1574
If a module provides a ``load_tests`` function it will be called to
1575
load the tests. This allows modules to customize test loading.
1576
This is the `load_tests protocol`_.
1578
.. versionchanged:: 3.2
1579
Support for ``load_tests`` added.
1582
.. method:: loadTestsFromName(name, module=None)
1584
Return a suite of all tests cases given a string specifier.
1586
The specifier *name* is a "dotted name" that may resolve either to a
1587
module, a test case class, a test method within a test case class, a
1588
:class:`TestSuite` instance, or a callable object which returns a
1589
:class:`TestCase` or :class:`TestSuite` instance. These checks are
1590
applied in the order listed here; that is, a method on a possible test
1591
case class will be picked up as "a test method within a test case class",
1592
rather than "a callable object".
1594
For example, if you have a module :mod:`SampleTests` containing a
1595
:class:`TestCase`\ -derived class :class:`SampleTestCase` with three test
1596
methods (:meth:`test_one`, :meth:`test_two`, and :meth:`test_three`), the
1597
specifier ``'SampleTests.SampleTestCase'`` would cause this method to
1598
return a suite which will run all three test methods. Using the specifier
1599
``'SampleTests.SampleTestCase.test_two'`` would cause it to return a test
1600
suite which will run only the :meth:`test_two` test method. The specifier
1601
can refer to modules and packages which have not been imported; they will
1602
be imported as a side-effect.
1604
The method optionally resolves *name* relative to the given *module*.
1607
.. method:: loadTestsFromNames(names, module=None)
1609
Similar to :meth:`loadTestsFromName`, but takes a sequence of names rather
1610
than a single name. The return value is a test suite which supports all
1611
the tests defined for each name.
1614
.. method:: getTestCaseNames(testCaseClass)
1616
Return a sorted sequence of method names found within *testCaseClass*;
1617
this should be a subclass of :class:`TestCase`.
1620
.. method:: discover(start_dir, pattern='test*.py', top_level_dir=None)
1622
Find and return all test modules from the specified start directory,
1623
recursing into subdirectories to find them. Only test files that match
1624
*pattern* will be loaded. (Using shell style pattern matching.) Only
1625
module names that are importable (i.e. are valid Python identifiers) will
1628
All test modules must be importable from the top level of the project. If
1629
the start directory is not the top level directory then the top level
1630
directory must be specified separately.
1632
If importing a module fails, for example due to a syntax error, then this
1633
will be recorded as a single error and discovery will continue. If the
1634
import failure is due to :exc:`SkipTest` being raised, it will be recorded
1635
as a skip instead of an error.
1637
If a test package name (directory with :file:`__init__.py`) matches the
1638
pattern then the package will be checked for a ``load_tests``
1639
function. If this exists then it will be called with *loader*, *tests*,
1642
If load_tests exists then discovery does *not* recurse into the package,
1643
``load_tests`` is responsible for loading all tests in the package.
1645
The pattern is deliberately not stored as a loader attribute so that
1646
packages can continue discovery themselves. *top_level_dir* is stored so
1647
``load_tests`` does not need to pass this argument in to
1648
``loader.discover()``.
1650
*start_dir* can be a dotted module name as well as a directory.
1652
.. versionadded:: 3.2
1654
.. versionchanged:: 3.4
1655
Modules that raise :exc:`SkipTest` on import are recorded as skips,
1658
.. versionchanged:: 3.4
1659
Paths are sorted before being imported to ensure execution order for a
1660
given test suite is the same even if the underlying file system's ordering
1661
is not dependent on file name like in ext3/4.
1664
The following attributes of a :class:`TestLoader` can be configured either by
1665
subclassing or assignment on an instance:
1668
.. attribute:: testMethodPrefix
1670
String giving the prefix of method names which will be interpreted as test
1671
methods. The default value is ``'test'``.
1673
This affects :meth:`getTestCaseNames` and all the :meth:`loadTestsFrom\*`
1677
.. attribute:: sortTestMethodsUsing
1679
Function to be used to compare method names when sorting them in
1680
:meth:`getTestCaseNames` and all the :meth:`loadTestsFrom\*` methods.
1683
.. attribute:: suiteClass
1685
Callable object that constructs a test suite from a list of tests. No
1686
methods on the resulting object are needed. The default value is the
1687
:class:`TestSuite` class.
1689
This affects all the :meth:`loadTestsFrom\*` methods.
1692
.. class:: TestResult
1694
This class is used to compile information about which tests have succeeded
1695
and which have failed.
1697
A :class:`TestResult` object stores the results of a set of tests. The
1698
:class:`TestCase` and :class:`TestSuite` classes ensure that results are
1699
properly recorded; test authors do not need to worry about recording the
1702
Testing frameworks built on top of :mod:`unittest` may want access to the
1703
:class:`TestResult` object generated by running a set of tests for reporting
1704
purposes; a :class:`TestResult` instance is returned by the
1705
:meth:`TestRunner.run` method for this purpose.
1707
:class:`TestResult` instances have the following attributes that will be of
1708
interest when inspecting the results of running a set of tests:
1711
.. attribute:: errors
1713
A list containing 2-tuples of :class:`TestCase` instances and strings
1714
holding formatted tracebacks. Each tuple represents a test which raised an
1715
unexpected exception.
1717
.. attribute:: failures
1719
A list containing 2-tuples of :class:`TestCase` instances and strings
1720
holding formatted tracebacks. Each tuple represents a test where a failure
1721
was explicitly signalled using the :meth:`TestCase.assert\*` methods.
1723
.. attribute:: skipped
1725
A list containing 2-tuples of :class:`TestCase` instances and strings
1726
holding the reason for skipping the test.
1728
.. versionadded:: 3.1
1730
.. attribute:: expectedFailures
1732
A list containing 2-tuples of :class:`TestCase` instances and strings
1733
holding formatted tracebacks. Each tuple represents an expected failure
1736
.. attribute:: unexpectedSuccesses
1738
A list containing :class:`TestCase` instances that were marked as expected
1739
failures, but succeeded.
1741
.. attribute:: shouldStop
1743
Set to ``True`` when the execution of tests should stop by :meth:`stop`.
1746
.. attribute:: testsRun
1748
The total number of tests run so far.
1751
.. attribute:: buffer
1753
If set to true, ``sys.stdout`` and ``sys.stderr`` will be buffered in between
1754
:meth:`startTest` and :meth:`stopTest` being called. Collected output will
1755
only be echoed onto the real ``sys.stdout`` and ``sys.stderr`` if the test
1756
fails or errors. Any output is also attached to the failure / error message.
1758
.. versionadded:: 3.2
1761
.. attribute:: failfast
1763
If set to true :meth:`stop` will be called on the first failure or error,
1764
halting the test run.
1766
.. versionadded:: 3.2
1769
.. method:: wasSuccessful()
1771
Return ``True`` if all tests run so far have passed, otherwise returns
1777
This method can be called to signal that the set of tests being run should
1778
be aborted by setting the :attr:`shouldStop` attribute to ``True``.
1779
:class:`TestRunner` objects should respect this flag and return without
1780
running any additional tests.
1782
For example, this feature is used by the :class:`TextTestRunner` class to
1783
stop the test framework when the user signals an interrupt from the
1784
keyboard. Interactive tools which provide :class:`TestRunner`
1785
implementations can use this in a similar manner.
1787
The following methods of the :class:`TestResult` class are used to maintain
1788
the internal data structures, and may be extended in subclasses to support
1789
additional reporting requirements. This is particularly useful in building
1790
tools which support interactive reporting while tests are being run.
1793
.. method:: startTest(test)
1795
Called when the test case *test* is about to be run.
1797
.. method:: stopTest(test)
1799
Called after the test case *test* has been executed, regardless of the
1802
.. method:: startTestRun(test)
1804
Called once before any tests are executed.
1806
.. versionadded:: 3.1
1809
.. method:: stopTestRun(test)
1811
Called once after all tests are executed.
1813
.. versionadded:: 3.1
1816
.. method:: addError(test, err)
1818
Called when the test case *test* raises an unexpected exception. *err* is a
1819
tuple of the form returned by :func:`sys.exc_info`: ``(type, value,
1822
The default implementation appends a tuple ``(test, formatted_err)`` to
1823
the instance's :attr:`errors` attribute, where *formatted_err* is a
1824
formatted traceback derived from *err*.
1827
.. method:: addFailure(test, err)
1829
Called when the test case *test* signals a failure. *err* is a tuple of
1830
the form returned by :func:`sys.exc_info`: ``(type, value, traceback)``.
1832
The default implementation appends a tuple ``(test, formatted_err)`` to
1833
the instance's :attr:`failures` attribute, where *formatted_err* is a
1834
formatted traceback derived from *err*.
1837
.. method:: addSuccess(test)
1839
Called when the test case *test* succeeds.
1841
The default implementation does nothing.
1844
.. method:: addSkip(test, reason)
1846
Called when the test case *test* is skipped. *reason* is the reason the
1847
test gave for skipping.
1849
The default implementation appends a tuple ``(test, reason)`` to the
1850
instance's :attr:`skipped` attribute.
1853
.. method:: addExpectedFailure(test, err)
1855
Called when the test case *test* fails, but was marked with the
1856
:func:`expectedFailure` decorator.
1858
The default implementation appends a tuple ``(test, formatted_err)`` to
1859
the instance's :attr:`expectedFailures` attribute, where *formatted_err*
1860
is a formatted traceback derived from *err*.
1863
.. method:: addUnexpectedSuccess(test)
1865
Called when the test case *test* was marked with the
1866
:func:`expectedFailure` decorator, but succeeded.
1868
The default implementation appends the test to the instance's
1869
:attr:`unexpectedSuccesses` attribute.
1872
.. method:: addSubTest(test, subtest, outcome)
1874
Called when a subtest finishes. *test* is the test case
1875
corresponding to the test method. *subtest* is a custom
1876
:class:`TestCase` instance describing the subtest.
1878
If *outcome* is :const:`None`, the subtest succeeded. Otherwise,
1879
it failed with an exception where *outcome* is a tuple of the form
1880
returned by :func:`sys.exc_info`: ``(type, value, traceback)``.
1882
The default implementation does nothing when the outcome is a
1883
success, and records subtest failures as normal failures.
1885
.. versionadded:: 3.4
1888
.. class:: TextTestResult(stream, descriptions, verbosity)
1890
A concrete implementation of :class:`TestResult` used by the
1891
:class:`TextTestRunner`.
1893
.. versionadded:: 3.2
1894
This class was previously named ``_TextTestResult``. The old name still
1895
exists as an alias but is deprecated.
1898
.. data:: defaultTestLoader
1900
Instance of the :class:`TestLoader` class intended to be shared. If no
1901
customization of the :class:`TestLoader` is needed, this instance can be used
1902
instead of repeatedly creating new instances.
1905
.. class:: TextTestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, \
1906
buffer=False, resultclass=None, warnings=None)
1908
A basic test runner implementation that outputs results to a stream. If *stream*
1909
is ``None``, the default, :data:`sys.stderr` is used as the output stream. This class
1910
has a few configurable parameters, but is essentially very simple. Graphical
1911
applications which run test suites should provide alternate implementations.
1913
By default this runner shows :exc:`DeprecationWarning`,
1914
:exc:`PendingDeprecationWarning`, and :exc:`ImportWarning` even if they are
1915
:ref:`ignored by default <warning-ignored>`. Deprecation warnings caused by
1916
:ref:`deprecated unittest methods <deprecated-aliases>` are also
1917
special-cased and, when the warning filters are ``'default'`` or ``'always'``,
1918
they will appear only once per-module, in order to avoid too many warning
1919
messages. This behavior can be overridden using the :option:`-Wd` or
1920
:option:`-Wa` options and leaving *warnings* to ``None``.
1922
.. versionchanged:: 3.2
1923
Added the ``warnings`` argument.
1925
.. versionchanged:: 3.2
1926
The default stream is set to :data:`sys.stderr` at instantiation time rather
1929
.. method:: _makeResult()
1931
This method returns the instance of ``TestResult`` used by :meth:`run`.
1932
It is not intended to be called directly, but can be overridden in
1933
subclasses to provide a custom ``TestResult``.
1935
``_makeResult()`` instantiates the class or callable passed in the
1936
``TextTestRunner`` constructor as the ``resultclass`` argument. It
1937
defaults to :class:`TextTestResult` if no ``resultclass`` is provided.
1938
The result class is instantiated with the following arguments::
1940
stream, descriptions, verbosity
1943
.. function:: main(module='__main__', defaultTest=None, argv=None, testRunner=None, \
1944
testLoader=unittest.defaultTestLoader, exit=True, verbosity=1, \
1945
failfast=None, catchbreak=None, buffer=None, warnings=None)
1947
A command-line program that loads a set of tests from *module* and runs them;
1948
this is primarily for making test modules conveniently executable.
1949
The simplest use for this function is to include the following line at the
1950
end of a test script::
1952
if __name__ == '__main__':
1955
You can run tests with more detailed information by passing in the verbosity
1958
if __name__ == '__main__':
1959
unittest.main(verbosity=2)
1961
The *argv* argument can be a list of options passed to the program, with the
1962
first element being the program name. If not specified or ``None``,
1963
the values of :data:`sys.argv` are used.
1965
The *testRunner* argument can either be a test runner class or an already
1966
created instance of it. By default ``main`` calls :func:`sys.exit` with
1967
an exit code indicating success or failure of the tests run.
1969
The *testLoader* argument has to be a :class:`TestLoader` instance,
1970
and defaults to :data:`defaultTestLoader`.
1972
``main`` supports being used from the interactive interpreter by passing in the
1973
argument ``exit=False``. This displays the result on standard output without
1974
calling :func:`sys.exit`::
1976
>>> from unittest import main
1977
>>> main(module='test_module', exit=False)
1979
The *failfast*, *catchbreak* and *buffer* parameters have the same
1980
effect as the same-name `command-line options`_.
1982
The *warning* argument specifies the :ref:`warning filter <warning-filter>`
1983
that should be used while running the tests. If it's not specified, it will
1984
remain ``None`` if a :option:`-W` option is passed to :program:`python`,
1985
otherwise it will be set to ``'default'``.
1987
Calling ``main`` actually returns an instance of the ``TestProgram`` class.
1988
This stores the result of the tests run as the ``result`` attribute.
1990
.. versionchanged:: 3.1
1991
The *exit* parameter was added.
1993
.. versionchanged:: 3.2
1994
The *verbosity*, *failfast*, *catchbreak*, *buffer*
1995
and *warnings* parameters were added.
1997
.. versionchanged:: 3.4
1998
The *defaultTest* parameter was changed to also accept an iterable of
2005
.. versionadded:: 3.2
2007
Modules or packages can customize how tests are loaded from them during normal
2008
test runs or test discovery by implementing a function called ``load_tests``.
2010
If a test module defines ``load_tests`` it will be called by
2011
:meth:`TestLoader.loadTestsFromModule` with the following arguments::
2013
load_tests(loader, standard_tests, None)
2015
It should return a :class:`TestSuite`.
2017
*loader* is the instance of :class:`TestLoader` doing the loading.
2018
*standard_tests* are the tests that would be loaded by default from the
2019
module. It is common for test modules to only want to add or remove tests
2020
from the standard set of tests.
2021
The third argument is used when loading packages as part of test discovery.
2023
A typical ``load_tests`` function that loads tests from a specific set of
2024
:class:`TestCase` classes may look like::
2026
test_cases = (TestCase1, TestCase2, TestCase3)
2028
def load_tests(loader, tests, pattern):
2030
for test_class in test_cases:
2031
tests = loader.loadTestsFromTestCase(test_class)
2032
suite.addTests(tests)
2035
If discovery is started, either from the command line or by calling
2036
:meth:`TestLoader.discover`, with a pattern that matches a package
2037
name then the package :file:`__init__.py` will be checked for ``load_tests``.
2041
The default pattern is ``'test*.py'``. This matches all Python files
2042
that start with ``'test'`` but *won't* match any test directories.
2044
A pattern like ``'test*'`` will match test packages as well as
2047
If the package :file:`__init__.py` defines ``load_tests`` then it will be
2048
called and discovery not continued into the package. ``load_tests``
2049
is called with the following arguments::
2051
load_tests(loader, standard_tests, pattern)
2053
This should return a :class:`TestSuite` representing all the tests
2054
from the package. (``standard_tests`` will only contain tests
2055
collected from :file:`__init__.py`.)
2057
Because the pattern is passed into ``load_tests`` the package is free to
2058
continue (and potentially modify) test discovery. A 'do nothing'
2059
``load_tests`` function for a test package would look like::
2061
def load_tests(loader, standard_tests, pattern):
2062
# top level directory cached on loader instance
2063
this_dir = os.path.dirname(__file__)
2064
package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
2065
standard_tests.addTests(package_tests)
2066
return standard_tests
2069
Class and Module Fixtures
2070
-------------------------
2072
Class and module level fixtures are implemented in :class:`TestSuite`. When
2073
the test suite encounters a test from a new class then :meth:`tearDownClass`
2074
from the previous class (if there is one) is called, followed by
2075
:meth:`setUpClass` from the new class.
2077
Similarly if a test is from a different module from the previous test then
2078
``tearDownModule`` from the previous module is run, followed by
2079
``setUpModule`` from the new module.
2081
After all the tests have run the final ``tearDownClass`` and
2082
``tearDownModule`` are run.
2084
Note that shared fixtures do not play well with [potential] features like test
2085
parallelization and they break test isolation. They should be used with care.
2087
The default ordering of tests created by the unittest test loaders is to group
2088
all tests from the same modules and classes together. This will lead to
2089
``setUpClass`` / ``setUpModule`` (etc) being called exactly once per class and
2090
module. If you randomize the order, so that tests from different modules and
2091
classes are adjacent to each other, then these shared fixture functions may be
2092
called multiple times in a single test run.
2094
Shared fixtures are not intended to work with suites with non-standard
2095
ordering. A ``BaseTestSuite`` still exists for frameworks that don't want to
2096
support shared fixtures.
2098
If there are any exceptions raised during one of the shared fixture functions
2099
the test is reported as an error. Because there is no corresponding test
2100
instance an ``_ErrorHolder`` object (that has the same interface as a
2101
:class:`TestCase`) is created to represent the error. If you are just using
2102
the standard unittest test runner then this detail doesn't matter, but if you
2103
are a framework author it may be relevant.
2106
setUpClass and tearDownClass
2107
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2109
These must be implemented as class methods::
2113
class Test(unittest.TestCase):
2115
def setUpClass(cls):
2116
cls._connection = createExpensiveConnectionObject()
2119
def tearDownClass(cls):
2120
cls._connection.destroy()
2122
If you want the ``setUpClass`` and ``tearDownClass`` on base classes called
2123
then you must call up to them yourself. The implementations in
2124
:class:`TestCase` are empty.
2126
If an exception is raised during a ``setUpClass`` then the tests in the class
2127
are not run and the ``tearDownClass`` is not run. Skipped classes will not
2128
have ``setUpClass`` or ``tearDownClass`` run. If the exception is a
2129
:exc:`SkipTest` exception then the class will be reported as having been skipped
2130
instead of as an error.
2133
setUpModule and tearDownModule
2134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2136
These should be implemented as functions::
2141
def tearDownModule():
2144
If an exception is raised in a ``setUpModule`` then none of the tests in the
2145
module will be run and the ``tearDownModule`` will not be run. If the exception is a
2146
:exc:`SkipTest` exception then the module will be reported as having been skipped
2147
instead of as an error.
2153
.. versionadded:: 3.2
2155
The :option:`-c/--catch <unittest -c>` command-line option to unittest,
2156
along with the ``catchbreak`` parameter to :func:`unittest.main()`, provide
2157
more friendly handling of control-C during a test run. With catch break
2158
behavior enabled control-C will allow the currently running test to complete,
2159
and the test run will then end and report all the results so far. A second
2160
control-c will raise a :exc:`KeyboardInterrupt` in the usual way.
2162
The control-c handling signal handler attempts to remain compatible with code or
2163
tests that install their own :const:`signal.SIGINT` handler. If the ``unittest``
2164
handler is called but *isn't* the installed :const:`signal.SIGINT` handler,
2165
i.e. it has been replaced by the system under test and delegated to, then it
2166
calls the default handler. This will normally be the expected behavior by code
2167
that replaces an installed handler and delegates to it. For individual tests
2168
that need ``unittest`` control-c handling disabled the :func:`removeHandler`
2169
decorator can be used.
2171
There are a few utility functions for framework authors to enable control-c
2172
handling functionality within test frameworks.
2174
.. function:: installHandler()
2176
Install the control-c handler. When a :const:`signal.SIGINT` is received
2177
(usually in response to the user pressing control-c) all registered results
2178
have :meth:`~TestResult.stop` called.
2181
.. function:: registerResult(result)
2183
Register a :class:`TestResult` object for control-c handling. Registering a
2184
result stores a weak reference to it, so it doesn't prevent the result from
2185
being garbage collected.
2187
Registering a :class:`TestResult` object has no side-effects if control-c
2188
handling is not enabled, so test frameworks can unconditionally register
2189
all results they create independently of whether or not handling is enabled.
2192
.. function:: removeResult(result)
2194
Remove a registered result. Once a result has been removed then
2195
:meth:`~TestResult.stop` will no longer be called on that result object in
2196
response to a control-c.
2199
.. function:: removeHandler(function=None)
2201
When called without arguments this function removes the control-c handler
2202
if it has been installed. This function can also be used as a test decorator
2203
to temporarily remove the handler whilst the test is being executed::
2205
@unittest.removeHandler
2206
def test_signal_handling(self):