blob: 3d8a84db38e1803ce4f63d7077e368fbeebc5dc9 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`unittest` --- Unit testing framework
2==========================================
3
4.. module:: unittest
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>
10
Georg Brandl8ec7f652007-08-15 14:28:01 +000011.. versionadded:: 2.1
Benjamin Peterson692428e2009-03-23 21:50:21 +000012
Ezio Melottidd7c5932011-03-10 23:00:48 +020013(If you are already familiar with the basic concepts of testing, you might want
14to skip to :ref:`the list of assert methods <assert-methods>`.)
15
Georg Brandl8ec7f652007-08-15 14:28:01 +000016The Python unit testing framework, sometimes referred to as "PyUnit," is a
17Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in
18turn, a Java version of Kent's Smalltalk testing framework. Each is the de
19facto standard unit testing framework for its respective language.
20
21:mod:`unittest` supports test automation, sharing of setup and shutdown code for
22tests, aggregation of tests into collections, and independence of the tests from
23the reporting framework. The :mod:`unittest` module provides classes that make
24it easy to support these qualities for a set of tests.
25
26To achieve this, :mod:`unittest` supports some important concepts:
27
28test fixture
29 A :dfn:`test fixture` represents the preparation needed to perform one or more
30 tests, and any associate cleanup actions. This may involve, for example,
31 creating temporary or proxy databases, directories, or starting a server
32 process.
33
34test case
35 A :dfn:`test case` is the smallest unit of testing. It checks for a specific
36 response to a particular set of inputs. :mod:`unittest` provides a base class,
37 :class:`TestCase`, which may be used to create new test cases.
38
39test suite
40 A :dfn:`test suite` is a collection of test cases, test suites, or both. It is
41 used to aggregate tests that should be executed together.
42
43test runner
44 A :dfn:`test runner` is a component which orchestrates the execution of tests
45 and provides the outcome to the user. The runner may use a graphical interface,
46 a textual interface, or return a special value to indicate the results of
47 executing the tests.
48
49The test case and test fixture concepts are supported through the
50:class:`TestCase` and :class:`FunctionTestCase` classes; the former should be
51used when creating new tests, and the latter can be used when integrating
52existing test code with a :mod:`unittest`\ -driven framework. When building test
Benjamin Peterson99721e02009-03-23 23:10:14 +000053fixtures using :class:`TestCase`, the :meth:`~TestCase.setUp` and
54:meth:`~TestCase.tearDown` methods can be overridden to provide initialization
55and cleanup for the fixture. With :class:`FunctionTestCase`, existing functions
56can be passed to the constructor for these purposes. When the test is run, the
57fixture initialization is run first; if it succeeds, the cleanup method is run
58after the test has been executed, regardless of the outcome of the test. Each
59instance of the :class:`TestCase` will only be used to run a single test method,
60so a new fixture is created for each test.
Georg Brandl8ec7f652007-08-15 14:28:01 +000061
62Test suites are implemented by the :class:`TestSuite` class. This class allows
63individual tests and test suites to be aggregated; when the suite is executed,
Benjamin Peterson176a56c2009-05-25 00:48:58 +000064all tests added directly to the suite and in "child" test suites are run.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
Benjamin Peterson99721e02009-03-23 23:10:14 +000066A test runner is an object that provides a single method,
67:meth:`~TestRunner.run`, which accepts a :class:`TestCase` or :class:`TestSuite`
68object as a parameter, and returns a result object. The class
69:class:`TestResult` is provided for use as the result object. :mod:`unittest`
70provides the :class:`TextTestRunner` as an example test runner which reports
71test results on the standard error stream by default. Alternate runners can be
72implemented for other environments (such as graphical environments) without any
73need to derive from a specific class.
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75
76.. seealso::
77
78 Module :mod:`doctest`
79 Another test-support module with a very different flavor.
80
Michael Foordba097ec2010-04-03 17:03:11 +000081 `unittest2: A backport of new unittest features for Python 2.4-2.6 <http://pypi.python.org/pypi/unittest2>`_
82 Many new features were added to unittest in Python 2.7, including test
83 discovery. unittest2 allows you to use these features with earlier
84 versions of Python.
85
Georg Brandld198b762009-05-31 14:15:25 +000086 `Simple Smalltalk Testing: With Patterns <http://www.XProgramming.com/testfram.htm>`_
Georg Brandl2fcd1732009-05-30 10:45:40 +000087 Kent Beck's original paper on testing frameworks using the pattern shared
88 by :mod:`unittest`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000089
Georg Brandld198b762009-05-31 14:15:25 +000090 `Nose <http://code.google.com/p/python-nose/>`_ and `py.test <http://pytest.org>`_
Georg Brandl2fcd1732009-05-30 10:45:40 +000091 Third-party unittest frameworks with a lighter-weight syntax for writing
92 tests. For example, ``assert func(10) == 42``.
Raymond Hettinger21b617b2009-03-24 00:17:11 +000093
Michael Foordba097ec2010-04-03 17:03:11 +000094 `The Python Testing Tools Taxonomy <http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy>`_
95 An extensive list of Python testing tools including functional testing
96 frameworks and mock object libraries.
Georg Brandl8ec7f652007-08-15 14:28:01 +000097
Michael Foordba097ec2010-04-03 17:03:11 +000098 `Testing in Python Mailing List <http://lists.idyll.org/listinfo/testing-in-python>`_
99 A special-interest-group for discussion of testing, and testing tools,
100 in Python.
Michael Foordb4a81c82009-05-29 20:33:46 +0000101
Michael Foord5d31e052009-05-11 17:59:43 +0000102
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103.. _unittest-minimal-example:
104
105Basic example
106-------------
107
108The :mod:`unittest` module provides a rich set of tools for constructing and
109running tests. This section demonstrates that a small subset of the tools
110suffice to meet the needs of most users.
111
112Here is a short script to test three functions from the :mod:`random` module::
113
114 import random
115 import unittest
116
117 class TestSequenceFunctions(unittest.TestCase):
118
119 def setUp(self):
120 self.seq = range(10)
121
Benjamin Peterson99721e02009-03-23 23:10:14 +0000122 def test_shuffle(self):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000123 # make sure the shuffled sequence does not lose any elements
124 random.shuffle(self.seq)
125 self.seq.sort()
126 self.assertEqual(self.seq, range(10))
127
Raymond Hettinger08090bf2010-03-09 08:44:18 +0000128 # should raise an exception for an immutable sequence
129 self.assertRaises(TypeError, random.shuffle, (1,2,3))
130
Benjamin Peterson99721e02009-03-23 23:10:14 +0000131 def test_choice(self):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132 element = random.choice(self.seq)
Ezio Melotti3cbb66b2011-03-10 23:35:39 +0200133 self.assertTrue(element in self.seq)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134
Benjamin Peterson99721e02009-03-23 23:10:14 +0000135 def test_sample(self):
Raymond Hettinger08090bf2010-03-09 08:44:18 +0000136 with self.assertRaises(ValueError):
137 random.sample(self.seq, 20)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138 for element in random.sample(self.seq, 5):
Ezio Melotti3cbb66b2011-03-10 23:35:39 +0200139 self.assertTrue(element in self.seq)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140
141 if __name__ == '__main__':
142 unittest.main()
143
Benjamin Peterson99721e02009-03-23 23:10:14 +0000144A testcase is created by subclassing :class:`unittest.TestCase`. The three
Georg Brandl8ec7f652007-08-15 14:28:01 +0000145individual tests are defined with methods whose names start with the letters
146``test``. This naming convention informs the test runner about which methods
147represent tests.
148
Benjamin Peterson99721e02009-03-23 23:10:14 +0000149The crux of each test is a call to :meth:`~TestCase.assertEqual` to check for an
Michael Foorddb43b5a2010-02-10 14:25:12 +0000150expected result; :meth:`~TestCase.assertTrue` to verify a condition; or
Benjamin Peterson99721e02009-03-23 23:10:14 +0000151:meth:`~TestCase.assertRaises` to verify that an expected exception gets raised.
152These methods are used instead of the :keyword:`assert` statement so the test
153runner can accumulate all test results and produce a report.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000154
Benjamin Peterson99721e02009-03-23 23:10:14 +0000155When a :meth:`~TestCase.setUp` method is defined, the test runner will run that
156method prior to each test. Likewise, if a :meth:`~TestCase.tearDown` method is
157defined, the test runner will invoke that method after each test. In the
158example, :meth:`~TestCase.setUp` was used to create a fresh sequence for each
159test.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160
161The final block shows a simple way to run the tests. :func:`unittest.main`
Éric Araujoa8132ec2010-12-16 03:53:53 +0000162provides a command-line interface to the test script. When run from the command
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163line, the above script produces an output that looks like this::
164
165 ...
166 ----------------------------------------------------------------------
167 Ran 3 tests in 0.000s
168
169 OK
170
171Instead of :func:`unittest.main`, there are other ways to run the tests with a
172finer level of control, less terse output, and no requirement to be run from the
173command line. For example, the last two lines may be replaced with::
174
175 suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
176 unittest.TextTestRunner(verbosity=2).run(suite)
177
178Running the revised script from the interpreter or another script produces the
179following output::
180
Ezio Melotti68beef62010-02-28 03:11:07 +0000181 test_choice (__main__.TestSequenceFunctions) ... ok
182 test_sample (__main__.TestSequenceFunctions) ... ok
183 test_shuffle (__main__.TestSequenceFunctions) ... ok
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184
185 ----------------------------------------------------------------------
186 Ran 3 tests in 0.110s
187
188 OK
189
190The above examples show the most commonly used :mod:`unittest` features which
191are sufficient to meet many everyday testing needs. The remainder of the
192documentation explores the full feature set from first principles.
193
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000194
195.. _unittest-command-line-interface:
196
Ezio Melottic3ab30b2011-03-12 22:21:37 +0200197Command-Line Interface
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000198----------------------
199
200The unittest module can be used from the command line to run tests from
201modules, classes or even individual test methods::
202
203 python -m unittest test_module1 test_module2
204 python -m unittest test_module.TestClass
205 python -m unittest test_module.TestClass.test_method
206
207You can pass in a list with any combination of module names, and fully
208qualified class or method names.
209
210You can run tests with more detail (higher verbosity) by passing in the -v flag::
211
Ezio Melotti062d2b52009-12-19 22:41:49 +0000212 python -m unittest -v test_module
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000213
Éric Araujoa8132ec2010-12-16 03:53:53 +0000214For a list of all the command-line options::
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000215
216 python -m unittest -h
217
218.. versionchanged:: 2.7
219 In earlier versions it was only possible to run individual test methods and
220 not modules or classes.
221
Michael Foordba097ec2010-04-03 17:03:11 +0000222
Éric Araujoa8132ec2010-12-16 03:53:53 +0000223Command-line options
224~~~~~~~~~~~~~~~~~~~~
Michael Foordba097ec2010-04-03 17:03:11 +0000225
Éric Araujoa8132ec2010-12-16 03:53:53 +0000226:program:`unittest` supports these command-line options:
Michael Foordba097ec2010-04-03 17:03:11 +0000227
Éric Araujoa8132ec2010-12-16 03:53:53 +0000228.. program:: unittest
Michael Foordba097ec2010-04-03 17:03:11 +0000229
Éric Araujoa8132ec2010-12-16 03:53:53 +0000230.. cmdoption:: -b, --buffer
Michael Foordba097ec2010-04-03 17:03:11 +0000231
Éric Araujoa8132ec2010-12-16 03:53:53 +0000232 The standard output and standard error streams are buffered during the test
233 run. Output during a passing test is discarded. Output is echoed normally
234 on test fail or error and is added to the failure messages.
Andrew M. Kuchlingfb759a22010-04-29 01:44:30 +0000235
Éric Araujoa8132ec2010-12-16 03:53:53 +0000236.. cmdoption:: -c, --catch
Andrew M. Kuchlingfb759a22010-04-29 01:44:30 +0000237
Éric Araujoa8132ec2010-12-16 03:53:53 +0000238 Control-C during the test run waits for the current test to end and then
239 reports all the results so far. A second control-C raises the normal
240 :exc:`KeyboardInterrupt` exception.
Andrew M. Kuchlingfb759a22010-04-29 01:44:30 +0000241
Éric Araujoa8132ec2010-12-16 03:53:53 +0000242 See `Signal Handling`_ for the functions that provide this functionality.
Andrew M. Kuchlingfb759a22010-04-29 01:44:30 +0000243
Éric Araujoa8132ec2010-12-16 03:53:53 +0000244.. cmdoption:: -f, --failfast
Andrew M. Kuchlingfb759a22010-04-29 01:44:30 +0000245
Éric Araujoa8132ec2010-12-16 03:53:53 +0000246 Stop the test run on the first error or failure.
247
248.. versionadded:: 2.7
249 The command-line options ``-b``, ``-c`` and ``-f`` were added.
Michael Foordba097ec2010-04-03 17:03:11 +0000250
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000251The command line can also be used for test discovery, for running all of the
252tests in a project or just a subset.
253
254
255.. _unittest-test-discovery:
256
257Test Discovery
258--------------
259
260.. versionadded:: 2.7
261
Ezio Melotti9e1ed472011-03-08 17:08:25 +0200262Unittest supports simple test discovery. In order to be compatible with test
263discovery, all of the test files must be :ref:`modules <tut-modules>` or
264:ref:`packages <tut-packages>` importable from the top-level directory of
265the project (this means that their filenames must be valid
266:ref:`identifiers <identifiers>`).
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000267
268Test discovery is implemented in :meth:`TestLoader.discover`, but can also be
Éric Araujoa8132ec2010-12-16 03:53:53 +0000269used from the command line. The basic command-line usage is::
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000270
271 cd project_directory
272 python -m unittest discover
273
274The ``discover`` sub-command has the following options:
275
Éric Araujoa8132ec2010-12-16 03:53:53 +0000276.. program:: unittest discover
277
278.. cmdoption:: -v, --verbose
279
280 Verbose output
281
282.. cmdoption:: -s directory
283
284 Directory to start discovery ('.' default)
285
286.. cmdoption:: -p pattern
287
288 Pattern to match test files ('test*.py' default)
289
290.. cmdoption:: -t directory
291
292 Top level directory of project (defaults to start directory)
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000293
Andrew M. Kuchling60383182010-04-30 01:32:47 +0000294The :option:`-s`, :option:`-p`, and :option:`-t` options can be passed in
295as positional arguments in that order. The following two command lines
296are equivalent::
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000297
Ezio Melotti7b4e02c2010-01-27 20:25:11 +0000298 python -m unittest discover -s project_directory -p '*_test.py'
299 python -m unittest discover project_directory '*_test.py'
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000300
Michael Foord8851b712010-05-08 15:09:37 +0000301As well as being a path it is possible to pass a package name, for example
302``myproject.subpackage.test``, as the start directory. The package name you
303supply will then be imported and its location on the filesystem will be used
304as the start directory.
305
306.. caution::
307
308 Test discovery loads tests by importing them. Once test discovery has
309 found all the test files from the start directory you specify it turns the
Éric Araujoa7cbe282011-09-01 19:49:31 +0200310 paths into package names to import. For example :file:`foo/bar/baz.py` will be
Michael Foord8851b712010-05-08 15:09:37 +0000311 imported as ``foo.bar.baz``.
312
313 If you have a package installed globally and attempt test discovery on
314 a different copy of the package then the import *could* happen from the
315 wrong place. If this happens test discovery will warn you and exit.
316
317 If you supply the start directory as a package name rather than a
318 path to a directory then discover assumes that whichever location it
319 imports from is the location you intended, so you will not get the
320 warning.
321
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000322Test modules and packages can customize test loading and discovery by through
323the `load_tests protocol`_.
324
325
Georg Brandl8ec7f652007-08-15 14:28:01 +0000326.. _organizing-tests:
327
328Organizing test code
329--------------------
330
331The basic building blocks of unit testing are :dfn:`test cases` --- single
332scenarios that must be set up and checked for correctness. In :mod:`unittest`,
333test cases are represented by instances of :mod:`unittest`'s :class:`TestCase`
334class. To make your own test cases you must write subclasses of
335:class:`TestCase`, or use :class:`FunctionTestCase`.
336
337An instance of a :class:`TestCase`\ -derived class is an object that can
338completely run a single test method, together with optional set-up and tidy-up
339code.
340
341The testing code of a :class:`TestCase` instance should be entirely self
342contained, such that it can be run either in isolation or in arbitrary
343combination with any number of other test cases.
344
Benjamin Peterson99721e02009-03-23 23:10:14 +0000345The simplest :class:`TestCase` subclass will simply override the
346:meth:`~TestCase.runTest` method in order to perform specific testing code::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000347
348 import unittest
349
350 class DefaultWidgetSizeTestCase(unittest.TestCase):
351 def runTest(self):
352 widget = Widget('The widget')
353 self.assertEqual(widget.size(), (50, 50), 'incorrect default size')
354
Gregory P. Smith28399852009-03-31 16:54:10 +0000355Note that in order to test something, we use the one of the :meth:`assert\*`
Georg Brandl2fcd1732009-05-30 10:45:40 +0000356methods provided by the :class:`TestCase` base class. If the test fails, an
357exception will be raised, and :mod:`unittest` will identify the test case as a
358:dfn:`failure`. Any other exceptions will be treated as :dfn:`errors`. This
359helps you identify where the problem is: :dfn:`failures` are caused by incorrect
360results - a 5 where you expected a 6. :dfn:`Errors` are caused by incorrect
361code - e.g., a :exc:`TypeError` caused by an incorrect function call.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000362
363The way to run a test case will be described later. For now, note that to
364construct an instance of such a test case, we call its constructor without
365arguments::
366
367 testCase = DefaultWidgetSizeTestCase()
368
369Now, such test cases can be numerous, and their set-up can be repetitive. In
370the above case, constructing a :class:`Widget` in each of 100 Widget test case
371subclasses would mean unsightly duplication.
372
373Luckily, we can factor out such set-up code by implementing a method called
Benjamin Peterson99721e02009-03-23 23:10:14 +0000374:meth:`~TestCase.setUp`, which the testing framework will automatically call for
375us when we run the test::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000376
377 import unittest
378
379 class SimpleWidgetTestCase(unittest.TestCase):
380 def setUp(self):
381 self.widget = Widget('The widget')
382
383 class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
384 def runTest(self):
Ezio Melotti85ee3e12010-02-04 20:06:38 +0000385 self.assertEqual(self.widget.size(), (50,50),
386 'incorrect default size')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000387
388 class WidgetResizeTestCase(SimpleWidgetTestCase):
389 def runTest(self):
390 self.widget.resize(100,150)
Ezio Melotti85ee3e12010-02-04 20:06:38 +0000391 self.assertEqual(self.widget.size(), (100,150),
392 'wrong size after resize')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000393
Benjamin Peterson99721e02009-03-23 23:10:14 +0000394If the :meth:`~TestCase.setUp` method raises an exception while the test is
395running, the framework will consider the test to have suffered an error, and the
396:meth:`~TestCase.runTest` method will not be executed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000397
Benjamin Peterson99721e02009-03-23 23:10:14 +0000398Similarly, we can provide a :meth:`~TestCase.tearDown` method that tidies up
399after the :meth:`~TestCase.runTest` method has been run::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000400
401 import unittest
402
403 class SimpleWidgetTestCase(unittest.TestCase):
404 def setUp(self):
405 self.widget = Widget('The widget')
406
407 def tearDown(self):
408 self.widget.dispose()
409 self.widget = None
410
Benjamin Peterson99721e02009-03-23 23:10:14 +0000411If :meth:`~TestCase.setUp` succeeded, the :meth:`~TestCase.tearDown` method will
412be run whether :meth:`~TestCase.runTest` succeeded or not.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000413
414Such a working environment for the testing code is called a :dfn:`fixture`.
415
416Often, many small test cases will use the same fixture. In this case, we would
417end up subclassing :class:`SimpleWidgetTestCase` into many small one-method
418classes such as :class:`DefaultWidgetSizeTestCase`. This is time-consuming and
Georg Brandl8ec7f652007-08-15 14:28:01 +0000419discouraging, so in the same vein as JUnit, :mod:`unittest` provides a simpler
420mechanism::
421
422 import unittest
423
424 class WidgetTestCase(unittest.TestCase):
425 def setUp(self):
426 self.widget = Widget('The widget')
427
428 def tearDown(self):
429 self.widget.dispose()
430 self.widget = None
431
Ezio Melotti68beef62010-02-28 03:11:07 +0000432 def test_default_size(self):
Ezio Melotti85ee3e12010-02-04 20:06:38 +0000433 self.assertEqual(self.widget.size(), (50,50),
434 'incorrect default size')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000435
Ezio Melotti68beef62010-02-28 03:11:07 +0000436 def test_resize(self):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000437 self.widget.resize(100,150)
Ezio Melotti85ee3e12010-02-04 20:06:38 +0000438 self.assertEqual(self.widget.size(), (100,150),
439 'wrong size after resize')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000440
Benjamin Peterson99721e02009-03-23 23:10:14 +0000441Here we have not provided a :meth:`~TestCase.runTest` method, but have instead
442provided two different test methods. Class instances will now each run one of
Ezio Melotti68beef62010-02-28 03:11:07 +0000443the :meth:`test_\*` methods, with ``self.widget`` created and destroyed
Benjamin Peterson99721e02009-03-23 23:10:14 +0000444separately for each instance. When creating an instance we must specify the
445test method it is to run. We do this by passing the method name in the
446constructor::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000447
Ezio Melotti68beef62010-02-28 03:11:07 +0000448 defaultSizeTestCase = WidgetTestCase('test_default_size')
449 resizeTestCase = WidgetTestCase('test_resize')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000450
451Test case instances are grouped together according to the features they test.
452:mod:`unittest` provides a mechanism for this: the :dfn:`test suite`,
453represented by :mod:`unittest`'s :class:`TestSuite` class::
454
455 widgetTestSuite = unittest.TestSuite()
Ezio Melotti68beef62010-02-28 03:11:07 +0000456 widgetTestSuite.addTest(WidgetTestCase('test_default_size'))
457 widgetTestSuite.addTest(WidgetTestCase('test_resize'))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000458
459For the ease of running tests, as we will see later, it is a good idea to
460provide in each test module a callable object that returns a pre-built test
461suite::
462
463 def suite():
464 suite = unittest.TestSuite()
Ezio Melotti68beef62010-02-28 03:11:07 +0000465 suite.addTest(WidgetTestCase('test_default_size'))
466 suite.addTest(WidgetTestCase('test_resize'))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000467 return suite
468
469or even::
470
471 def suite():
Ezio Melotti68beef62010-02-28 03:11:07 +0000472 tests = ['test_default_size', 'test_resize']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000473
474 return unittest.TestSuite(map(WidgetTestCase, tests))
475
476Since it is a common pattern to create a :class:`TestCase` subclass with many
477similarly named test functions, :mod:`unittest` provides a :class:`TestLoader`
478class that can be used to automate the process of creating a test suite and
479populating it with individual tests. For example, ::
480
481 suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
482
Ezio Melotti68beef62010-02-28 03:11:07 +0000483will create a test suite that will run ``WidgetTestCase.test_default_size()`` and
484``WidgetTestCase.test_resize``. :class:`TestLoader` uses the ``'test'`` method
Georg Brandl8ec7f652007-08-15 14:28:01 +0000485name prefix to identify test methods automatically.
486
Ezio Melottidd7c5932011-03-10 23:00:48 +0200487Note that the order in which the various test cases will be run is
488determined by sorting the test function names with respect to the
489built-in ordering for strings.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000490
491Often it is desirable to group suites of test cases together, so as to run tests
492for the whole system at once. This is easy, since :class:`TestSuite` instances
493can be added to a :class:`TestSuite` just as :class:`TestCase` instances can be
494added to a :class:`TestSuite`::
495
496 suite1 = module1.TheTestSuite()
497 suite2 = module2.TheTestSuite()
498 alltests = unittest.TestSuite([suite1, suite2])
499
500You can place the definitions of test cases and test suites in the same modules
501as the code they are to test (such as :file:`widget.py`), but there are several
502advantages to placing the test code in a separate module, such as
503:file:`test_widget.py`:
504
505* The test module can be run standalone from the command line.
506
507* The test code can more easily be separated from shipped code.
508
509* There is less temptation to change test code to fit the code it tests without
510 a good reason.
511
512* Test code should be modified much less frequently than the code it tests.
513
514* Tested code can be refactored more easily.
515
516* Tests for modules written in C must be in separate modules anyway, so why not
517 be consistent?
518
519* If the testing strategy changes, there is no need to change the source code.
520
521
522.. _legacy-unit-tests:
523
524Re-using old test code
525----------------------
526
527Some users will find that they have existing test code that they would like to
528run from :mod:`unittest`, without converting every old test function to a
529:class:`TestCase` subclass.
530
531For this reason, :mod:`unittest` provides a :class:`FunctionTestCase` class.
532This subclass of :class:`TestCase` can be used to wrap an existing test
533function. Set-up and tear-down functions can also be provided.
534
535Given the following test function::
536
537 def testSomething():
538 something = makeSomething()
539 assert something.name is not None
540 # ...
541
542one can create an equivalent test case instance as follows::
543
544 testcase = unittest.FunctionTestCase(testSomething)
545
546If there are additional set-up and tear-down methods that should be called as
547part of the test case's operation, they can also be provided like so::
548
549 testcase = unittest.FunctionTestCase(testSomething,
550 setUp=makeSomethingDB,
551 tearDown=deleteSomethingDB)
552
553To make migrating existing test suites easier, :mod:`unittest` supports tests
554raising :exc:`AssertionError` to indicate test failure. However, it is
555recommended that you use the explicit :meth:`TestCase.fail\*` and
556:meth:`TestCase.assert\*` methods instead, as future versions of :mod:`unittest`
557may treat :exc:`AssertionError` differently.
558
559.. note::
560
Georg Brandl2fcd1732009-05-30 10:45:40 +0000561 Even though :class:`FunctionTestCase` can be used to quickly convert an
562 existing test base over to a :mod:`unittest`\ -based system, this approach is
563 not recommended. Taking the time to set up proper :class:`TestCase`
564 subclasses will make future test refactorings infinitely easier.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000565
Benjamin Peterson99721e02009-03-23 23:10:14 +0000566In some cases, the existing tests may have been written using the :mod:`doctest`
567module. If so, :mod:`doctest` provides a :class:`DocTestSuite` class that can
568automatically build :class:`unittest.TestSuite` instances from the existing
569:mod:`doctest`\ -based tests.
570
Georg Brandl8ec7f652007-08-15 14:28:01 +0000571
Benjamin Peterson692428e2009-03-23 21:50:21 +0000572.. _unittest-skipping:
573
574Skipping tests and expected failures
575------------------------------------
576
Michael Foordfb0844b2010-02-05 21:45:12 +0000577.. versionadded:: 2.7
578
Benjamin Peterson692428e2009-03-23 21:50:21 +0000579Unittest supports skipping individual test methods and even whole classes of
580tests. In addition, it supports marking a test as a "expected failure," a test
581that is broken and will fail, but shouldn't be counted as a failure on a
582:class:`TestResult`.
583
584Skipping a test is simply a matter of using the :func:`skip` :term:`decorator`
585or one of its conditional variants.
586
587Basic skipping looks like this: ::
588
589 class MyTestCase(unittest.TestCase):
590
591 @unittest.skip("demonstrating skipping")
592 def test_nothing(self):
593 self.fail("shouldn't happen")
594
Georg Brandl2fcd1732009-05-30 10:45:40 +0000595 @unittest.skipIf(mylib.__version__ < (1, 3),
596 "not supported in this library version")
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000597 def test_format(self):
Benjamin Peterson097aafd2009-03-29 03:39:58 +0000598 # Tests that work for only a certain version of the library.
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000599 pass
600
601 @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
602 def test_windows_support(self):
603 # windows specific testing code
604 pass
605
Benjamin Peterson692428e2009-03-23 21:50:21 +0000606This is the output of running the example above in verbose mode: ::
607
Benjamin Peterson097aafd2009-03-29 03:39:58 +0000608 test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'
Benjamin Peterson692428e2009-03-23 21:50:21 +0000609 test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000610 test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'
Benjamin Peterson692428e2009-03-23 21:50:21 +0000611
612 ----------------------------------------------------------------------
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000613 Ran 3 tests in 0.005s
614
615 OK (skipped=3)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000616
617Classes can be skipped just like methods: ::
618
619 @skip("showing class skipping")
620 class MySkippedTestCase(unittest.TestCase):
621 def test_not_run(self):
622 pass
623
Benjamin Peterson31b78062009-03-23 23:13:36 +0000624:meth:`TestCase.setUp` can also skip the test. This is useful when a resource
625that needs to be set up is not available.
626
Benjamin Peterson692428e2009-03-23 21:50:21 +0000627Expected failures use the :func:`expectedFailure` decorator. ::
628
629 class ExpectedFailureTestCase(unittest.TestCase):
630 @unittest.expectedFailure
631 def test_fail(self):
632 self.assertEqual(1, 0, "broken")
633
634It's easy to roll your own skipping decorators by making a decorator that calls
635:func:`skip` on the test when it wants it to be skipped. This decorator skips
636the test unless the passed object has a certain attribute: ::
637
638 def skipUnlessHasattr(obj, attr):
639 if hasattr(obj, attr):
640 return lambda func: func
641 return unittest.skip("{0!r} doesn't have {1!r}".format(obj, attr))
642
643The following decorators implement test skipping and expected failures:
644
645.. function:: skip(reason)
646
647 Unconditionally skip the decorated test. *reason* should describe why the
648 test is being skipped.
649
650.. function:: skipIf(condition, reason)
651
652 Skip the decorated test if *condition* is true.
653
654.. function:: skipUnless(condition, reason)
655
Georg Brandl09302282010-10-06 09:32:48 +0000656 Skip the decorated test unless *condition* is true.
Benjamin Peterson692428e2009-03-23 21:50:21 +0000657
658.. function:: expectedFailure
659
660 Mark the test as an expected failure. If the test fails when run, the test
661 is not counted as a failure.
662
Michael Foord09e29802010-04-04 22:41:54 +0000663Skipped tests will not have :meth:`setUp` or :meth:`tearDown` run around them.
664Skipped classes will not have :meth:`setUpClass` or :meth:`tearDownClass` run.
Michael Foordba097ec2010-04-03 17:03:11 +0000665
Benjamin Peterson692428e2009-03-23 21:50:21 +0000666
Georg Brandl8ec7f652007-08-15 14:28:01 +0000667.. _unittest-contents:
668
669Classes and functions
670---------------------
671
Benjamin Peterson99721e02009-03-23 23:10:14 +0000672This section describes in depth the API of :mod:`unittest`.
673
674
675.. _testcase-objects:
676
677Test cases
678~~~~~~~~~~
Georg Brandl8ec7f652007-08-15 14:28:01 +0000679
Ezio Melottidd7c5932011-03-10 23:00:48 +0200680.. class:: TestCase(methodName='runTest')
Georg Brandl8ec7f652007-08-15 14:28:01 +0000681
682 Instances of the :class:`TestCase` class represent the smallest testable units
683 in the :mod:`unittest` universe. This class is intended to be used as a base
684 class, with specific tests being implemented by concrete subclasses. This class
685 implements the interface needed by the test runner to allow it to drive the
686 test, and methods that the test code can use to check for and report various
687 kinds of failure.
688
689 Each instance of :class:`TestCase` will run a single test method: the method
690 named *methodName*. If you remember, we had an earlier example that went
691 something like this::
692
693 def suite():
694 suite = unittest.TestSuite()
Ezio Melotti68beef62010-02-28 03:11:07 +0000695 suite.addTest(WidgetTestCase('test_default_size'))
696 suite.addTest(WidgetTestCase('test_resize'))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000697 return suite
698
699 Here, we create two instances of :class:`WidgetTestCase`, each of which runs a
700 single test.
701
Benjamin Peterson99721e02009-03-23 23:10:14 +0000702 *methodName* defaults to :meth:`runTest`.
703
704 :class:`TestCase` instances provide three groups of methods: one group used
705 to run the test, another used by the test implementation to check conditions
706 and report failures, and some inquiry methods allowing information about the
707 test itself to be gathered.
708
709 Methods in the first group (running the test) are:
710
711
712 .. method:: setUp()
713
714 Method called to prepare the test fixture. This is called immediately
715 before calling the test method; any exception raised by this method will
716 be considered an error rather than a test failure. The default
717 implementation does nothing.
718
719
720 .. method:: tearDown()
721
722 Method called immediately after the test method has been called and the
723 result recorded. This is called even if the test method raised an
724 exception, so the implementation in subclasses may need to be particularly
725 careful about checking internal state. Any exception raised by this
726 method will be considered an error rather than a test failure. This
727 method will only be called if the :meth:`setUp` succeeds, regardless of
728 the outcome of the test method. The default implementation does nothing.
729
730
Michael Foordba097ec2010-04-03 17:03:11 +0000731 .. method:: setUpClass()
732
733 A class method called before tests in an individual class run.
734 ``setUpClass`` is called with the class as the only argument
Michael Foord09e29802010-04-04 22:41:54 +0000735 and must be decorated as a :func:`classmethod`::
Michael Foordba097ec2010-04-03 17:03:11 +0000736
737 @classmethod
738 def setUpClass(cls):
739 ...
740
741 See `Class and Module Fixtures`_ for more details.
742
743 .. versionadded:: 2.7
744
745
746 .. method:: tearDownClass()
747
748 A class method called after tests in an individual class have run.
749 ``tearDownClass`` is called with the class as the only argument
750 and must be decorated as a :meth:`classmethod`::
751
752 @classmethod
753 def tearDownClass(cls):
754 ...
755
756 See `Class and Module Fixtures`_ for more details.
757
758 .. versionadded:: 2.7
759
760
Ezio Melottidd7c5932011-03-10 23:00:48 +0200761 .. method:: run(result=None)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000762
763 Run the test, collecting the result into the test result object passed as
Ezio Melottidd7c5932011-03-10 23:00:48 +0200764 *result*. If *result* is omitted or ``None``, a temporary result
Ezio Melottic2f5a592009-06-30 22:51:06 +0000765 object is created (by calling the :meth:`defaultTestResult` method) and
766 used. The result object is not returned to :meth:`run`'s caller.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000767
768 The same effect may be had by simply calling the :class:`TestCase`
769 instance.
770
771
Benjamin Peterson47d97382009-03-26 20:05:50 +0000772 .. method:: skipTest(reason)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000773
Stefan Krah4a769052010-05-19 15:59:40 +0000774 Calling this during a test method or :meth:`setUp` skips the current
Benjamin Peterson31b78062009-03-23 23:13:36 +0000775 test. See :ref:`unittest-skipping` for more information.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000776
Georg Brandl90aae552010-04-10 11:15:24 +0000777 .. versionadded:: 2.7
778
Benjamin Peterson99721e02009-03-23 23:10:14 +0000779
780 .. method:: debug()
781
782 Run the test without collecting the result. This allows exceptions raised
783 by the test to be propagated to the caller, and can be used to support
784 running tests under a debugger.
785
Ezio Melottidd7c5932011-03-10 23:00:48 +0200786 .. _assert-methods:
Benjamin Peterson99721e02009-03-23 23:10:14 +0000787
Ezio Melottidd7c5932011-03-10 23:00:48 +0200788 The :class:`TestCase` class provides a number of methods to check for and
789 report failures, such as:
Benjamin Peterson99721e02009-03-23 23:10:14 +0000790
Ezio Melottidd7c5932011-03-10 23:00:48 +0200791 +-----------------------------------------+-----------------------------+---------------+
792 | Method | Checks that | New in |
793 +=========================================+=============================+===============+
794 | :meth:`assertEqual(a, b) | ``a == b`` | |
795 | <TestCase.assertEqual>` | | |
796 +-----------------------------------------+-----------------------------+---------------+
797 | :meth:`assertNotEqual(a, b) | ``a != b`` | |
798 | <TestCase.assertNotEqual>` | | |
799 +-----------------------------------------+-----------------------------+---------------+
800 | :meth:`assertTrue(x) | ``bool(x) is True`` | |
801 | <TestCase.assertTrue>` | | |
802 +-----------------------------------------+-----------------------------+---------------+
803 | :meth:`assertFalse(x) | ``bool(x) is False`` | |
804 | <TestCase.assertFalse>` | | |
805 +-----------------------------------------+-----------------------------+---------------+
806 | :meth:`assertIs(a, b) | ``a is b`` | 2.7 |
807 | <TestCase.assertIs>` | | |
808 +-----------------------------------------+-----------------------------+---------------+
809 | :meth:`assertIsNot(a, b) | ``a is not b`` | 2.7 |
810 | <TestCase.assertIsNot>` | | |
811 +-----------------------------------------+-----------------------------+---------------+
812 | :meth:`assertIsNone(x) | ``x is None`` | 2.7 |
813 | <TestCase.assertIsNone>` | | |
814 +-----------------------------------------+-----------------------------+---------------+
815 | :meth:`assertIsNotNone(x) | ``x is not None`` | 2.7 |
816 | <TestCase.assertIsNotNone>` | | |
817 +-----------------------------------------+-----------------------------+---------------+
818 | :meth:`assertIn(a, b) | ``a in b`` | 2.7 |
819 | <TestCase.assertIn>` | | |
820 +-----------------------------------------+-----------------------------+---------------+
821 | :meth:`assertNotIn(a, b) | ``a not in b`` | 2.7 |
822 | <TestCase.assertNotIn>` | | |
823 +-----------------------------------------+-----------------------------+---------------+
824 | :meth:`assertIsInstance(a, b) | ``isinstance(a, b)`` | 2.7 |
825 | <TestCase.assertIsInstance>` | | |
826 +-----------------------------------------+-----------------------------+---------------+
827 | :meth:`assertNotIsInstance(a, b) | ``not isinstance(a, b)`` | 2.7 |
828 | <TestCase.assertNotIsInstance>` | | |
829 +-----------------------------------------+-----------------------------+---------------+
Benjamin Peterson99721e02009-03-23 23:10:14 +0000830
Ezio Melottidd7c5932011-03-10 23:00:48 +0200831 All the assert methods (except :meth:`assertRaises`,
832 :meth:`assertRaisesRegexp`)
833 accept a *msg* argument that, if specified, is used as the error message on
834 failure (see also :data:`longMessage`).
Benjamin Peterson99721e02009-03-23 23:10:14 +0000835
Ezio Melottidd7c5932011-03-10 23:00:48 +0200836 .. method:: assertEqual(first, second, msg=None)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000837
838 Test that *first* and *second* are equal. If the values do not compare
Ezio Melottidd7c5932011-03-10 23:00:48 +0200839 equal, the test will fail.
Gregory P. Smith28399852009-03-31 16:54:10 +0000840
841 In addition, if *first* and *second* are the exact same type and one of
Michael Foordfe6349c2010-02-08 22:41:16 +0000842 list, tuple, dict, set, frozenset or unicode or any type that a subclass
Michael Foord7b5aa462010-02-08 23:15:22 +0000843 registers with :meth:`addTypeEqualityFunc` the type specific equality
Ezio Melottidd7c5932011-03-10 23:00:48 +0200844 function will be called in order to generate a more useful default
845 error message (see also the :ref:`list of type-specific methods
846 <type-specific-methods>`).
Gregory P. Smith28399852009-03-31 16:54:10 +0000847
848 .. versionchanged:: 2.7
849 Added the automatic calling of type specific equality function.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000850
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000851
Ezio Melottidd7c5932011-03-10 23:00:48 +0200852 .. method:: assertNotEqual(first, second, msg=None)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000853
854 Test that *first* and *second* are not equal. If the values do compare
Ezio Melottidd7c5932011-03-10 23:00:48 +0200855 equal, the test will fail.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000856
Ezio Melottidd7c5932011-03-10 23:00:48 +0200857 .. method:: assertTrue(expr, msg=None)
858 assertFalse(expr, msg=None)
859
860 Test that *expr* is true (or false).
861
862 Note that this is equivalent to ``bool(expr) is True`` and not to ``expr
863 is True`` (use ``assertIs(expr, True)`` for the latter). This method
864 should also be avoided when more specific methods are available (e.g.
865 ``assertEqual(a, b)`` instead of ``assertTrue(a == b)``), because they
866 provide a better error message in case of failure.
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000867
Benjamin Peterson99721e02009-03-23 23:10:14 +0000868
Ezio Melottidd7c5932011-03-10 23:00:48 +0200869 .. method:: assertIs(first, second, msg=None)
870 assertIsNot(first, second, msg=None)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000871
Ezio Melottidd7c5932011-03-10 23:00:48 +0200872 Test that *first* and *second* evaluate (or don't evaluate) to the same object.
Gregory P. Smith28399852009-03-31 16:54:10 +0000873
874 .. versionadded:: 2.7
875
876
Ezio Melottidd7c5932011-03-10 23:00:48 +0200877 .. method:: assertIsNone(expr, msg=None)
878 assertIsNotNone(expr, msg=None)
Gregory P. Smith28399852009-03-31 16:54:10 +0000879
Ezio Melottidd7c5932011-03-10 23:00:48 +0200880 Test that *expr* is (or is not) None.
Michael Foordba097ec2010-04-03 17:03:11 +0000881
882 .. versionadded:: 2.7
883
884
Gregory P. Smith28399852009-03-31 16:54:10 +0000885 .. method:: assertIn(first, second, msg=None)
886 assertNotIn(first, second, msg=None)
887
Ezio Melottidd7c5932011-03-10 23:00:48 +0200888 Test that *first* is (or is not) in *second*.
Gregory P. Smith28399852009-03-31 16:54:10 +0000889
890 .. versionadded:: 2.7
891
892
Ezio Melottidd7c5932011-03-10 23:00:48 +0200893 .. method:: assertIsInstance(obj, cls, msg=None)
894 assertNotIsInstance(obj, cls, msg=None)
Gregory P. Smith28399852009-03-31 16:54:10 +0000895
Ezio Melottidd7c5932011-03-10 23:00:48 +0200896 Test that *obj* is (or is not) an instance of *cls* (which can be a
897 class or a tuple of classes, as supported by :func:`isinstance`).
Ezio Melottiaa512f02011-11-18 18:59:36 +0200898 To check for a specific type (without including superclasses) use
899 :func:`assertIs(type(obj), cls) <assertIs>`.
Gregory P. Smith28399852009-03-31 16:54:10 +0000900
901 .. versionadded:: 2.7
902
903
Ezio Melottidd7c5932011-03-10 23:00:48 +0200904 It is also possible to check that exceptions and warnings are raised using
905 the following methods:
Gregory P. Smith28399852009-03-31 16:54:10 +0000906
Ezio Melottidd7c5932011-03-10 23:00:48 +0200907 +---------------------------------------------------------+--------------------------------------+------------+
908 | Method | Checks that | New in |
909 +=========================================================+======================================+============+
Éric Araujoa7cbe282011-09-01 19:49:31 +0200910 | :meth:`assertRaises(exc, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises *exc* | |
Ezio Melottidd7c5932011-03-10 23:00:48 +0200911 | <TestCase.assertRaises>` | | |
912 +---------------------------------------------------------+--------------------------------------+------------+
Éric Araujoa7cbe282011-09-01 19:49:31 +0200913 | :meth:`assertRaisesRegexp(exc, re, fun, *args, **kwds) | ``fun(*args, **kwds)`` raises *exc* | 2.7 |
914 | <TestCase.assertRaisesRegexp>` | and the message matches *re* | |
Ezio Melottidd7c5932011-03-10 23:00:48 +0200915 +---------------------------------------------------------+--------------------------------------+------------+
Gregory P. Smith28399852009-03-31 16:54:10 +0000916
Ezio Melottidd7c5932011-03-10 23:00:48 +0200917 .. method:: assertRaises(exception, callable, *args, **kwds)
918 assertRaises(exception)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000919
920 Test that an exception is raised when *callable* is called with any
921 positional or keyword arguments that are also passed to
922 :meth:`assertRaises`. The test passes if *exception* is raised, is an
923 error if another exception is raised, or fails if no exception is raised.
924 To catch any of a group of exceptions, a tuple containing the exception
925 classes may be passed as *exception*.
926
Ezio Melottidd7c5932011-03-10 23:00:48 +0200927 If only the *exception* argument is given, returns a context manager so
928 that the code under test can be written inline rather than as a function::
Benjamin Peterson7233acc2009-03-29 03:31:40 +0000929
Michael Foord1f3fa8a2010-02-05 21:07:38 +0000930 with self.assertRaises(SomeException):
Benjamin Peterson7233acc2009-03-29 03:31:40 +0000931 do_something()
932
Kristján Valur Jónssone2a77982009-08-27 22:20:21 +0000933 The context manager will store the caught exception object in its
Georg Brandldc3694b2010-02-07 17:02:22 +0000934 :attr:`exception` attribute. This can be useful if the intention
Michael Foord1f3fa8a2010-02-05 21:07:38 +0000935 is to perform additional checks on the exception raised::
936
937 with self.assertRaises(SomeException) as cm:
938 do_something()
939
Georg Brandldc3694b2010-02-07 17:02:22 +0000940 the_exception = cm.exception
Michael Foordba7732e2010-02-05 23:28:12 +0000941 self.assertEqual(the_exception.error_code, 3)
Kristján Valur Jónssone2a77982009-08-27 22:20:21 +0000942
Benjamin Peterson99721e02009-03-23 23:10:14 +0000943 .. versionchanged:: 2.7
Benjamin Peterson7233acc2009-03-29 03:31:40 +0000944 Added the ability to use :meth:`assertRaises` as a context manager.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000945
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000946
Ezio Melottidd7c5932011-03-10 23:00:48 +0200947 .. method:: assertRaisesRegexp(exception, regexp, callable, *args, **kwds)
948 assertRaisesRegexp(exception, regexp)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000949
Gregory P. Smith28399852009-03-31 16:54:10 +0000950 Like :meth:`assertRaises` but also tests that *regexp* matches
951 on the string representation of the raised exception. *regexp* may be
952 a regular expression object or a string containing a regular expression
953 suitable for use by :func:`re.search`. Examples::
954
955 self.assertRaisesRegexp(ValueError, 'invalid literal for.*XYZ$',
956 int, 'XYZ')
957
958 or::
959
960 with self.assertRaisesRegexp(ValueError, 'literal'):
961 int('XYZ')
962
963 .. versionadded:: 2.7
964
965
Gregory P. Smith28399852009-03-31 16:54:10 +0000966
Ezio Melottidd7c5932011-03-10 23:00:48 +0200967 There are also other methods used to perform more specific checks, such as:
968
969 +---------------------------------------+--------------------------------+--------------+
970 | Method | Checks that | New in |
971 +=======================================+================================+==============+
972 | :meth:`assertAlmostEqual(a, b) | ``round(a-b, 7) == 0`` | |
973 | <TestCase.assertAlmostEqual>` | | |
974 +---------------------------------------+--------------------------------+--------------+
975 | :meth:`assertNotAlmostEqual(a, b) | ``round(a-b, 7) != 0`` | |
976 | <TestCase.assertNotAlmostEqual>` | | |
977 +---------------------------------------+--------------------------------+--------------+
978 | :meth:`assertGreater(a, b) | ``a > b`` | 2.7 |
979 | <TestCase.assertGreater>` | | |
980 +---------------------------------------+--------------------------------+--------------+
981 | :meth:`assertGreaterEqual(a, b) | ``a >= b`` | 2.7 |
982 | <TestCase.assertGreaterEqual>` | | |
983 +---------------------------------------+--------------------------------+--------------+
984 | :meth:`assertLess(a, b) | ``a < b`` | 2.7 |
985 | <TestCase.assertLess>` | | |
986 +---------------------------------------+--------------------------------+--------------+
987 | :meth:`assertLessEqual(a, b) | ``a <= b`` | 2.7 |
988 | <TestCase.assertLessEqual>` | | |
989 +---------------------------------------+--------------------------------+--------------+
990 | :meth:`assertRegexpMatches(s, re) | ``regex.search(s)`` | 2.7 |
991 | <TestCase.assertRegexpMatches>` | | |
992 +---------------------------------------+--------------------------------+--------------+
993 | :meth:`assertNotRegexpMatches(s, re) | ``not regex.search(s)`` | 2.7 |
994 | <TestCase.assertNotRegexpMatches>` | | |
995 +---------------------------------------+--------------------------------+--------------+
996 | :meth:`assertItemsEqual(a, b) | sorted(a) == sorted(b) and | 2.7 |
997 | <TestCase.assertItemsEqual>` | works with unhashable objs | |
998 +---------------------------------------+--------------------------------+--------------+
999 | :meth:`assertDictContainsSubset(a, b) | all the key/value pairs | 2.7 |
Éric Araujoa7cbe282011-09-01 19:49:31 +02001000 | <TestCase.assertDictContainsSubset>` | in *a* exist in *b* | |
Ezio Melottidd7c5932011-03-10 23:00:48 +02001001 +---------------------------------------+--------------------------------+--------------+
1002
1003
1004 .. method:: assertAlmostEqual(first, second, places=7, msg=None, delta=None)
1005 assertNotAlmostEqual(first, second, places=7, msg=None, delta=None)
1006
1007 Test that *first* and *second* are approximately (or not approximately)
1008 equal by computing the difference, rounding to the given number of
1009 decimal *places* (default 7), and comparing to zero. Note that these
1010 methods round the values to the given number of *decimal places* (i.e.
1011 like the :func:`round` function) and not *significant digits*.
1012
1013 If *delta* is supplied instead of *places* then the difference
1014 between *first* and *second* must be less (or more) than *delta*.
1015
1016 Supplying both *delta* and *places* raises a ``TypeError``.
1017
1018 .. versionchanged:: 2.7
1019 :meth:`assertAlmostEqual` automatically considers almost equal objects
1020 that compare equal. :meth:`assertNotAlmostEqual` automatically fails
1021 if the objects compare equal. Added the *delta* keyword argument.
1022
1023
1024
1025 .. method:: assertGreater(first, second, msg=None)
1026 assertGreaterEqual(first, second, msg=None)
1027 assertLess(first, second, msg=None)
1028 assertLessEqual(first, second, msg=None)
1029
1030 Test that *first* is respectively >, >=, < or <= than *second* depending
1031 on the method name. If not, the test will fail::
1032
1033 >>> self.assertGreaterEqual(3, 4)
1034 AssertionError: "3" unexpectedly not greater than or equal to "4"
Gregory P. Smith28399852009-03-31 16:54:10 +00001035
1036 .. versionadded:: 2.7
1037
1038
Ezio Melottidd7c5932011-03-10 23:00:48 +02001039 .. method:: assertRegexpMatches(text, regexp, msg=None)
Gregory P. Smith28399852009-03-31 16:54:10 +00001040
Ezio Melottidd7c5932011-03-10 23:00:48 +02001041 Test that a *regexp* search matches *text*. In case
1042 of failure, the error message will include the pattern and the *text* (or
1043 the pattern and the part of *text* that unexpectedly matched). *regexp*
1044 may be a regular expression object or a string containing a regular
1045 expression suitable for use by :func:`re.search`.
Gregory P. Smith28399852009-03-31 16:54:10 +00001046
1047 .. versionadded:: 2.7
1048
1049
Ezio Melottidd7c5932011-03-10 23:00:48 +02001050 .. method:: assertNotRegexpMatches(text, regexp, msg=None)
Michael Foordf2dfef12009-04-05 19:19:28 +00001051
Ezio Melottidd7c5932011-03-10 23:00:48 +02001052 Verifies that a *regexp* search does not match *text*. Fails with an error
1053 message including the pattern and the part of *text* that matches. *regexp*
1054 may be a regular expression object or a string containing a regular
1055 expression suitable for use by :func:`re.search`.
Michael Foordf2dfef12009-04-05 19:19:28 +00001056
1057 .. versionadded:: 2.7
1058
1059
Ezio Melottidd7c5932011-03-10 23:00:48 +02001060 .. method:: assertItemsEqual(actual, expected, msg=None)
Michael Foordf2dfef12009-04-05 19:19:28 +00001061
Ezio Melottidd7c5932011-03-10 23:00:48 +02001062 Test that sequence *expected* contains the same elements as *actual*,
1063 regardless of their order. When they don't, an error message listing the
1064 differences between the sequences will be generated.
1065
1066 Duplicate elements are *not* ignored when comparing *actual* and
1067 *expected*. It verifies if each element has the same count in both
1068 sequences. It is the equivalent of ``assertEqual(sorted(expected),
1069 sorted(actual))`` but it works with sequences of unhashable objects as
1070 well.
Michael Foordf2dfef12009-04-05 19:19:28 +00001071
1072 .. versionadded:: 2.7
1073
1074
Ezio Melottidd7c5932011-03-10 23:00:48 +02001075 .. method:: assertDictContainsSubset(expected, actual, msg=None)
Georg Brandlf895cf52009-10-01 20:59:31 +00001076
Ezio Melottidd7c5932011-03-10 23:00:48 +02001077 Tests whether the key/value pairs in dictionary *actual* are a
1078 superset of those in *expected*. If not, an error message listing
1079 the missing keys and mismatched values is generated.
1080
1081 .. versionadded:: 2.7
1082 .. deprecated:: 3.2
1083
1084
1085
1086 .. _type-specific-methods:
1087
1088 The :meth:`assertEqual` method dispatches the equality check for objects of
1089 the same type to different type-specific methods. These methods are already
1090 implemented for most of the built-in types, but it's also possible to
1091 register new methods using :meth:`addTypeEqualityFunc`:
1092
1093 .. method:: addTypeEqualityFunc(typeobj, function)
1094
1095 Registers a type-specific method called by :meth:`assertEqual` to check
1096 if two objects of exactly the same *typeobj* (not subclasses) compare
1097 equal. *function* must take two positional arguments and a third msg=None
1098 keyword argument just as :meth:`assertEqual` does. It must raise
1099 :data:`self.failureException(msg) <failureException>` when inequality
1100 between the first two parameters is detected -- possibly providing useful
1101 information and explaining the inequalities in details in the error
1102 message.
1103
1104 .. versionadded:: 2.7
1105
1106 The list of type-specific methods automatically used by
1107 :meth:`~TestCase.assertEqual` are summarized in the following table. Note
1108 that it's usually not necessary to invoke these methods directly.
1109
1110 +-----------------------------------------+-----------------------------+--------------+
1111 | Method | Used to compare | New in |
1112 +=========================================+=============================+==============+
1113 | :meth:`assertMultiLineEqual(a, b) | strings | 2.7 |
1114 | <TestCase.assertMultiLineEqual>` | | |
1115 +-----------------------------------------+-----------------------------+--------------+
1116 | :meth:`assertSequenceEqual(a, b) | sequences | 2.7 |
1117 | <TestCase.assertSequenceEqual>` | | |
1118 +-----------------------------------------+-----------------------------+--------------+
1119 | :meth:`assertListEqual(a, b) | lists | 2.7 |
1120 | <TestCase.assertListEqual>` | | |
1121 +-----------------------------------------+-----------------------------+--------------+
1122 | :meth:`assertTupleEqual(a, b) | tuples | 2.7 |
1123 | <TestCase.assertTupleEqual>` | | |
1124 +-----------------------------------------+-----------------------------+--------------+
1125 | :meth:`assertSetEqual(a, b) | sets or frozensets | 2.7 |
1126 | <TestCase.assertSetEqual>` | | |
1127 +-----------------------------------------+-----------------------------+--------------+
1128 | :meth:`assertDictEqual(a, b) | dicts | 2.7 |
1129 | <TestCase.assertDictEqual>` | | |
1130 +-----------------------------------------+-----------------------------+--------------+
1131
1132
1133
1134 .. method:: assertMultiLineEqual(first, second, msg=None)
1135
1136 Test that the multiline string *first* is equal to the string *second*.
1137 When not equal a diff of the two strings highlighting the differences
1138 will be included in the error message. This method is used by default
1139 when comparing strings with :meth:`assertEqual`.
Georg Brandlf895cf52009-10-01 20:59:31 +00001140
1141 .. versionadded:: 2.7
1142
1143
Ezio Melottidd7c5932011-03-10 23:00:48 +02001144 .. method:: assertSequenceEqual(seq1, seq2, msg=None, seq_type=None)
Georg Brandlf895cf52009-10-01 20:59:31 +00001145
Ezio Melottidd7c5932011-03-10 23:00:48 +02001146 Tests that two sequences are equal. If a *seq_type* is supplied, both
1147 *seq1* and *seq2* must be instances of *seq_type* or a failure will
1148 be raised. If the sequences are different an error message is
1149 constructed that shows the difference between the two.
1150
1151 This method is not called directly by :meth:`assertEqual`, but
1152 it's used to implement :meth:`assertListEqual` and
1153 :meth:`assertTupleEqual`.
Georg Brandlf895cf52009-10-01 20:59:31 +00001154
1155 .. versionadded:: 2.7
1156
1157
Ezio Melottidd7c5932011-03-10 23:00:48 +02001158 .. method:: assertListEqual(list1, list2, msg=None)
1159 assertTupleEqual(tuple1, tuple2, msg=None)
Gregory P. Smith28399852009-03-31 16:54:10 +00001160
Ezio Melottidd7c5932011-03-10 23:00:48 +02001161 Tests that two lists or tuples are equal. If not an error message is
1162 constructed that shows only the differences between the two. An error
1163 is also raised if either of the parameters are of the wrong type.
1164 These methods are used by default when comparing lists or tuples with
1165 :meth:`assertEqual`.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001166
Ezio Melottidd7c5932011-03-10 23:00:48 +02001167 .. versionadded:: 2.7
Gregory P. Smith65ff0052009-03-31 19:59:14 +00001168
Benjamin Peterson99721e02009-03-23 23:10:14 +00001169
Ezio Melottidd7c5932011-03-10 23:00:48 +02001170 .. method:: assertSetEqual(set1, set2, msg=None)
Benjamin Peterson99721e02009-03-23 23:10:14 +00001171
Ezio Melottidd7c5932011-03-10 23:00:48 +02001172 Tests that two sets are equal. If not, an error message is constructed
1173 that lists the differences between the sets. This method is used by
1174 default when comparing sets or frozensets with :meth:`assertEqual`.
1175
1176 Fails if either of *set1* or *set2* does not have a :meth:`set.difference`
1177 method.
1178
1179 .. versionadded:: 2.7
1180
1181
1182 .. method:: assertDictEqual(expected, actual, msg=None)
1183
1184 Test that two dictionaries are equal. If not, an error message is
1185 constructed that shows the differences in the dictionaries. This
1186 method will be used by default to compare dictionaries in
1187 calls to :meth:`assertEqual`.
1188
1189 .. versionadded:: 2.7
1190
1191
1192
1193 .. _other-methods-and-attrs:
1194
1195 Finally the :class:`TestCase` provides the following methods and attributes:
1196
1197
1198 .. method:: fail(msg=None)
1199
1200 Signals a test failure unconditionally, with *msg* or ``None`` for
Benjamin Peterson99721e02009-03-23 23:10:14 +00001201 the error message.
1202
1203
1204 .. attribute:: failureException
1205
1206 This class attribute gives the exception raised by the test method. If a
1207 test framework needs to use a specialized exception, possibly to carry
1208 additional information, it must subclass this exception in order to "play
1209 fair" with the framework. The initial value of this attribute is
1210 :exc:`AssertionError`.
1211
Michael Foord345b2fe2009-04-02 03:20:38 +00001212
1213 .. attribute:: longMessage
1214
Ezio Melottidd7c5932011-03-10 23:00:48 +02001215 If set to ``True`` then any explicit failure message you pass in to the
1216 :ref:`assert methods <assert-methods>` will be appended to the end of the
1217 normal failure message. The normal messages contain useful information
1218 about the objects involved, for example the message from assertEqual
1219 shows you the repr of the two unequal objects. Setting this attribute
1220 to ``True`` allows you to have a custom error message in addition to the
1221 normal one.
Michael Foord345b2fe2009-04-02 03:20:38 +00001222
Ezio Melottidd7c5932011-03-10 23:00:48 +02001223 This attribute defaults to ``False``, meaning that a custom message passed
Michael Foord345b2fe2009-04-02 03:20:38 +00001224 to an assert method will silence the normal message.
1225
1226 The class setting can be overridden in individual tests by assigning an
Ezio Melottidd7c5932011-03-10 23:00:48 +02001227 instance attribute to ``True`` or ``False`` before calling the assert methods.
Michael Foord345b2fe2009-04-02 03:20:38 +00001228
1229 .. versionadded:: 2.7
1230
1231
Michael Foord8dde2012010-06-05 21:57:03 +00001232 .. attribute:: maxDiff
1233
1234 This attribute controls the maximum length of diffs output by assert
1235 methods that report diffs on failure. It defaults to 80*8 characters.
1236 Assert methods affected by this attribute are
1237 :meth:`assertSequenceEqual` (including all the sequence comparison
1238 methods that delegate to it), :meth:`assertDictEqual` and
1239 :meth:`assertMultiLineEqual`.
1240
1241 Setting ``maxDiff`` to None means that there is no maximum length of
1242 diffs.
1243
1244 .. versionadded:: 2.7
1245
1246
Benjamin Peterson99721e02009-03-23 23:10:14 +00001247 Testing frameworks can use the following methods to collect information on
1248 the test:
1249
1250
1251 .. method:: countTestCases()
1252
1253 Return the number of tests represented by this test object. For
1254 :class:`TestCase` instances, this will always be ``1``.
1255
1256
1257 .. method:: defaultTestResult()
1258
1259 Return an instance of the test result class that should be used for this
1260 test case class (if no other result instance is provided to the
1261 :meth:`run` method).
1262
1263 For :class:`TestCase` instances, this will always be an instance of
1264 :class:`TestResult`; subclasses of :class:`TestCase` should override this
1265 as necessary.
1266
1267
1268 .. method:: id()
1269
1270 Return a string identifying the specific test case. This is usually the
1271 full name of the test method, including the module and class name.
1272
1273
1274 .. method:: shortDescription()
1275
Ezio Melottidd7c5932011-03-10 23:00:48 +02001276 Returns a description of the test, or ``None`` if no description
Gregory P. Smith28399852009-03-31 16:54:10 +00001277 has been provided. The default implementation of this method
1278 returns the first line of the test method's docstring, if available,
Michael Foorddb43b5a2010-02-10 14:25:12 +00001279 or :const:`None`.
Gregory P. Smith28399852009-03-31 16:54:10 +00001280
1281
Gregory P. Smith28399852009-03-31 16:54:10 +00001282
Ezio Melottidd7c5932011-03-10 23:00:48 +02001283 .. method:: addCleanup(function, *args, **kwargs)
Michael Foorde2fb98f2009-05-02 20:15:05 +00001284
1285 Add a function to be called after :meth:`tearDown` to cleanup resources
1286 used during the test. Functions will be called in reverse order to the
1287 order they are added (LIFO). They are called with any arguments and
1288 keyword arguments passed into :meth:`addCleanup` when they are
1289 added.
1290
1291 If :meth:`setUp` fails, meaning that :meth:`tearDown` is not called,
1292 then any cleanup functions added will still be called.
1293
1294 .. versionadded:: 2.7
1295
1296
1297 .. method:: doCleanups()
1298
Barry Warsawfa900d42010-04-12 14:40:49 +00001299 This method is called unconditionally after :meth:`tearDown`, or
Michael Foorde2fb98f2009-05-02 20:15:05 +00001300 after :meth:`setUp` if :meth:`setUp` raises an exception.
1301
1302 It is responsible for calling all the cleanup functions added by
1303 :meth:`addCleanup`. If you need cleanup functions to be called
1304 *prior* to :meth:`tearDown` then you can call :meth:`doCleanups`
1305 yourself.
1306
1307 :meth:`doCleanups` pops methods off the stack of cleanup
1308 functions one at a time, so it can be called at any time.
1309
1310 .. versionadded:: 2.7
1311
1312
Ezio Melottidd7c5932011-03-10 23:00:48 +02001313.. class:: FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001314
1315 This class implements the portion of the :class:`TestCase` interface which
Georg Brandl2fcd1732009-05-30 10:45:40 +00001316 allows the test runner to drive the test, but does not provide the methods
1317 which test code can use to check and report errors. This is used to create
1318 test cases using legacy test code, allowing it to be integrated into a
1319 :mod:`unittest`-based test framework.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001320
1321
Ezio Melottidd7c5932011-03-10 23:00:48 +02001322Deprecated aliases
1323##################
1324
1325For historical reasons, some of the :class:`TestCase` methods had one or more
1326aliases that are now deprecated. The following table lists the correct names
1327along with their deprecated aliases:
1328
1329 ============================== ===============================
1330 Method Name Deprecated alias(es)
1331 ============================== ===============================
1332 :meth:`.assertEqual` failUnlessEqual, assertEquals
1333 :meth:`.assertNotEqual` failIfEqual
1334 :meth:`.assertTrue` failUnless, assert\_
1335 :meth:`.assertFalse` failIf
1336 :meth:`.assertRaises` failUnlessRaises
1337 :meth:`.assertAlmostEqual` failUnlessAlmostEqual
1338 :meth:`.assertNotAlmostEqual` failIfAlmostEqual
1339 ============================== ===============================
1340
1341 .. deprecated:: 2.7
1342 the aliases listed in the second column
1343
1344
1345
Benjamin Peterson99721e02009-03-23 23:10:14 +00001346.. _testsuite-objects:
1347
1348Grouping tests
1349~~~~~~~~~~~~~~
1350
Ezio Melottidd7c5932011-03-10 23:00:48 +02001351.. class:: TestSuite(tests=())
Georg Brandl8ec7f652007-08-15 14:28:01 +00001352
1353 This class represents an aggregation of individual tests cases and test suites.
1354 The class presents the interface needed by the test runner to allow it to be run
1355 as any other test case. Running a :class:`TestSuite` instance is the same as
1356 iterating over the suite, running each test individually.
1357
1358 If *tests* is given, it must be an iterable of individual test cases or other
1359 test suites that will be used to build the suite initially. Additional methods
1360 are provided to add test cases and suites to the collection later on.
1361
Benjamin Peterson176a56c2009-05-25 00:48:58 +00001362 :class:`TestSuite` objects behave much like :class:`TestCase` objects, except
1363 they do not actually implement a test. Instead, they are used to aggregate
1364 tests into groups of tests that should be run together. Some additional
1365 methods are available to add tests to :class:`TestSuite` instances:
Benjamin Peterson99721e02009-03-23 23:10:14 +00001366
1367
1368 .. method:: TestSuite.addTest(test)
1369
1370 Add a :class:`TestCase` or :class:`TestSuite` to the suite.
1371
1372
1373 .. method:: TestSuite.addTests(tests)
1374
1375 Add all the tests from an iterable of :class:`TestCase` and :class:`TestSuite`
1376 instances to this test suite.
1377
Georg Brandl2fcd1732009-05-30 10:45:40 +00001378 This is equivalent to iterating over *tests*, calling :meth:`addTest` for
1379 each element.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001380
1381 :class:`TestSuite` shares the following methods with :class:`TestCase`:
1382
1383
1384 .. method:: run(result)
1385
1386 Run the tests associated with this suite, collecting the result into the
1387 test result object passed as *result*. Note that unlike
1388 :meth:`TestCase.run`, :meth:`TestSuite.run` requires the result object to
1389 be passed in.
1390
1391
1392 .. method:: debug()
1393
1394 Run the tests associated with this suite without collecting the
1395 result. This allows exceptions raised by the test to be propagated to the
1396 caller and can be used to support running tests under a debugger.
1397
1398
1399 .. method:: countTestCases()
1400
1401 Return the number of tests represented by this test object, including all
1402 individual tests and sub-suites.
1403
Georg Brandl9bc66822009-04-27 17:04:23 +00001404
1405 .. method:: __iter__()
1406
1407 Tests grouped by a :class:`TestSuite` are always accessed by iteration.
1408 Subclasses can lazily provide tests by overriding :meth:`__iter__`. Note
1409 that this method maybe called several times on a single suite
1410 (for example when counting tests or comparing for equality)
1411 so the tests returned must be the same for repeated iterations.
1412
1413 .. versionchanged:: 2.7
1414 In earlier versions the :class:`TestSuite` accessed tests directly rather
1415 than through iteration, so overriding :meth:`__iter__` wasn't sufficient
1416 for providing tests.
1417
Benjamin Peterson99721e02009-03-23 23:10:14 +00001418 In the typical usage of a :class:`TestSuite` object, the :meth:`run` method
1419 is invoked by a :class:`TestRunner` rather than by the end-user test harness.
1420
1421
Benjamin Peterson99721e02009-03-23 23:10:14 +00001422Loading and running tests
1423~~~~~~~~~~~~~~~~~~~~~~~~~
1424
Georg Brandl8ec7f652007-08-15 14:28:01 +00001425.. class:: TestLoader()
1426
Benjamin Peterson99721e02009-03-23 23:10:14 +00001427 The :class:`TestLoader` class is used to create test suites from classes and
1428 modules. Normally, there is no need to create an instance of this class; the
1429 :mod:`unittest` module provides an instance that can be shared as
1430 ``unittest.defaultTestLoader``. Using a subclass or instance, however, allows
1431 customization of some configurable properties.
1432
1433 :class:`TestLoader` objects have the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001434
1435
Benjamin Peterson99721e02009-03-23 23:10:14 +00001436 .. method:: loadTestsFromTestCase(testCaseClass)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001437
Benjamin Peterson99721e02009-03-23 23:10:14 +00001438 Return a suite of all tests cases contained in the :class:`TestCase`\ -derived
1439 :class:`testCaseClass`.
1440
1441
1442 .. method:: loadTestsFromModule(module)
1443
1444 Return a suite of all tests cases contained in the given module. This
1445 method searches *module* for classes derived from :class:`TestCase` and
1446 creates an instance of the class for each test method defined for the
1447 class.
1448
Georg Brandl16a57f62009-04-27 15:29:09 +00001449 .. note::
Benjamin Peterson99721e02009-03-23 23:10:14 +00001450
1451 While using a hierarchy of :class:`TestCase`\ -derived classes can be
1452 convenient in sharing fixtures and helper functions, defining test
1453 methods on base classes that are not intended to be instantiated
1454 directly does not play well with this method. Doing so, however, can
1455 be useful when the fixtures are different and defined in subclasses.
1456
Michael Foordb4a81c82009-05-29 20:33:46 +00001457 If a module provides a ``load_tests`` function it will be called to
1458 load the tests. This allows modules to customize test loading.
1459 This is the `load_tests protocol`_.
1460
1461 .. versionchanged:: 2.7
1462 Support for ``load_tests`` added.
1463
Benjamin Peterson99721e02009-03-23 23:10:14 +00001464
Ezio Melottidd7c5932011-03-10 23:00:48 +02001465 .. method:: loadTestsFromName(name, module=None)
Benjamin Peterson99721e02009-03-23 23:10:14 +00001466
1467 Return a suite of all tests cases given a string specifier.
1468
1469 The specifier *name* is a "dotted name" that may resolve either to a
1470 module, a test case class, a test method within a test case class, a
1471 :class:`TestSuite` instance, or a callable object which returns a
1472 :class:`TestCase` or :class:`TestSuite` instance. These checks are
1473 applied in the order listed here; that is, a method on a possible test
1474 case class will be picked up as "a test method within a test case class",
1475 rather than "a callable object".
1476
1477 For example, if you have a module :mod:`SampleTests` containing a
Georg Brandl2fcd1732009-05-30 10:45:40 +00001478 :class:`TestCase`\ -derived class :class:`SampleTestCase` with three test
1479 methods (:meth:`test_one`, :meth:`test_two`, and :meth:`test_three`), the
1480 specifier ``'SampleTests.SampleTestCase'`` would cause this method to
1481 return a suite which will run all three test methods. Using the specifier
1482 ``'SampleTests.SampleTestCase.test_two'`` would cause it to return a test
1483 suite which will run only the :meth:`test_two` test method. The specifier
1484 can refer to modules and packages which have not been imported; they will
1485 be imported as a side-effect.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001486
1487 The method optionally resolves *name* relative to the given *module*.
1488
1489
Ezio Melottidd7c5932011-03-10 23:00:48 +02001490 .. method:: loadTestsFromNames(names, module=None)
Benjamin Peterson99721e02009-03-23 23:10:14 +00001491
1492 Similar to :meth:`loadTestsFromName`, but takes a sequence of names rather
1493 than a single name. The return value is a test suite which supports all
1494 the tests defined for each name.
1495
1496
1497 .. method:: getTestCaseNames(testCaseClass)
1498
1499 Return a sorted sequence of method names found within *testCaseClass*;
1500 this should be a subclass of :class:`TestCase`.
1501
Michael Foordb4a81c82009-05-29 20:33:46 +00001502
1503 .. method:: discover(start_dir, pattern='test*.py', top_level_dir=None)
1504
1505 Find and return all test modules from the specified start directory,
1506 recursing into subdirectories to find them. Only test files that match
Michael Foorde91ea562009-09-13 19:07:03 +00001507 *pattern* will be loaded. (Using shell style pattern matching.) Only
1508 module names that are importable (i.e. are valid Python identifiers) will
1509 be loaded.
Michael Foordb4a81c82009-05-29 20:33:46 +00001510
1511 All test modules must be importable from the top level of the project. If
1512 the start directory is not the top level directory then the top level
1513 directory must be specified separately.
1514
Michael Foorde91ea562009-09-13 19:07:03 +00001515 If importing a module fails, for example due to a syntax error, then this
1516 will be recorded as a single error and discovery will continue.
1517
Michael Foordb4a81c82009-05-29 20:33:46 +00001518 If a test package name (directory with :file:`__init__.py`) matches the
1519 pattern then the package will be checked for a ``load_tests``
1520 function. If this exists then it will be called with *loader*, *tests*,
1521 *pattern*.
1522
Michael Foorddc0460a2009-09-13 19:08:18 +00001523 If load_tests exists then discovery does *not* recurse into the package,
Michael Foordb4a81c82009-05-29 20:33:46 +00001524 ``load_tests`` is responsible for loading all tests in the package.
1525
1526 The pattern is deliberately not stored as a loader attribute so that
1527 packages can continue discovery themselves. *top_level_dir* is stored so
1528 ``load_tests`` does not need to pass this argument in to
1529 ``loader.discover()``.
1530
Michael Foordba097ec2010-04-03 17:03:11 +00001531 *start_dir* can be a dotted module name as well as a directory.
1532
Michael Foord17565e52009-09-27 20:08:23 +00001533 .. versionadded:: 2.7
Michael Foordb4a81c82009-05-29 20:33:46 +00001534
Benjamin Peterson99721e02009-03-23 23:10:14 +00001535 The following attributes of a :class:`TestLoader` can be configured either by
1536 subclassing or assignment on an instance:
1537
1538
1539 .. attribute:: testMethodPrefix
1540
1541 String giving the prefix of method names which will be interpreted as test
1542 methods. The default value is ``'test'``.
1543
1544 This affects :meth:`getTestCaseNames` and all the :meth:`loadTestsFrom\*`
1545 methods.
1546
1547
1548 .. attribute:: sortTestMethodsUsing
1549
1550 Function to be used to compare method names when sorting them in
1551 :meth:`getTestCaseNames` and all the :meth:`loadTestsFrom\*` methods. The
1552 default value is the built-in :func:`cmp` function; the attribute can also
1553 be set to :const:`None` to disable the sort.
1554
1555
1556 .. attribute:: suiteClass
1557
1558 Callable object that constructs a test suite from a list of tests. No
1559 methods on the resulting object are needed. The default value is the
1560 :class:`TestSuite` class.
1561
1562 This affects all the :meth:`loadTestsFrom\*` methods.
1563
1564
Benjamin Peterson99721e02009-03-23 23:10:14 +00001565.. class:: TestResult
1566
1567 This class is used to compile information about which tests have succeeded
1568 and which have failed.
1569
1570 A :class:`TestResult` object stores the results of a set of tests. The
1571 :class:`TestCase` and :class:`TestSuite` classes ensure that results are
1572 properly recorded; test authors do not need to worry about recording the
1573 outcome of tests.
1574
1575 Testing frameworks built on top of :mod:`unittest` may want access to the
1576 :class:`TestResult` object generated by running a set of tests for reporting
1577 purposes; a :class:`TestResult` instance is returned by the
1578 :meth:`TestRunner.run` method for this purpose.
1579
1580 :class:`TestResult` instances have the following attributes that will be of
1581 interest when inspecting the results of running a set of tests:
1582
1583
1584 .. attribute:: errors
1585
1586 A list containing 2-tuples of :class:`TestCase` instances and strings
1587 holding formatted tracebacks. Each tuple represents a test which raised an
1588 unexpected exception.
1589
1590 .. versionchanged:: 2.2
Benjamin Peterson99721e02009-03-23 23:10:14 +00001591 Contains formatted tracebacks instead of :func:`sys.exc_info` results.
1592
1593
1594 .. attribute:: failures
1595
1596 A list containing 2-tuples of :class:`TestCase` instances and strings
1597 holding formatted tracebacks. Each tuple represents a test where a failure
1598 was explicitly signalled using the :meth:`TestCase.fail\*` or
1599 :meth:`TestCase.assert\*` methods.
1600
1601 .. versionchanged:: 2.2
Benjamin Peterson99721e02009-03-23 23:10:14 +00001602 Contains formatted tracebacks instead of :func:`sys.exc_info` results.
1603
1604 .. attribute:: skipped
1605
1606 A list containing 2-tuples of :class:`TestCase` instances and strings
1607 holding the reason for skipping the test.
1608
1609 .. versionadded:: 2.7
1610
1611 .. attribute:: expectedFailures
1612
Georg Brandl09302282010-10-06 09:32:48 +00001613 A list containing 2-tuples of :class:`TestCase` instances and strings
1614 holding formatted tracebacks. Each tuple represents an expected failure
Benjamin Peterson99721e02009-03-23 23:10:14 +00001615 of the test case.
1616
1617 .. attribute:: unexpectedSuccesses
1618
1619 A list containing :class:`TestCase` instances that were marked as expected
1620 failures, but succeeded.
1621
1622 .. attribute:: shouldStop
1623
1624 Set to ``True`` when the execution of tests should stop by :meth:`stop`.
1625
1626
1627 .. attribute:: testsRun
1628
1629 The total number of tests run so far.
1630
1631
Michael Foordba097ec2010-04-03 17:03:11 +00001632 .. attribute:: buffer
1633
1634 If set to true, ``sys.stdout`` and ``sys.stderr`` will be buffered in between
1635 :meth:`startTest` and :meth:`stopTest` being called. Collected output will
1636 only be echoed onto the real ``sys.stdout`` and ``sys.stderr`` if the test
1637 fails or errors. Any output is also attached to the failure / error message.
1638
1639 .. versionadded:: 2.7
1640
1641
1642 .. attribute:: failfast
1643
1644 If set to true :meth:`stop` will be called on the first failure or error,
1645 halting the test run.
1646
1647 .. versionadded:: 2.7
1648
1649
Benjamin Peterson99721e02009-03-23 23:10:14 +00001650 .. method:: wasSuccessful()
1651
Ezio Melottidd7c5932011-03-10 23:00:48 +02001652 Return ``True`` if all tests run so far have passed, otherwise returns
1653 ``False``.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001654
1655
1656 .. method:: stop()
1657
1658 This method can be called to signal that the set of tests being run should
Ezio Melottidd7c5932011-03-10 23:00:48 +02001659 be aborted by setting the :attr:`shouldStop` attribute to ``True``.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001660 :class:`TestRunner` objects should respect this flag and return without
1661 running any additional tests.
1662
1663 For example, this feature is used by the :class:`TextTestRunner` class to
1664 stop the test framework when the user signals an interrupt from the
1665 keyboard. Interactive tools which provide :class:`TestRunner`
1666 implementations can use this in a similar manner.
1667
1668 The following methods of the :class:`TestResult` class are used to maintain
1669 the internal data structures, and may be extended in subclasses to support
1670 additional reporting requirements. This is particularly useful in building
1671 tools which support interactive reporting while tests are being run.
1672
1673
1674 .. method:: startTest(test)
1675
1676 Called when the test case *test* is about to be run.
1677
Benjamin Peterson99721e02009-03-23 23:10:14 +00001678 .. method:: stopTest(test)
1679
1680 Called after the test case *test* has been executed, regardless of the
1681 outcome.
1682
Michael Foord07ef4872009-05-02 22:43:34 +00001683 .. method:: startTestRun(test)
1684
1685 Called once before any tests are executed.
1686
1687 .. versionadded:: 2.7
1688
1689
1690 .. method:: stopTestRun(test)
1691
Ezio Melotti7b4e02c2010-01-27 20:25:11 +00001692 Called once after all tests are executed.
Michael Foord07ef4872009-05-02 22:43:34 +00001693
1694 .. versionadded:: 2.7
1695
1696
Benjamin Peterson99721e02009-03-23 23:10:14 +00001697 .. method:: addError(test, err)
1698
1699 Called when the test case *test* raises an unexpected exception *err* is a
1700 tuple of the form returned by :func:`sys.exc_info`: ``(type, value,
1701 traceback)``.
1702
1703 The default implementation appends a tuple ``(test, formatted_err)`` to
1704 the instance's :attr:`errors` attribute, where *formatted_err* is a
1705 formatted traceback derived from *err*.
1706
1707
1708 .. method:: addFailure(test, err)
1709
Michael Foordb4a81c82009-05-29 20:33:46 +00001710 Called when the test case *test* signals a failure. *err* is a tuple of
1711 the form returned by :func:`sys.exc_info`: ``(type, value, traceback)``.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001712
1713 The default implementation appends a tuple ``(test, formatted_err)`` to
1714 the instance's :attr:`failures` attribute, where *formatted_err* is a
1715 formatted traceback derived from *err*.
1716
1717
1718 .. method:: addSuccess(test)
1719
1720 Called when the test case *test* succeeds.
1721
1722 The default implementation does nothing.
1723
1724
1725 .. method:: addSkip(test, reason)
1726
1727 Called when the test case *test* is skipped. *reason* is the reason the
1728 test gave for skipping.
1729
1730 The default implementation appends a tuple ``(test, reason)`` to the
1731 instance's :attr:`skipped` attribute.
1732
1733
1734 .. method:: addExpectedFailure(test, err)
1735
1736 Called when the test case *test* fails, but was marked with the
1737 :func:`expectedFailure` decorator.
1738
1739 The default implementation appends a tuple ``(test, formatted_err)`` to
1740 the instance's :attr:`expectedFailures` attribute, where *formatted_err*
1741 is a formatted traceback derived from *err*.
1742
1743
1744 .. method:: addUnexpectedSuccess(test)
1745
1746 Called when the test case *test* was marked with the
1747 :func:`expectedFailure` decorator, but succeeded.
1748
1749 The default implementation appends the test to the instance's
1750 :attr:`unexpectedSuccesses` attribute.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001751
Michael Foorddb43b5a2010-02-10 14:25:12 +00001752.. class:: TextTestResult(stream, descriptions, verbosity)
1753
1754 A concrete implementation of :class:`TestResult` used by the
1755 :class:`TextTestRunner`.
1756
1757 .. versionadded:: 2.7
1758 This class was previously named ``_TextTestResult``. The old name still
1759 exists as an alias but is deprecated.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001760
1761.. data:: defaultTestLoader
1762
1763 Instance of the :class:`TestLoader` class intended to be shared. If no
1764 customization of the :class:`TestLoader` is needed, this instance can be used
1765 instead of repeatedly creating new instances.
1766
1767
Ezio Melottidd7c5932011-03-10 23:00:48 +02001768.. class:: TextTestRunner(stream=sys.stderr, descriptions=True, verbosity=1)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001769
1770 A basic test runner implementation which prints results on standard error. It
1771 has a few configurable parameters, but is essentially very simple. Graphical
1772 applications which run test suites should provide alternate implementations.
1773
Georg Brandl9bc66822009-04-27 17:04:23 +00001774 .. method:: _makeResult()
1775
1776 This method returns the instance of ``TestResult`` used by :meth:`run`.
1777 It is not intended to be called directly, but can be overridden in
1778 subclasses to provide a custom ``TestResult``.
1779
Michael Foorddb43b5a2010-02-10 14:25:12 +00001780 ``_makeResult()`` instantiates the class or callable passed in the
1781 ``TextTestRunner`` constructor as the ``resultclass`` argument. It
Michael Foordefc2f492010-04-08 04:33:20 +00001782 defaults to :class:`TextTestResult` if no ``resultclass`` is provided.
Michael Foorddb43b5a2010-02-10 14:25:12 +00001783 The result class is instantiated with the following arguments::
1784
1785 stream, descriptions, verbosity
1786
Georg Brandl8ec7f652007-08-15 14:28:01 +00001787
Michael Foordddb20df2010-04-04 23:28:44 +00001788.. function:: main([module[, defaultTest[, argv[, testRunner[, testLoader[, exit[, verbosity[, failfast[, catchbreak[,buffer]]]]]]]]]])
Michael Foord55430352010-04-05 00:39:50 +00001789
Georg Brandl8ec7f652007-08-15 14:28:01 +00001790 A command-line program that runs a set of tests; this is primarily for making
1791 test modules conveniently executable. The simplest use for this function is to
1792 include the following line at the end of a test script::
1793
1794 if __name__ == '__main__':
1795 unittest.main()
1796
Michael Foord5d31e052009-05-11 17:59:43 +00001797 You can run tests with more detailed information by passing in the verbosity
1798 argument::
1799
1800 if __name__ == '__main__':
1801 unittest.main(verbosity=2)
1802
Georg Brandl8ec7f652007-08-15 14:28:01 +00001803 The *testRunner* argument can either be a test runner class or an already
Michael Foord829f6b82009-05-02 11:43:06 +00001804 created instance of it. By default ``main`` calls :func:`sys.exit` with
1805 an exit code indicating success or failure of the tests run.
1806
1807 ``main`` supports being used from the interactive interpreter by passing in the
1808 argument ``exit=False``. This displays the result on standard output without
1809 calling :func:`sys.exit`::
1810
1811 >>> from unittest import main
1812 >>> main(module='test_module', exit=False)
1813
Michael Foordddb20df2010-04-04 23:28:44 +00001814 The ``failfast``, ``catchbreak`` and ``buffer`` parameters have the same
Éric Araujoa8132ec2010-12-16 03:53:53 +00001815 effect as the same-name `command-line options`_.
Michael Foordddb20df2010-04-04 23:28:44 +00001816
Michael Foord829f6b82009-05-02 11:43:06 +00001817 Calling ``main`` actually returns an instance of the ``TestProgram`` class.
1818 This stores the result of the tests run as the ``result`` attribute.
1819
1820 .. versionchanged:: 2.7
Michael Foordddb20df2010-04-04 23:28:44 +00001821 The ``exit``, ``verbosity``, ``failfast``, ``catchbreak`` and ``buffer``
1822 parameters were added.
Michael Foordb4a81c82009-05-29 20:33:46 +00001823
1824
1825load_tests Protocol
1826###################
1827
Michael Foord17565e52009-09-27 20:08:23 +00001828.. versionadded:: 2.7
1829
Michael Foordb4a81c82009-05-29 20:33:46 +00001830Modules or packages can customize how tests are loaded from them during normal
1831test runs or test discovery by implementing a function called ``load_tests``.
1832
1833If a test module defines ``load_tests`` it will be called by
1834:meth:`TestLoader.loadTestsFromModule` with the following arguments::
1835
1836 load_tests(loader, standard_tests, None)
1837
1838It should return a :class:`TestSuite`.
1839
1840*loader* is the instance of :class:`TestLoader` doing the loading.
1841*standard_tests* are the tests that would be loaded by default from the
1842module. It is common for test modules to only want to add or remove tests
1843from the standard set of tests.
1844The third argument is used when loading packages as part of test discovery.
1845
1846A typical ``load_tests`` function that loads tests from a specific set of
1847:class:`TestCase` classes may look like::
1848
1849 test_cases = (TestCase1, TestCase2, TestCase3)
1850
1851 def load_tests(loader, tests, pattern):
1852 suite = TestSuite()
1853 for test_class in test_cases:
1854 tests = loader.loadTestsFromTestCase(test_class)
1855 suite.addTests(tests)
1856 return suite
1857
1858If discovery is started, either from the command line or by calling
1859:meth:`TestLoader.discover`, with a pattern that matches a package
1860name then the package :file:`__init__.py` will be checked for ``load_tests``.
1861
1862.. note::
1863
Ezio Melotti062d2b52009-12-19 22:41:49 +00001864 The default pattern is 'test*.py'. This matches all Python files
Michael Foordb4a81c82009-05-29 20:33:46 +00001865 that start with 'test' but *won't* match any test directories.
1866
1867 A pattern like 'test*' will match test packages as well as
1868 modules.
1869
1870If the package :file:`__init__.py` defines ``load_tests`` then it will be
1871called and discovery not continued into the package. ``load_tests``
1872is called with the following arguments::
1873
1874 load_tests(loader, standard_tests, pattern)
1875
1876This should return a :class:`TestSuite` representing all the tests
1877from the package. (``standard_tests`` will only contain tests
1878collected from :file:`__init__.py`.)
1879
1880Because the pattern is passed into ``load_tests`` the package is free to
1881continue (and potentially modify) test discovery. A 'do nothing'
1882``load_tests`` function for a test package would look like::
1883
1884 def load_tests(loader, standard_tests, pattern):
1885 # top level directory cached on loader instance
1886 this_dir = os.path.dirname(__file__)
1887 package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
1888 standard_tests.addTests(package_tests)
1889 return standard_tests
Michael Foordba097ec2010-04-03 17:03:11 +00001890
1891
1892Class and Module Fixtures
1893-------------------------
1894
Michael Foord09e29802010-04-04 22:41:54 +00001895Class and module level fixtures are implemented in :class:`TestSuite`. When
1896the test suite encounters a test from a new class then :meth:`tearDownClass`
1897from the previous class (if there is one) is called, followed by
1898:meth:`setUpClass` from the new class.
Michael Foordba097ec2010-04-03 17:03:11 +00001899
Michael Foord09e29802010-04-04 22:41:54 +00001900Similarly if a test is from a different module from the previous test then
1901``tearDownModule`` from the previous module is run, followed by
1902``setUpModule`` from the new module.
Michael Foordba097ec2010-04-03 17:03:11 +00001903
Michael Foord09e29802010-04-04 22:41:54 +00001904After all the tests have run the final ``tearDownClass`` and
1905``tearDownModule`` are run.
Michael Foordba097ec2010-04-03 17:03:11 +00001906
Michael Foord09e29802010-04-04 22:41:54 +00001907Note that shared fixtures do not play well with [potential] features like test
1908parallelization and they break test isolation. They should be used with care.
Michael Foordba097ec2010-04-03 17:03:11 +00001909
Michael Foord09e29802010-04-04 22:41:54 +00001910The default ordering of tests created by the unittest test loaders is to group
1911all tests from the same modules and classes together. This will lead to
1912``setUpClass`` / ``setUpModule`` (etc) being called exactly once per class and
1913module. If you randomize the order, so that tests from different modules and
1914classes are adjacent to each other, then these shared fixture functions may be
1915called multiple times in a single test run.
Michael Foordba097ec2010-04-03 17:03:11 +00001916
Michael Foord09e29802010-04-04 22:41:54 +00001917Shared fixtures are not intended to work with suites with non-standard
1918ordering. A ``BaseTestSuite`` still exists for frameworks that don't want to
1919support shared fixtures.
Michael Foordba097ec2010-04-03 17:03:11 +00001920
Michael Foord09e29802010-04-04 22:41:54 +00001921If there are any exceptions raised during one of the shared fixture functions
1922the test is reported as an error. Because there is no corresponding test
1923instance an ``_ErrorHolder`` object (that has the same interface as a
1924:class:`TestCase`) is created to represent the error. If you are just using
1925the standard unittest test runner then this detail doesn't matter, but if you
1926are a framework author it may be relevant.
Michael Foordba097ec2010-04-03 17:03:11 +00001927
1928
1929setUpClass and tearDownClass
1930~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1931
1932These must be implemented as class methods::
1933
1934 import unittest
1935
1936 class Test(unittest.TestCase):
1937 @classmethod
1938 def setUpClass(cls):
1939 cls._connection = createExpensiveConnectionObject()
1940
1941 @classmethod
1942 def tearDownClass(cls):
1943 cls._connection.destroy()
1944
Michael Foord09e29802010-04-04 22:41:54 +00001945If you want the ``setUpClass`` and ``tearDownClass`` on base classes called
1946then you must call up to them yourself. The implementations in
1947:class:`TestCase` are empty.
Michael Foordba097ec2010-04-03 17:03:11 +00001948
Michael Foord09e29802010-04-04 22:41:54 +00001949If an exception is raised during a ``setUpClass`` then the tests in the class
1950are not run and the ``tearDownClass`` is not run. Skipped classes will not
Michael Foord8dde2012010-06-05 21:57:03 +00001951have ``setUpClass`` or ``tearDownClass`` run. If the exception is a
1952``SkipTest`` exception then the class will be reported as having been skipped
1953instead of as an error.
Michael Foordba097ec2010-04-03 17:03:11 +00001954
1955
1956setUpModule and tearDownModule
1957~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1958
1959These should be implemented as functions::
1960
1961 def setUpModule():
1962 createConnection()
1963
1964 def tearDownModule():
1965 closeConnection()
1966
Michael Foord09e29802010-04-04 22:41:54 +00001967If an exception is raised in a ``setUpModule`` then none of the tests in the
Michael Foord8dde2012010-06-05 21:57:03 +00001968module will be run and the ``tearDownModule`` will not be run. If the exception is a
1969``SkipTest`` exception then the module will be reported as having been skipped
1970instead of as an error.
Michael Foordba097ec2010-04-03 17:03:11 +00001971
1972
Michael Foord55430352010-04-05 00:39:50 +00001973Signal Handling
1974---------------
1975
Éric Araujoa8132ec2010-12-16 03:53:53 +00001976The :option:`-c/--catch <unittest -c>` command-line option to unittest,
1977along with the ``catchbreak`` parameter to :func:`unittest.main()`, provide
1978more friendly handling of control-C during a test run. With catch break
1979behavior enabled control-C will allow the currently running test to complete,
1980and the test run will then end and report all the results so far. A second
1981control-c will raise a :exc:`KeyboardInterrupt` in the usual way.
Michael Foord55430352010-04-05 00:39:50 +00001982
Michael Foord5c322ec2010-04-25 19:02:46 +00001983The control-c handling signal handler attempts to remain compatible with code or
1984tests that install their own :const:`signal.SIGINT` handler. If the ``unittest``
1985handler is called but *isn't* the installed :const:`signal.SIGINT` handler,
1986i.e. it has been replaced by the system under test and delegated to, then it
1987calls the default handler. This will normally be the expected behavior by code
1988that replaces an installed handler and delegates to it. For individual tests
1989that need ``unittest`` control-c handling disabled the :func:`removeHandler`
1990decorator can be used.
1991
1992There are a few utility functions for framework authors to enable control-c
1993handling functionality within test frameworks.
Michael Foord55430352010-04-05 00:39:50 +00001994
1995.. function:: installHandler()
1996
Michael Foord31655032010-04-05 10:26:26 +00001997 Install the control-c handler. When a :const:`signal.SIGINT` is received
1998 (usually in response to the user pressing control-c) all registered results
Michael Foord55430352010-04-05 00:39:50 +00001999 have :meth:`~TestResult.stop` called.
2000
Michael Foord47b54402010-04-26 23:36:47 +00002001 .. versionadded:: 2.7
2002
Michael Foord55430352010-04-05 00:39:50 +00002003.. function:: registerResult(result)
2004
Michael Foord31655032010-04-05 10:26:26 +00002005 Register a :class:`TestResult` object for control-c handling. Registering a
Michael Foord55430352010-04-05 00:39:50 +00002006 result stores a weak reference to it, so it doesn't prevent the result from
2007 being garbage collected.
2008
Michael Foord5c322ec2010-04-25 19:02:46 +00002009 Registering a :class:`TestResult` object has no side-effects if control-c
2010 handling is not enabled, so test frameworks can unconditionally register
2011 all results they create independently of whether or not handling is enabled.
2012
Michael Foord47b54402010-04-26 23:36:47 +00002013 .. versionadded:: 2.7
2014
Michael Foord55430352010-04-05 00:39:50 +00002015.. function:: removeResult(result)
2016
Michael Foord31655032010-04-05 10:26:26 +00002017 Remove a registered result. Once a result has been removed then
Michael Foordd341ec82010-04-05 10:30:14 +00002018 :meth:`~TestResult.stop` will no longer be called on that result object in
Michael Foord31655032010-04-05 10:26:26 +00002019 response to a control-c.
Michael Foord55430352010-04-05 00:39:50 +00002020
Michael Foord47b54402010-04-26 23:36:47 +00002021 .. versionadded:: 2.7
2022
Michael Foord5c322ec2010-04-25 19:02:46 +00002023.. function:: removeHandler(function=None)
2024
2025 When called without arguments this function removes the control-c handler
2026 if it has been installed. This function can also be used as a test decorator
2027 to temporarily remove the handler whilst the test is being executed::
2028
2029 @unittest.removeHandler
2030 def test_signal_handling(self):
2031 ...
2032
Michael Foord47b54402010-04-26 23:36:47 +00002033 .. versionadded:: 2.7
2034