blob: 460b2f32a73bfe9a9dbd720a750c75253ee7c489 [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
11
12.. versionadded:: 2.1
Benjamin Peterson692428e2009-03-23 21:50:21 +000013
Benjamin Peterson99721e02009-03-23 23:10:14 +000014.. versionchanged:: 2.7
15 Added test :ref:`skipping and expected failures <unittest-skipping>`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000016
17The Python unit testing framework, sometimes referred to as "PyUnit," is a
18Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in
19turn, a Java version of Kent's Smalltalk testing framework. Each is the de
20facto standard unit testing framework for its respective language.
21
22:mod:`unittest` supports test automation, sharing of setup and shutdown code for
23tests, aggregation of tests into collections, and independence of the tests from
24the reporting framework. The :mod:`unittest` module provides classes that make
25it easy to support these qualities for a set of tests.
26
27To achieve this, :mod:`unittest` supports some important concepts:
28
29test fixture
30 A :dfn:`test fixture` represents the preparation needed to perform one or more
31 tests, and any associate cleanup actions. This may involve, for example,
32 creating temporary or proxy databases, directories, or starting a server
33 process.
34
35test case
36 A :dfn:`test case` is the smallest unit of testing. It checks for a specific
37 response to a particular set of inputs. :mod:`unittest` provides a base class,
38 :class:`TestCase`, which may be used to create new test cases.
39
40test suite
41 A :dfn:`test suite` is a collection of test cases, test suites, or both. It is
42 used to aggregate tests that should be executed together.
43
44test runner
45 A :dfn:`test runner` is a component which orchestrates the execution of tests
46 and provides the outcome to the user. The runner may use a graphical interface,
47 a textual interface, or return a special value to indicate the results of
48 executing the tests.
49
50The test case and test fixture concepts are supported through the
51:class:`TestCase` and :class:`FunctionTestCase` classes; the former should be
52used when creating new tests, and the latter can be used when integrating
53existing test code with a :mod:`unittest`\ -driven framework. When building test
Benjamin Peterson99721e02009-03-23 23:10:14 +000054fixtures using :class:`TestCase`, the :meth:`~TestCase.setUp` and
55:meth:`~TestCase.tearDown` methods can be overridden to provide initialization
56and cleanup for the fixture. With :class:`FunctionTestCase`, existing functions
57can be passed to the constructor for these purposes. When the test is run, the
58fixture initialization is run first; if it succeeds, the cleanup method is run
59after the test has been executed, regardless of the outcome of the test. Each
60instance of the :class:`TestCase` will only be used to run a single test method,
61so a new fixture is created for each test.
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63Test suites are implemented by the :class:`TestSuite` class. This class allows
64individual tests and test suites to be aggregated; when the suite is executed,
Benjamin Peterson176a56c2009-05-25 00:48:58 +000065all tests added directly to the suite and in "child" test suites are run.
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
Benjamin Peterson99721e02009-03-23 23:10:14 +000067A test runner is an object that provides a single method,
68:meth:`~TestRunner.run`, which accepts a :class:`TestCase` or :class:`TestSuite`
69object as a parameter, and returns a result object. The class
70:class:`TestResult` is provided for use as the result object. :mod:`unittest`
71provides the :class:`TextTestRunner` as an example test runner which reports
72test results on the standard error stream by default. Alternate runners can be
73implemented for other environments (such as graphical environments) without any
74need to derive from a specific class.
Georg Brandl8ec7f652007-08-15 14:28:01 +000075
76
77.. seealso::
78
79 Module :mod:`doctest`
80 Another test-support module with a very different flavor.
81
Georg Brandld198b762009-05-31 14:15:25 +000082 `Simple Smalltalk Testing: With Patterns <http://www.XProgramming.com/testfram.htm>`_
Georg Brandl2fcd1732009-05-30 10:45:40 +000083 Kent Beck's original paper on testing frameworks using the pattern shared
84 by :mod:`unittest`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000085
Georg Brandld198b762009-05-31 14:15:25 +000086 `Nose <http://code.google.com/p/python-nose/>`_ and `py.test <http://pytest.org>`_
Georg Brandl2fcd1732009-05-30 10:45:40 +000087 Third-party unittest frameworks with a lighter-weight syntax for writing
88 tests. For example, ``assert func(10) == 42``.
Raymond Hettinger21b617b2009-03-24 00:17:11 +000089
Georg Brandld198b762009-05-31 14:15:25 +000090 `python-mock <http://python-mock.sourceforge.net/>`_ and `minimock <http://blog.ianbicking.org/minimock.html>`_
Georg Brandl2fcd1732009-05-30 10:45:40 +000091 Tools for creating mock test objects (objects simulating external
92 resources).
Georg Brandl8ec7f652007-08-15 14:28:01 +000093
Michael Foordb4a81c82009-05-29 20:33:46 +000094
Michael Foordb4a81c82009-05-29 20:33:46 +000095
Michael Foord5d31e052009-05-11 17:59:43 +000096
Georg Brandl8ec7f652007-08-15 14:28:01 +000097.. _unittest-minimal-example:
98
99Basic example
100-------------
101
102The :mod:`unittest` module provides a rich set of tools for constructing and
103running tests. This section demonstrates that a small subset of the tools
104suffice to meet the needs of most users.
105
106Here is a short script to test three functions from the :mod:`random` module::
107
108 import random
109 import unittest
110
111 class TestSequenceFunctions(unittest.TestCase):
112
113 def setUp(self):
114 self.seq = range(10)
115
Benjamin Peterson99721e02009-03-23 23:10:14 +0000116 def test_shuffle(self):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117 # make sure the shuffled sequence does not lose any elements
118 random.shuffle(self.seq)
119 self.seq.sort()
120 self.assertEqual(self.seq, range(10))
121
Benjamin Peterson99721e02009-03-23 23:10:14 +0000122 def test_choice(self):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000123 element = random.choice(self.seq)
124 self.assert_(element in self.seq)
125
Benjamin Peterson99721e02009-03-23 23:10:14 +0000126 def test_sample(self):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127 self.assertRaises(ValueError, random.sample, self.seq, 20)
128 for element in random.sample(self.seq, 5):
129 self.assert_(element in self.seq)
130
131 if __name__ == '__main__':
132 unittest.main()
133
Benjamin Peterson99721e02009-03-23 23:10:14 +0000134A testcase is created by subclassing :class:`unittest.TestCase`. The three
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135individual tests are defined with methods whose names start with the letters
136``test``. This naming convention informs the test runner about which methods
137represent tests.
138
Benjamin Peterson99721e02009-03-23 23:10:14 +0000139The crux of each test is a call to :meth:`~TestCase.assertEqual` to check for an
140expected result; :meth:`~TestCase.assert_` to verify a condition; or
141:meth:`~TestCase.assertRaises` to verify that an expected exception gets raised.
142These methods are used instead of the :keyword:`assert` statement so the test
143runner can accumulate all test results and produce a report.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000144
Benjamin Peterson99721e02009-03-23 23:10:14 +0000145When a :meth:`~TestCase.setUp` method is defined, the test runner will run that
146method prior to each test. Likewise, if a :meth:`~TestCase.tearDown` method is
147defined, the test runner will invoke that method after each test. In the
148example, :meth:`~TestCase.setUp` was used to create a fresh sequence for each
149test.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150
151The final block shows a simple way to run the tests. :func:`unittest.main`
152provides a command line interface to the test script. When run from the command
153line, the above script produces an output that looks like this::
154
155 ...
156 ----------------------------------------------------------------------
157 Ran 3 tests in 0.000s
158
159 OK
160
161Instead of :func:`unittest.main`, there are other ways to run the tests with a
162finer level of control, less terse output, and no requirement to be run from the
163command line. For example, the last two lines may be replaced with::
164
165 suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
166 unittest.TextTestRunner(verbosity=2).run(suite)
167
168Running the revised script from the interpreter or another script produces the
169following output::
170
171 testchoice (__main__.TestSequenceFunctions) ... ok
172 testsample (__main__.TestSequenceFunctions) ... ok
173 testshuffle (__main__.TestSequenceFunctions) ... ok
174
175 ----------------------------------------------------------------------
176 Ran 3 tests in 0.110s
177
178 OK
179
180The above examples show the most commonly used :mod:`unittest` features which
181are sufficient to meet many everyday testing needs. The remainder of the
182documentation explores the full feature set from first principles.
183
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000184
185.. _unittest-command-line-interface:
186
187Command Line Interface
188----------------------
189
190The unittest module can be used from the command line to run tests from
191modules, classes or even individual test methods::
192
193 python -m unittest test_module1 test_module2
194 python -m unittest test_module.TestClass
195 python -m unittest test_module.TestClass.test_method
196
197You can pass in a list with any combination of module names, and fully
198qualified class or method names.
199
200You can run tests with more detail (higher verbosity) by passing in the -v flag::
201
Ezio Melotti062d2b52009-12-19 22:41:49 +0000202 python -m unittest -v test_module
Raymond Hettingerb09f1982009-05-29 21:20:41 +0000203
204For a list of all the command line options::
205
206 python -m unittest -h
207
208.. versionchanged:: 2.7
209 In earlier versions it was only possible to run individual test methods and
210 not modules or classes.
211
212The command line can also be used for test discovery, for running all of the
213tests in a project or just a subset.
214
215
216.. _unittest-test-discovery:
217
218Test Discovery
219--------------
220
221.. versionadded:: 2.7
222
223Unittest supports simple test discovery. For a project's tests to be
224compatible with test discovery they must all be importable from the top level
225directory of the project (in other words, they must all be in Python packages).
226
227Test discovery is implemented in :meth:`TestLoader.discover`, but can also be
228used from the command line. The basic command line usage is::
229
230 cd project_directory
231 python -m unittest discover
232
233The ``discover`` sub-command has the following options:
234
235 -v, --verbose Verbose output
236 -s directory Directory to start discovery ('.' default)
237 -p pattern Pattern to match test files ('test*.py' default)
238 -t directory Top level directory of project (default to
239 start directory)
240
241The -s, -p, & -t options can be passsed in as positional arguments. The
242following two command lines are equivalent::
243
244 python -m unittest -s project_directory -p '*_test.py'
245 python -m unittest project_directory '*_test.py'
246
247Test modules and packages can customize test loading and discovery by through
248the `load_tests protocol`_.
249
250
Georg Brandl8ec7f652007-08-15 14:28:01 +0000251.. _organizing-tests:
252
253Organizing test code
254--------------------
255
256The basic building blocks of unit testing are :dfn:`test cases` --- single
257scenarios that must be set up and checked for correctness. In :mod:`unittest`,
258test cases are represented by instances of :mod:`unittest`'s :class:`TestCase`
259class. To make your own test cases you must write subclasses of
260:class:`TestCase`, or use :class:`FunctionTestCase`.
261
262An instance of a :class:`TestCase`\ -derived class is an object that can
263completely run a single test method, together with optional set-up and tidy-up
264code.
265
266The testing code of a :class:`TestCase` instance should be entirely self
267contained, such that it can be run either in isolation or in arbitrary
268combination with any number of other test cases.
269
Benjamin Peterson99721e02009-03-23 23:10:14 +0000270The simplest :class:`TestCase` subclass will simply override the
271:meth:`~TestCase.runTest` method in order to perform specific testing code::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000272
273 import unittest
274
275 class DefaultWidgetSizeTestCase(unittest.TestCase):
276 def runTest(self):
277 widget = Widget('The widget')
278 self.assertEqual(widget.size(), (50, 50), 'incorrect default size')
279
Gregory P. Smith28399852009-03-31 16:54:10 +0000280Note that in order to test something, we use the one of the :meth:`assert\*`
Georg Brandl2fcd1732009-05-30 10:45:40 +0000281methods provided by the :class:`TestCase` base class. If the test fails, an
282exception will be raised, and :mod:`unittest` will identify the test case as a
283:dfn:`failure`. Any other exceptions will be treated as :dfn:`errors`. This
284helps you identify where the problem is: :dfn:`failures` are caused by incorrect
285results - a 5 where you expected a 6. :dfn:`Errors` are caused by incorrect
286code - e.g., a :exc:`TypeError` caused by an incorrect function call.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000287
288The way to run a test case will be described later. For now, note that to
289construct an instance of such a test case, we call its constructor without
290arguments::
291
292 testCase = DefaultWidgetSizeTestCase()
293
294Now, such test cases can be numerous, and their set-up can be repetitive. In
295the above case, constructing a :class:`Widget` in each of 100 Widget test case
296subclasses would mean unsightly duplication.
297
298Luckily, we can factor out such set-up code by implementing a method called
Benjamin Peterson99721e02009-03-23 23:10:14 +0000299:meth:`~TestCase.setUp`, which the testing framework will automatically call for
300us when we run the test::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000301
302 import unittest
303
304 class SimpleWidgetTestCase(unittest.TestCase):
305 def setUp(self):
306 self.widget = Widget('The widget')
307
308 class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
309 def runTest(self):
Gregory P. Smith28399852009-03-31 16:54:10 +0000310 self.assertTrue(self.widget.size() == (50,50),
Georg Brandl8ec7f652007-08-15 14:28:01 +0000311 'incorrect default size')
312
313 class WidgetResizeTestCase(SimpleWidgetTestCase):
314 def runTest(self):
315 self.widget.resize(100,150)
Gregory P. Smith28399852009-03-31 16:54:10 +0000316 self.assertTrue(self.widget.size() == (100,150),
Georg Brandl8ec7f652007-08-15 14:28:01 +0000317 'wrong size after resize')
318
Benjamin Peterson99721e02009-03-23 23:10:14 +0000319If the :meth:`~TestCase.setUp` method raises an exception while the test is
320running, the framework will consider the test to have suffered an error, and the
321:meth:`~TestCase.runTest` method will not be executed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000322
Benjamin Peterson99721e02009-03-23 23:10:14 +0000323Similarly, we can provide a :meth:`~TestCase.tearDown` method that tidies up
324after the :meth:`~TestCase.runTest` method has been run::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000325
326 import unittest
327
328 class SimpleWidgetTestCase(unittest.TestCase):
329 def setUp(self):
330 self.widget = Widget('The widget')
331
332 def tearDown(self):
333 self.widget.dispose()
334 self.widget = None
335
Benjamin Peterson99721e02009-03-23 23:10:14 +0000336If :meth:`~TestCase.setUp` succeeded, the :meth:`~TestCase.tearDown` method will
337be run whether :meth:`~TestCase.runTest` succeeded or not.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000338
339Such a working environment for the testing code is called a :dfn:`fixture`.
340
341Often, many small test cases will use the same fixture. In this case, we would
342end up subclassing :class:`SimpleWidgetTestCase` into many small one-method
343classes such as :class:`DefaultWidgetSizeTestCase`. This is time-consuming and
Georg Brandl8ec7f652007-08-15 14:28:01 +0000344discouraging, so in the same vein as JUnit, :mod:`unittest` provides a simpler
345mechanism::
346
347 import unittest
348
349 class WidgetTestCase(unittest.TestCase):
350 def setUp(self):
351 self.widget = Widget('The widget')
352
353 def tearDown(self):
354 self.widget.dispose()
355 self.widget = None
356
357 def testDefaultSize(self):
Gregory P. Smith28399852009-03-31 16:54:10 +0000358 self.assertTrue(self.widget.size() == (50,50),
Georg Brandl8ec7f652007-08-15 14:28:01 +0000359 'incorrect default size')
360
361 def testResize(self):
362 self.widget.resize(100,150)
Gregory P. Smith28399852009-03-31 16:54:10 +0000363 self.assertTrue(self.widget.size() == (100,150),
Georg Brandl8ec7f652007-08-15 14:28:01 +0000364 'wrong size after resize')
365
Benjamin Peterson99721e02009-03-23 23:10:14 +0000366Here we have not provided a :meth:`~TestCase.runTest` method, but have instead
367provided two different test methods. Class instances will now each run one of
368the :meth:`test\*` methods, with ``self.widget`` created and destroyed
369separately for each instance. When creating an instance we must specify the
370test method it is to run. We do this by passing the method name in the
371constructor::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000372
373 defaultSizeTestCase = WidgetTestCase('testDefaultSize')
374 resizeTestCase = WidgetTestCase('testResize')
375
376Test case instances are grouped together according to the features they test.
377:mod:`unittest` provides a mechanism for this: the :dfn:`test suite`,
378represented by :mod:`unittest`'s :class:`TestSuite` class::
379
380 widgetTestSuite = unittest.TestSuite()
381 widgetTestSuite.addTest(WidgetTestCase('testDefaultSize'))
382 widgetTestSuite.addTest(WidgetTestCase('testResize'))
383
384For the ease of running tests, as we will see later, it is a good idea to
385provide in each test module a callable object that returns a pre-built test
386suite::
387
388 def suite():
389 suite = unittest.TestSuite()
390 suite.addTest(WidgetTestCase('testDefaultSize'))
391 suite.addTest(WidgetTestCase('testResize'))
392 return suite
393
394or even::
395
396 def suite():
397 tests = ['testDefaultSize', 'testResize']
398
399 return unittest.TestSuite(map(WidgetTestCase, tests))
400
401Since it is a common pattern to create a :class:`TestCase` subclass with many
402similarly named test functions, :mod:`unittest` provides a :class:`TestLoader`
403class that can be used to automate the process of creating a test suite and
404populating it with individual tests. For example, ::
405
406 suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
407
408will create a test suite that will run ``WidgetTestCase.testDefaultSize()`` and
409``WidgetTestCase.testResize``. :class:`TestLoader` uses the ``'test'`` method
410name prefix to identify test methods automatically.
411
412Note that the order in which the various test cases will be run is determined by
413sorting the test function names with the built-in :func:`cmp` function.
414
415Often it is desirable to group suites of test cases together, so as to run tests
416for the whole system at once. This is easy, since :class:`TestSuite` instances
417can be added to a :class:`TestSuite` just as :class:`TestCase` instances can be
418added to a :class:`TestSuite`::
419
420 suite1 = module1.TheTestSuite()
421 suite2 = module2.TheTestSuite()
422 alltests = unittest.TestSuite([suite1, suite2])
423
424You can place the definitions of test cases and test suites in the same modules
425as the code they are to test (such as :file:`widget.py`), but there are several
426advantages to placing the test code in a separate module, such as
427:file:`test_widget.py`:
428
429* The test module can be run standalone from the command line.
430
431* The test code can more easily be separated from shipped code.
432
433* There is less temptation to change test code to fit the code it tests without
434 a good reason.
435
436* Test code should be modified much less frequently than the code it tests.
437
438* Tested code can be refactored more easily.
439
440* Tests for modules written in C must be in separate modules anyway, so why not
441 be consistent?
442
443* If the testing strategy changes, there is no need to change the source code.
444
445
446.. _legacy-unit-tests:
447
448Re-using old test code
449----------------------
450
451Some users will find that they have existing test code that they would like to
452run from :mod:`unittest`, without converting every old test function to a
453:class:`TestCase` subclass.
454
455For this reason, :mod:`unittest` provides a :class:`FunctionTestCase` class.
456This subclass of :class:`TestCase` can be used to wrap an existing test
457function. Set-up and tear-down functions can also be provided.
458
459Given the following test function::
460
461 def testSomething():
462 something = makeSomething()
463 assert something.name is not None
464 # ...
465
466one can create an equivalent test case instance as follows::
467
468 testcase = unittest.FunctionTestCase(testSomething)
469
470If there are additional set-up and tear-down methods that should be called as
471part of the test case's operation, they can also be provided like so::
472
473 testcase = unittest.FunctionTestCase(testSomething,
474 setUp=makeSomethingDB,
475 tearDown=deleteSomethingDB)
476
477To make migrating existing test suites easier, :mod:`unittest` supports tests
478raising :exc:`AssertionError` to indicate test failure. However, it is
479recommended that you use the explicit :meth:`TestCase.fail\*` and
480:meth:`TestCase.assert\*` methods instead, as future versions of :mod:`unittest`
481may treat :exc:`AssertionError` differently.
482
483.. note::
484
Georg Brandl2fcd1732009-05-30 10:45:40 +0000485 Even though :class:`FunctionTestCase` can be used to quickly convert an
486 existing test base over to a :mod:`unittest`\ -based system, this approach is
487 not recommended. Taking the time to set up proper :class:`TestCase`
488 subclasses will make future test refactorings infinitely easier.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000489
Benjamin Peterson99721e02009-03-23 23:10:14 +0000490In some cases, the existing tests may have been written using the :mod:`doctest`
491module. If so, :mod:`doctest` provides a :class:`DocTestSuite` class that can
492automatically build :class:`unittest.TestSuite` instances from the existing
493:mod:`doctest`\ -based tests.
494
Georg Brandl8ec7f652007-08-15 14:28:01 +0000495
Benjamin Peterson692428e2009-03-23 21:50:21 +0000496.. _unittest-skipping:
497
498Skipping tests and expected failures
499------------------------------------
500
501Unittest supports skipping individual test methods and even whole classes of
502tests. In addition, it supports marking a test as a "expected failure," a test
503that is broken and will fail, but shouldn't be counted as a failure on a
504:class:`TestResult`.
505
506Skipping a test is simply a matter of using the :func:`skip` :term:`decorator`
507or one of its conditional variants.
508
509Basic skipping looks like this: ::
510
511 class MyTestCase(unittest.TestCase):
512
513 @unittest.skip("demonstrating skipping")
514 def test_nothing(self):
515 self.fail("shouldn't happen")
516
Georg Brandl2fcd1732009-05-30 10:45:40 +0000517 @unittest.skipIf(mylib.__version__ < (1, 3),
518 "not supported in this library version")
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000519 def test_format(self):
Benjamin Peterson097aafd2009-03-29 03:39:58 +0000520 # Tests that work for only a certain version of the library.
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000521 pass
522
523 @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
524 def test_windows_support(self):
525 # windows specific testing code
526 pass
527
Benjamin Peterson692428e2009-03-23 21:50:21 +0000528This is the output of running the example above in verbose mode: ::
529
Benjamin Peterson097aafd2009-03-29 03:39:58 +0000530 test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'
Benjamin Peterson692428e2009-03-23 21:50:21 +0000531 test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000532 test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'
Benjamin Peterson692428e2009-03-23 21:50:21 +0000533
534 ----------------------------------------------------------------------
Benjamin Petersonbe76d4c2009-03-29 03:16:57 +0000535 Ran 3 tests in 0.005s
536
537 OK (skipped=3)
Benjamin Peterson692428e2009-03-23 21:50:21 +0000538
539Classes can be skipped just like methods: ::
540
541 @skip("showing class skipping")
542 class MySkippedTestCase(unittest.TestCase):
543 def test_not_run(self):
544 pass
545
Benjamin Peterson31b78062009-03-23 23:13:36 +0000546:meth:`TestCase.setUp` can also skip the test. This is useful when a resource
547that needs to be set up is not available.
548
Benjamin Peterson692428e2009-03-23 21:50:21 +0000549Expected failures use the :func:`expectedFailure` decorator. ::
550
551 class ExpectedFailureTestCase(unittest.TestCase):
552 @unittest.expectedFailure
553 def test_fail(self):
554 self.assertEqual(1, 0, "broken")
555
556It's easy to roll your own skipping decorators by making a decorator that calls
557:func:`skip` on the test when it wants it to be skipped. This decorator skips
558the test unless the passed object has a certain attribute: ::
559
560 def skipUnlessHasattr(obj, attr):
561 if hasattr(obj, attr):
562 return lambda func: func
563 return unittest.skip("{0!r} doesn't have {1!r}".format(obj, attr))
564
565The following decorators implement test skipping and expected failures:
566
567.. function:: skip(reason)
568
569 Unconditionally skip the decorated test. *reason* should describe why the
570 test is being skipped.
571
572.. function:: skipIf(condition, reason)
573
574 Skip the decorated test if *condition* is true.
575
576.. function:: skipUnless(condition, reason)
577
578 Skip the decoratored test unless *condition* is true.
579
580.. function:: expectedFailure
581
582 Mark the test as an expected failure. If the test fails when run, the test
583 is not counted as a failure.
584
585
Georg Brandl8ec7f652007-08-15 14:28:01 +0000586.. _unittest-contents:
587
588Classes and functions
589---------------------
590
Benjamin Peterson99721e02009-03-23 23:10:14 +0000591This section describes in depth the API of :mod:`unittest`.
592
593
594.. _testcase-objects:
595
596Test cases
597~~~~~~~~~~
Georg Brandl8ec7f652007-08-15 14:28:01 +0000598
599.. class:: TestCase([methodName])
600
601 Instances of the :class:`TestCase` class represent the smallest testable units
602 in the :mod:`unittest` universe. This class is intended to be used as a base
603 class, with specific tests being implemented by concrete subclasses. This class
604 implements the interface needed by the test runner to allow it to drive the
605 test, and methods that the test code can use to check for and report various
606 kinds of failure.
607
608 Each instance of :class:`TestCase` will run a single test method: the method
609 named *methodName*. If you remember, we had an earlier example that went
610 something like this::
611
612 def suite():
613 suite = unittest.TestSuite()
614 suite.addTest(WidgetTestCase('testDefaultSize'))
615 suite.addTest(WidgetTestCase('testResize'))
616 return suite
617
618 Here, we create two instances of :class:`WidgetTestCase`, each of which runs a
619 single test.
620
Benjamin Peterson99721e02009-03-23 23:10:14 +0000621 *methodName* defaults to :meth:`runTest`.
622
623 :class:`TestCase` instances provide three groups of methods: one group used
624 to run the test, another used by the test implementation to check conditions
625 and report failures, and some inquiry methods allowing information about the
626 test itself to be gathered.
627
628 Methods in the first group (running the test) are:
629
630
631 .. method:: setUp()
632
633 Method called to prepare the test fixture. This is called immediately
634 before calling the test method; any exception raised by this method will
635 be considered an error rather than a test failure. The default
636 implementation does nothing.
637
638
639 .. method:: tearDown()
640
641 Method called immediately after the test method has been called and the
642 result recorded. This is called even if the test method raised an
643 exception, so the implementation in subclasses may need to be particularly
644 careful about checking internal state. Any exception raised by this
645 method will be considered an error rather than a test failure. This
646 method will only be called if the :meth:`setUp` succeeds, regardless of
647 the outcome of the test method. The default implementation does nothing.
648
649
650 .. method:: run([result])
651
652 Run the test, collecting the result into the test result object passed as
653 *result*. If *result* is omitted or :const:`None`, a temporary result
Ezio Melottic2f5a592009-06-30 22:51:06 +0000654 object is created (by calling the :meth:`defaultTestResult` method) and
655 used. The result object is not returned to :meth:`run`'s caller.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000656
657 The same effect may be had by simply calling the :class:`TestCase`
658 instance.
659
660
Benjamin Peterson47d97382009-03-26 20:05:50 +0000661 .. method:: skipTest(reason)
Benjamin Peterson99721e02009-03-23 23:10:14 +0000662
Benjamin Peterson31b78062009-03-23 23:13:36 +0000663 Calling this during the a test method or :meth:`setUp` skips the current
664 test. See :ref:`unittest-skipping` for more information.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000665
666
667 .. method:: debug()
668
669 Run the test without collecting the result. This allows exceptions raised
670 by the test to be propagated to the caller, and can be used to support
671 running tests under a debugger.
672
673 The test code can use any of the following methods to check for and report
674 failures.
675
676
Gregory P. Smith28399852009-03-31 16:54:10 +0000677 .. method:: assertTrue(expr[, msg])
678 assert_(expr[, msg])
Benjamin Peterson99721e02009-03-23 23:10:14 +0000679 failUnless(expr[, msg])
Benjamin Peterson99721e02009-03-23 23:10:14 +0000680
Georg Brandl64034bb2009-04-25 14:51:31 +0000681 Signal a test failure if *expr* is false; the explanation for the failure
Benjamin Peterson99721e02009-03-23 23:10:14 +0000682 will be *msg* if given, otherwise it will be :const:`None`.
683
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000684 .. deprecated:: 2.7
685 :meth:`failUnless`.
686
Benjamin Peterson99721e02009-03-23 23:10:14 +0000687
688 .. method:: assertEqual(first, second[, msg])
689 failUnlessEqual(first, second[, msg])
690
691 Test that *first* and *second* are equal. If the values do not compare
692 equal, the test will fail with the explanation given by *msg*, or
Gregory P. Smith28399852009-03-31 16:54:10 +0000693 :const:`None`. Note that using :meth:`assertEqual` improves upon
694 doing the comparison as the first parameter to :meth:`assertTrue`: the
695 default value for *msg* include representations of both *first* and
696 *second*.
697
698 In addition, if *first* and *second* are the exact same type and one of
699 list, tuple, dict, set, or frozenset or any type that a subclass
700 registers :meth:`addTypeEqualityFunc` the type specific equality function
701 will be called in order to generate a more useful default error message.
702
703 .. versionchanged:: 2.7
704 Added the automatic calling of type specific equality function.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000705
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000706 .. deprecated:: 2.7
707 :meth:`failUnlessEqual`.
708
Benjamin Peterson99721e02009-03-23 23:10:14 +0000709
710 .. method:: assertNotEqual(first, second[, msg])
711 failIfEqual(first, second[, msg])
712
713 Test that *first* and *second* are not equal. If the values do compare
714 equal, the test will fail with the explanation given by *msg*, or
Gregory P. Smith28399852009-03-31 16:54:10 +0000715 :const:`None`. Note that using :meth:`assertNotEqual` improves upon doing
716 the comparison as the first parameter to :meth:`assertTrue` is that the
Benjamin Peterson99721e02009-03-23 23:10:14 +0000717 default value for *msg* can be computed to include representations of both
718 *first* and *second*.
719
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000720 .. deprecated:: 2.7
721 :meth:`failIfEqual`.
722
Benjamin Peterson99721e02009-03-23 23:10:14 +0000723
724 .. method:: assertAlmostEqual(first, second[, places[, msg]])
725 failUnlessAlmostEqual(first, second[, places[, msg]])
726
727 Test that *first* and *second* are approximately equal by computing the
728 difference, rounding to the given number of decimal *places* (default 7),
729 and comparing to zero.
730
731 Note that comparing a given number of decimal places is not the same as
732 comparing a given number of significant digits. If the values do not
733 compare equal, the test will fail with the explanation given by *msg*, or
734 :const:`None`.
735
Michael Foordc3f79372009-09-13 16:40:02 +0000736 .. versionchanged:: 2.7
737 Objects that compare equal are automatically almost equal.
738
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000739 .. deprecated:: 2.7
740 :meth:`failUnlessAlmostEqual`.
741
Benjamin Peterson99721e02009-03-23 23:10:14 +0000742
743 .. method:: assertNotAlmostEqual(first, second[, places[, msg]])
744 failIfAlmostEqual(first, second[, places[, msg]])
745
746 Test that *first* and *second* are not approximately equal by computing
747 the difference, rounding to the given number of decimal *places* (default
748 7), and comparing to zero.
749
750 Note that comparing a given number of decimal places is not the same as
751 comparing a given number of significant digits. If the values do not
752 compare equal, the test will fail with the explanation given by *msg*, or
753 :const:`None`.
754
Michael Foordc3f79372009-09-13 16:40:02 +0000755 .. versionchanged:: 2.7
756 Objects that compare equal automatically fail.
757
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000758 .. deprecated:: 2.7
759 :meth:`failIfAlmostEqual`.
760
Benjamin Peterson99721e02009-03-23 23:10:14 +0000761
Gregory P. Smith28399852009-03-31 16:54:10 +0000762 .. method:: assertGreater(first, second, msg=None)
763 assertGreaterEqual(first, second, msg=None)
764 assertLess(first, second, msg=None)
765 assertLessEqual(first, second, msg=None)
766
767 Test that *first* is respectively >, >=, < or <= than *second* depending
Andrew M. Kuchling59631852009-04-09 11:23:36 +0000768 on the method name. If not, the test will fail with an explanation
Gregory P. Smith28399852009-03-31 16:54:10 +0000769 or with the explanation given by *msg*::
770
771 >>> self.assertGreaterEqual(3, 4)
772 AssertionError: "3" unexpectedly not greater than or equal to "4"
773
774 .. versionadded:: 2.7
775
776
777 .. method:: assertMultiLineEqual(self, first, second, msg=None)
778
779 Test that the multiline string *first* is equal to the string *second*.
780 When not equal a diff of the two strings highlighting the differences
781 will be included in the error message.
782
783 If specified *msg* will be used as the error message on failure.
784
785 .. versionadded:: 2.7
786
787
788 .. method:: assertRegexpMatches(text, regexp[, msg=None]):
789
790 Verifies that a *regexp* search matches *text*. Fails with an error
791 message including the pattern and the *text*. *regexp* may be
792 a regular expression object or a string containing a regular expression
793 suitable for use by :func:`re.search`.
794
795 .. versionadded:: 2.7
796
797
798 .. method:: assertIn(first, second, msg=None)
799 assertNotIn(first, second, msg=None)
800
Andrew M. Kuchling59631852009-04-09 11:23:36 +0000801 Tests that *first* is or is not in *second* with an explanatory error
Gregory P. Smith28399852009-03-31 16:54:10 +0000802 message as appropriate.
803
804 If specified *msg* will be used as the error message on failure.
805
806 .. versionadded:: 2.7
807
808
809 .. method:: assertSameElements(expected, actual, msg=None)
810
811 Test that sequence *expected* contains the same elements as *actual*.
812 When they don't an error message listing the differences between the
813 sequences will be generated.
814
815 If specified *msg* will be used as the error message on failure.
816
817 .. versionadded:: 2.7
818
819
820 .. method:: assertSetEqual(set1, set2, msg=None)
821
822 Tests that two sets are equal. If not, an error message is constructed
823 that lists the differences between the sets.
824
825 Fails if either of *set1* or *set2* does not have a :meth:`set.difference`
826 method.
827
828 If specified *msg* will be used as the error message on failure.
829
830 .. versionadded:: 2.7
831
832
833 .. method:: assertDictEqual(expected, actual, msg=None)
834
835 Test that two dictionaries are equal. If not, an error message is
836 constructed that shows the differences in the dictionaries.
837
838 If specified *msg* will be used as the error message on failure.
839
840 .. versionadded:: 2.7
841
842
843 .. method:: assertDictContainsSubset(expected, actual, msg=None)
844
Andrew M. Kuchling59631852009-04-09 11:23:36 +0000845 Tests whether the key/value pairs in dictionary *actual* are a
Gregory P. Smith28399852009-03-31 16:54:10 +0000846 superset of those in *expected*. If not, an error message listing
847 the missing keys and mismatched values is generated.
848
849 If specified *msg* will be used as the error message on failure.
850
851 .. versionadded:: 2.7
852
853
854 .. method:: assertListEqual(list1, list2, msg=None)
855 assertTupleEqual(tuple1, tuple2, msg=None)
856
857 Tests that two lists or tuples are equal. If not an error message is
858 constructed that shows only the differences between the two. An error
859 is also raised if either of the parameters are of the wrong type.
860
861 If specified *msg* will be used as the error message on failure.
862
863 .. versionadded:: 2.7
864
865
866 .. method:: assertSequenceEqual(seq1, seq2, msg=None, seq_type=None)
867
868 Tests that two sequences are equal. If a *seq_type* is supplied, both
869 *seq1* and *seq2* must be instances of *seq_type* or a failure will
870 be raised. If the sequences are different an error message is
871 constructed that shows the difference between the two.
872
873 If specified *msg* will be used as the error message on failure.
874
875 This method is used to implement :meth:`assertListEqual` and
876 :meth:`assertTupleEqual`.
877
878 .. versionadded:: 2.7
879
880
Benjamin Peterson99721e02009-03-23 23:10:14 +0000881 .. method:: assertRaises(exception[, callable, ...])
882 failUnlessRaises(exception[, callable, ...])
883
884 Test that an exception is raised when *callable* is called with any
885 positional or keyword arguments that are also passed to
886 :meth:`assertRaises`. The test passes if *exception* is raised, is an
887 error if another exception is raised, or fails if no exception is raised.
888 To catch any of a group of exceptions, a tuple containing the exception
889 classes may be passed as *exception*.
890
Benjamin Peterson7233acc2009-03-29 03:31:40 +0000891 If *callable* is omitted or None, returns a context manager so that the
892 code under test can be written inline rather than as a function::
893
894 with self.failUnlessRaises(some_error_class):
895 do_something()
896
Kristján Valur Jónssone2a77982009-08-27 22:20:21 +0000897 The context manager will store the caught exception object in its
898 :attr:`exc_value` attribute. This can be useful if the intention
899 is to perform additional checks on the exception raised.
900
Benjamin Peterson99721e02009-03-23 23:10:14 +0000901 .. versionchanged:: 2.7
Benjamin Peterson7233acc2009-03-29 03:31:40 +0000902 Added the ability to use :meth:`assertRaises` as a context manager.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000903
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000904 .. deprecated:: 2.7
905 :meth:`failUnlessRaises`.
906
Benjamin Peterson99721e02009-03-23 23:10:14 +0000907
Gregory P. Smith28399852009-03-31 16:54:10 +0000908 .. method:: assertRaisesRegexp(exception, regexp[, callable, ...])
Benjamin Peterson99721e02009-03-23 23:10:14 +0000909
Gregory P. Smith28399852009-03-31 16:54:10 +0000910 Like :meth:`assertRaises` but also tests that *regexp* matches
911 on the string representation of the raised exception. *regexp* may be
912 a regular expression object or a string containing a regular expression
913 suitable for use by :func:`re.search`. Examples::
914
915 self.assertRaisesRegexp(ValueError, 'invalid literal for.*XYZ$',
916 int, 'XYZ')
917
918 or::
919
920 with self.assertRaisesRegexp(ValueError, 'literal'):
921 int('XYZ')
922
923 .. versionadded:: 2.7
924
925
926 .. method:: assertIsNone(expr[, msg])
927
928 This signals a test failure if *expr* is not None.
929
930 .. versionadded:: 2.7
931
932
933 .. method:: assertIsNotNone(expr[, msg])
934
935 The inverse of the :meth:`assertIsNone` method.
936 This signals a test failure if *expr* is None.
937
938 .. versionadded:: 2.7
939
940
Michael Foordf2dfef12009-04-05 19:19:28 +0000941 .. method:: assertIs(expr1, expr2[, msg])
942
943 This signals a test failure if *expr1* and *expr2* don't evaluate to the same
944 object.
945
946 .. versionadded:: 2.7
947
948
949 .. method:: assertIsNot(expr1, expr2[, msg])
950
951 The inverse of the :meth:`assertIs` method.
952 This signals a test failure if *expr1* and *expr2* evaluate to the same
953 object.
954
955 .. versionadded:: 2.7
956
957
Georg Brandlf895cf52009-10-01 20:59:31 +0000958 .. method:: assertIsInstance(obj, cls[, msg])
959
960 This signals a test failure if *obj* is not an instance of *cls* (which
961 can be a class or a tuple of classes, as supported by :func:`isinstance`).
962
963 .. versionadded:: 2.7
964
965
966 .. method:: assertNotIsInstance(obj, cls[, msg])
967
968 The inverse of the :meth:`assertIsInstance` method. This signals a test
969 failure if *obj* is an instance of *cls*.
970
971 .. versionadded:: 2.7
972
973
Gregory P. Smith28399852009-03-31 16:54:10 +0000974 .. method:: assertFalse(expr[, msg])
975 failIf(expr[, msg])
976
977 The inverse of the :meth:`assertTrue` method is the :meth:`assertFalse` method.
Benjamin Peterson99721e02009-03-23 23:10:14 +0000978 This signals a test failure if *expr* is true, with *msg* or :const:`None`
979 for the error message.
980
Gregory P. Smith65ff0052009-03-31 19:59:14 +0000981 .. deprecated:: 2.7
982 :meth:`failIf`.
983
Benjamin Peterson99721e02009-03-23 23:10:14 +0000984
985 .. method:: fail([msg])
986
987 Signals a test failure unconditionally, with *msg* or :const:`None` for
988 the error message.
989
990
991 .. attribute:: failureException
992
993 This class attribute gives the exception raised by the test method. If a
994 test framework needs to use a specialized exception, possibly to carry
995 additional information, it must subclass this exception in order to "play
996 fair" with the framework. The initial value of this attribute is
997 :exc:`AssertionError`.
998
Michael Foord345b2fe2009-04-02 03:20:38 +0000999
1000 .. attribute:: longMessage
1001
1002 If set to True then any explicit failure message you pass in to the
1003 assert methods will be appended to the end of the normal failure message.
1004 The normal messages contain useful information about the objects involved,
1005 for example the message from assertEqual shows you the repr of the two
1006 unequal objects. Setting this attribute to True allows you to have a
1007 custom error message in addition to the normal one.
1008
1009 This attribute defaults to False, meaning that a custom message passed
1010 to an assert method will silence the normal message.
1011
1012 The class setting can be overridden in individual tests by assigning an
1013 instance attribute to True or False before calling the assert methods.
1014
1015 .. versionadded:: 2.7
1016
1017
Benjamin Peterson99721e02009-03-23 23:10:14 +00001018 Testing frameworks can use the following methods to collect information on
1019 the test:
1020
1021
1022 .. method:: countTestCases()
1023
1024 Return the number of tests represented by this test object. For
1025 :class:`TestCase` instances, this will always be ``1``.
1026
1027
1028 .. method:: defaultTestResult()
1029
1030 Return an instance of the test result class that should be used for this
1031 test case class (if no other result instance is provided to the
1032 :meth:`run` method).
1033
1034 For :class:`TestCase` instances, this will always be an instance of
1035 :class:`TestResult`; subclasses of :class:`TestCase` should override this
1036 as necessary.
1037
1038
1039 .. method:: id()
1040
1041 Return a string identifying the specific test case. This is usually the
1042 full name of the test method, including the module and class name.
1043
1044
1045 .. method:: shortDescription()
1046
Gregory P. Smith28399852009-03-31 16:54:10 +00001047 Returns a description of the test, or :const:`None` if no description
1048 has been provided. The default implementation of this method
1049 returns the first line of the test method's docstring, if available,
1050 along with the method name.
1051
1052 .. versionchanged:: 2.7
Gregory P. Smith28399852009-03-31 16:54:10 +00001053 In earlier versions this only returned the first line of the test
1054 method's docstring, if available or the :const:`None`. That led to
1055 undesirable behavior of not printing the test name when someone was
1056 thoughtful enough to write a docstring.
1057
1058
1059 .. method:: addTypeEqualityFunc(typeobj, function)
1060
1061 Registers a type specific :meth:`assertEqual` equality checking
1062 function to be called by :meth:`assertEqual` when both objects it has
1063 been asked to compare are exactly *typeobj* (not subclasses).
1064 *function* must take two positional arguments and a third msg=None
1065 keyword argument just as :meth:`assertEqual` does. It must raise
Andrew M. Kuchling59631852009-04-09 11:23:36 +00001066 ``self.failureException`` when inequality between the first two
Gregory P. Smith28399852009-03-31 16:54:10 +00001067 parameters is detected.
1068
1069 One good use of custom equality checking functions for a type
Andrew M. Kuchling59631852009-04-09 11:23:36 +00001070 is to raise ``self.failureException`` with an error message useful
1071 for debugging the problem by explaining the inequalities in detail.
Gregory P. Smith28399852009-03-31 16:54:10 +00001072
1073 .. versionadded:: 2.7
Georg Brandl8ec7f652007-08-15 14:28:01 +00001074
1075
Michael Foorde2fb98f2009-05-02 20:15:05 +00001076 .. method:: addCleanup(function[, *args[, **kwargs]])
1077
1078 Add a function to be called after :meth:`tearDown` to cleanup resources
1079 used during the test. Functions will be called in reverse order to the
1080 order they are added (LIFO). They are called with any arguments and
1081 keyword arguments passed into :meth:`addCleanup` when they are
1082 added.
1083
1084 If :meth:`setUp` fails, meaning that :meth:`tearDown` is not called,
1085 then any cleanup functions added will still be called.
1086
1087 .. versionadded:: 2.7
1088
1089
1090 .. method:: doCleanups()
1091
1092 This method is called uncoditionally after :meth:`tearDown`, or
1093 after :meth:`setUp` if :meth:`setUp` raises an exception.
1094
1095 It is responsible for calling all the cleanup functions added by
1096 :meth:`addCleanup`. If you need cleanup functions to be called
1097 *prior* to :meth:`tearDown` then you can call :meth:`doCleanups`
1098 yourself.
1099
1100 :meth:`doCleanups` pops methods off the stack of cleanup
1101 functions one at a time, so it can be called at any time.
1102
1103 .. versionadded:: 2.7
1104
1105
Georg Brandl8ec7f652007-08-15 14:28:01 +00001106.. class:: FunctionTestCase(testFunc[, setUp[, tearDown[, description]]])
1107
1108 This class implements the portion of the :class:`TestCase` interface which
Georg Brandl2fcd1732009-05-30 10:45:40 +00001109 allows the test runner to drive the test, but does not provide the methods
1110 which test code can use to check and report errors. This is used to create
1111 test cases using legacy test code, allowing it to be integrated into a
1112 :mod:`unittest`-based test framework.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001113
1114
Benjamin Peterson99721e02009-03-23 23:10:14 +00001115.. _testsuite-objects:
1116
1117Grouping tests
1118~~~~~~~~~~~~~~
1119
Georg Brandl8ec7f652007-08-15 14:28:01 +00001120.. class:: TestSuite([tests])
1121
1122 This class represents an aggregation of individual tests cases and test suites.
1123 The class presents the interface needed by the test runner to allow it to be run
1124 as any other test case. Running a :class:`TestSuite` instance is the same as
1125 iterating over the suite, running each test individually.
1126
1127 If *tests* is given, it must be an iterable of individual test cases or other
1128 test suites that will be used to build the suite initially. Additional methods
1129 are provided to add test cases and suites to the collection later on.
1130
Benjamin Peterson176a56c2009-05-25 00:48:58 +00001131 :class:`TestSuite` objects behave much like :class:`TestCase` objects, except
1132 they do not actually implement a test. Instead, they are used to aggregate
1133 tests into groups of tests that should be run together. Some additional
1134 methods are available to add tests to :class:`TestSuite` instances:
Benjamin Peterson99721e02009-03-23 23:10:14 +00001135
1136
1137 .. method:: TestSuite.addTest(test)
1138
1139 Add a :class:`TestCase` or :class:`TestSuite` to the suite.
1140
1141
1142 .. method:: TestSuite.addTests(tests)
1143
1144 Add all the tests from an iterable of :class:`TestCase` and :class:`TestSuite`
1145 instances to this test suite.
1146
Georg Brandl2fcd1732009-05-30 10:45:40 +00001147 This is equivalent to iterating over *tests*, calling :meth:`addTest` for
1148 each element.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001149
1150 :class:`TestSuite` shares the following methods with :class:`TestCase`:
1151
1152
1153 .. method:: run(result)
1154
1155 Run the tests associated with this suite, collecting the result into the
1156 test result object passed as *result*. Note that unlike
1157 :meth:`TestCase.run`, :meth:`TestSuite.run` requires the result object to
1158 be passed in.
1159
1160
1161 .. method:: debug()
1162
1163 Run the tests associated with this suite without collecting the
1164 result. This allows exceptions raised by the test to be propagated to the
1165 caller and can be used to support running tests under a debugger.
1166
1167
1168 .. method:: countTestCases()
1169
1170 Return the number of tests represented by this test object, including all
1171 individual tests and sub-suites.
1172
Georg Brandl9bc66822009-04-27 17:04:23 +00001173
1174 .. method:: __iter__()
1175
1176 Tests grouped by a :class:`TestSuite` are always accessed by iteration.
1177 Subclasses can lazily provide tests by overriding :meth:`__iter__`. Note
1178 that this method maybe called several times on a single suite
1179 (for example when counting tests or comparing for equality)
1180 so the tests returned must be the same for repeated iterations.
1181
1182 .. versionchanged:: 2.7
1183 In earlier versions the :class:`TestSuite` accessed tests directly rather
1184 than through iteration, so overriding :meth:`__iter__` wasn't sufficient
1185 for providing tests.
1186
Benjamin Peterson99721e02009-03-23 23:10:14 +00001187 In the typical usage of a :class:`TestSuite` object, the :meth:`run` method
1188 is invoked by a :class:`TestRunner` rather than by the end-user test harness.
1189
1190
Benjamin Peterson99721e02009-03-23 23:10:14 +00001191Loading and running tests
1192~~~~~~~~~~~~~~~~~~~~~~~~~
1193
Georg Brandl8ec7f652007-08-15 14:28:01 +00001194.. class:: TestLoader()
1195
Benjamin Peterson99721e02009-03-23 23:10:14 +00001196 The :class:`TestLoader` class is used to create test suites from classes and
1197 modules. Normally, there is no need to create an instance of this class; the
1198 :mod:`unittest` module provides an instance that can be shared as
1199 ``unittest.defaultTestLoader``. Using a subclass or instance, however, allows
1200 customization of some configurable properties.
1201
1202 :class:`TestLoader` objects have the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +00001203
1204
Benjamin Peterson99721e02009-03-23 23:10:14 +00001205 .. method:: loadTestsFromTestCase(testCaseClass)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001206
Benjamin Peterson99721e02009-03-23 23:10:14 +00001207 Return a suite of all tests cases contained in the :class:`TestCase`\ -derived
1208 :class:`testCaseClass`.
1209
1210
1211 .. method:: loadTestsFromModule(module)
1212
1213 Return a suite of all tests cases contained in the given module. This
1214 method searches *module* for classes derived from :class:`TestCase` and
1215 creates an instance of the class for each test method defined for the
1216 class.
1217
Georg Brandl16a57f62009-04-27 15:29:09 +00001218 .. note::
Benjamin Peterson99721e02009-03-23 23:10:14 +00001219
1220 While using a hierarchy of :class:`TestCase`\ -derived classes can be
1221 convenient in sharing fixtures and helper functions, defining test
1222 methods on base classes that are not intended to be instantiated
1223 directly does not play well with this method. Doing so, however, can
1224 be useful when the fixtures are different and defined in subclasses.
1225
Michael Foordb4a81c82009-05-29 20:33:46 +00001226 If a module provides a ``load_tests`` function it will be called to
1227 load the tests. This allows modules to customize test loading.
1228 This is the `load_tests protocol`_.
1229
1230 .. versionchanged:: 2.7
1231 Support for ``load_tests`` added.
1232
Benjamin Peterson99721e02009-03-23 23:10:14 +00001233
1234 .. method:: loadTestsFromName(name[, module])
1235
1236 Return a suite of all tests cases given a string specifier.
1237
1238 The specifier *name* is a "dotted name" that may resolve either to a
1239 module, a test case class, a test method within a test case class, a
1240 :class:`TestSuite` instance, or a callable object which returns a
1241 :class:`TestCase` or :class:`TestSuite` instance. These checks are
1242 applied in the order listed here; that is, a method on a possible test
1243 case class will be picked up as "a test method within a test case class",
1244 rather than "a callable object".
1245
1246 For example, if you have a module :mod:`SampleTests` containing a
Georg Brandl2fcd1732009-05-30 10:45:40 +00001247 :class:`TestCase`\ -derived class :class:`SampleTestCase` with three test
1248 methods (:meth:`test_one`, :meth:`test_two`, and :meth:`test_three`), the
1249 specifier ``'SampleTests.SampleTestCase'`` would cause this method to
1250 return a suite which will run all three test methods. Using the specifier
1251 ``'SampleTests.SampleTestCase.test_two'`` would cause it to return a test
1252 suite which will run only the :meth:`test_two` test method. The specifier
1253 can refer to modules and packages which have not been imported; they will
1254 be imported as a side-effect.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001255
1256 The method optionally resolves *name* relative to the given *module*.
1257
1258
1259 .. method:: loadTestsFromNames(names[, module])
1260
1261 Similar to :meth:`loadTestsFromName`, but takes a sequence of names rather
1262 than a single name. The return value is a test suite which supports all
1263 the tests defined for each name.
1264
1265
1266 .. method:: getTestCaseNames(testCaseClass)
1267
1268 Return a sorted sequence of method names found within *testCaseClass*;
1269 this should be a subclass of :class:`TestCase`.
1270
Michael Foordb4a81c82009-05-29 20:33:46 +00001271
1272 .. method:: discover(start_dir, pattern='test*.py', top_level_dir=None)
1273
1274 Find and return all test modules from the specified start directory,
1275 recursing into subdirectories to find them. Only test files that match
Michael Foorde91ea562009-09-13 19:07:03 +00001276 *pattern* will be loaded. (Using shell style pattern matching.) Only
1277 module names that are importable (i.e. are valid Python identifiers) will
1278 be loaded.
Michael Foordb4a81c82009-05-29 20:33:46 +00001279
1280 All test modules must be importable from the top level of the project. If
1281 the start directory is not the top level directory then the top level
1282 directory must be specified separately.
1283
Michael Foorde91ea562009-09-13 19:07:03 +00001284 If importing a module fails, for example due to a syntax error, then this
1285 will be recorded as a single error and discovery will continue.
1286
Michael Foordb4a81c82009-05-29 20:33:46 +00001287 If a test package name (directory with :file:`__init__.py`) matches the
1288 pattern then the package will be checked for a ``load_tests``
1289 function. If this exists then it will be called with *loader*, *tests*,
1290 *pattern*.
1291
Michael Foorddc0460a2009-09-13 19:08:18 +00001292 If load_tests exists then discovery does *not* recurse into the package,
Michael Foordb4a81c82009-05-29 20:33:46 +00001293 ``load_tests`` is responsible for loading all tests in the package.
1294
1295 The pattern is deliberately not stored as a loader attribute so that
1296 packages can continue discovery themselves. *top_level_dir* is stored so
1297 ``load_tests`` does not need to pass this argument in to
1298 ``loader.discover()``.
1299
Michael Foord17565e52009-09-27 20:08:23 +00001300 .. versionadded:: 2.7
Michael Foordb4a81c82009-05-29 20:33:46 +00001301
Benjamin Peterson99721e02009-03-23 23:10:14 +00001302 The following attributes of a :class:`TestLoader` can be configured either by
1303 subclassing or assignment on an instance:
1304
1305
1306 .. attribute:: testMethodPrefix
1307
1308 String giving the prefix of method names which will be interpreted as test
1309 methods. The default value is ``'test'``.
1310
1311 This affects :meth:`getTestCaseNames` and all the :meth:`loadTestsFrom\*`
1312 methods.
1313
1314
1315 .. attribute:: sortTestMethodsUsing
1316
1317 Function to be used to compare method names when sorting them in
1318 :meth:`getTestCaseNames` and all the :meth:`loadTestsFrom\*` methods. The
1319 default value is the built-in :func:`cmp` function; the attribute can also
1320 be set to :const:`None` to disable the sort.
1321
1322
1323 .. attribute:: suiteClass
1324
1325 Callable object that constructs a test suite from a list of tests. No
1326 methods on the resulting object are needed. The default value is the
1327 :class:`TestSuite` class.
1328
1329 This affects all the :meth:`loadTestsFrom\*` methods.
1330
1331
Benjamin Peterson99721e02009-03-23 23:10:14 +00001332.. class:: TestResult
1333
1334 This class is used to compile information about which tests have succeeded
1335 and which have failed.
1336
1337 A :class:`TestResult` object stores the results of a set of tests. The
1338 :class:`TestCase` and :class:`TestSuite` classes ensure that results are
1339 properly recorded; test authors do not need to worry about recording the
1340 outcome of tests.
1341
1342 Testing frameworks built on top of :mod:`unittest` may want access to the
1343 :class:`TestResult` object generated by running a set of tests for reporting
1344 purposes; a :class:`TestResult` instance is returned by the
1345 :meth:`TestRunner.run` method for this purpose.
1346
1347 :class:`TestResult` instances have the following attributes that will be of
1348 interest when inspecting the results of running a set of tests:
1349
1350
1351 .. attribute:: errors
1352
1353 A list containing 2-tuples of :class:`TestCase` instances and strings
1354 holding formatted tracebacks. Each tuple represents a test which raised an
1355 unexpected exception.
1356
1357 .. versionchanged:: 2.2
Benjamin Peterson99721e02009-03-23 23:10:14 +00001358 Contains formatted tracebacks instead of :func:`sys.exc_info` results.
1359
1360
1361 .. attribute:: failures
1362
1363 A list containing 2-tuples of :class:`TestCase` instances and strings
1364 holding formatted tracebacks. Each tuple represents a test where a failure
1365 was explicitly signalled using the :meth:`TestCase.fail\*` or
1366 :meth:`TestCase.assert\*` methods.
1367
1368 .. versionchanged:: 2.2
Benjamin Peterson99721e02009-03-23 23:10:14 +00001369 Contains formatted tracebacks instead of :func:`sys.exc_info` results.
1370
1371 .. attribute:: skipped
1372
1373 A list containing 2-tuples of :class:`TestCase` instances and strings
1374 holding the reason for skipping the test.
1375
1376 .. versionadded:: 2.7
1377
1378 .. attribute:: expectedFailures
1379
1380 A list contaning 2-tuples of :class:`TestCase` instances and strings
1381 holding formatted tracebacks. Each tuple represents a expected failures
1382 of the test case.
1383
1384 .. attribute:: unexpectedSuccesses
1385
1386 A list containing :class:`TestCase` instances that were marked as expected
1387 failures, but succeeded.
1388
1389 .. attribute:: shouldStop
1390
1391 Set to ``True`` when the execution of tests should stop by :meth:`stop`.
1392
1393
1394 .. attribute:: testsRun
1395
1396 The total number of tests run so far.
1397
1398
1399 .. method:: wasSuccessful()
1400
1401 Return :const:`True` if all tests run so far have passed, otherwise returns
1402 :const:`False`.
1403
1404
1405 .. method:: stop()
1406
1407 This method can be called to signal that the set of tests being run should
1408 be aborted by setting the :attr:`shouldStop` attribute to :const:`True`.
1409 :class:`TestRunner` objects should respect this flag and return without
1410 running any additional tests.
1411
1412 For example, this feature is used by the :class:`TextTestRunner` class to
1413 stop the test framework when the user signals an interrupt from the
1414 keyboard. Interactive tools which provide :class:`TestRunner`
1415 implementations can use this in a similar manner.
1416
1417 The following methods of the :class:`TestResult` class are used to maintain
1418 the internal data structures, and may be extended in subclasses to support
1419 additional reporting requirements. This is particularly useful in building
1420 tools which support interactive reporting while tests are being run.
1421
1422
1423 .. method:: startTest(test)
1424
1425 Called when the test case *test* is about to be run.
1426
1427 The default implementation simply increments the instance's :attr:`testsRun`
1428 counter.
1429
1430
1431 .. method:: stopTest(test)
1432
1433 Called after the test case *test* has been executed, regardless of the
1434 outcome.
1435
1436 The default implementation does nothing.
1437
1438
Michael Foord07ef4872009-05-02 22:43:34 +00001439 .. method:: startTestRun(test)
1440
1441 Called once before any tests are executed.
1442
1443 .. versionadded:: 2.7
1444
1445
1446 .. method:: stopTestRun(test)
1447
1448 Called once before any tests are executed.
1449
1450 .. versionadded:: 2.7
1451
1452
Benjamin Peterson99721e02009-03-23 23:10:14 +00001453 .. method:: addError(test, err)
1454
1455 Called when the test case *test* raises an unexpected exception *err* is a
1456 tuple of the form returned by :func:`sys.exc_info`: ``(type, value,
1457 traceback)``.
1458
1459 The default implementation appends a tuple ``(test, formatted_err)`` to
1460 the instance's :attr:`errors` attribute, where *formatted_err* is a
1461 formatted traceback derived from *err*.
1462
1463
1464 .. method:: addFailure(test, err)
1465
Michael Foordb4a81c82009-05-29 20:33:46 +00001466 Called when the test case *test* signals a failure. *err* is a tuple of
1467 the form returned by :func:`sys.exc_info`: ``(type, value, traceback)``.
Benjamin Peterson99721e02009-03-23 23:10:14 +00001468
1469 The default implementation appends a tuple ``(test, formatted_err)`` to
1470 the instance's :attr:`failures` attribute, where *formatted_err* is a
1471 formatted traceback derived from *err*.
1472
1473
1474 .. method:: addSuccess(test)
1475
1476 Called when the test case *test* succeeds.
1477
1478 The default implementation does nothing.
1479
1480
1481 .. method:: addSkip(test, reason)
1482
1483 Called when the test case *test* is skipped. *reason* is the reason the
1484 test gave for skipping.
1485
1486 The default implementation appends a tuple ``(test, reason)`` to the
1487 instance's :attr:`skipped` attribute.
1488
1489
1490 .. method:: addExpectedFailure(test, err)
1491
1492 Called when the test case *test* fails, but was marked with the
1493 :func:`expectedFailure` decorator.
1494
1495 The default implementation appends a tuple ``(test, formatted_err)`` to
1496 the instance's :attr:`expectedFailures` attribute, where *formatted_err*
1497 is a formatted traceback derived from *err*.
1498
1499
1500 .. method:: addUnexpectedSuccess(test)
1501
1502 Called when the test case *test* was marked with the
1503 :func:`expectedFailure` decorator, but succeeded.
1504
1505 The default implementation appends the test to the instance's
1506 :attr:`unexpectedSuccesses` attribute.
Georg Brandl8ec7f652007-08-15 14:28:01 +00001507
1508
1509.. data:: defaultTestLoader
1510
1511 Instance of the :class:`TestLoader` class intended to be shared. If no
1512 customization of the :class:`TestLoader` is needed, this instance can be used
1513 instead of repeatedly creating new instances.
1514
1515
1516.. class:: TextTestRunner([stream[, descriptions[, verbosity]]])
1517
1518 A basic test runner implementation which prints results on standard error. It
1519 has a few configurable parameters, but is essentially very simple. Graphical
1520 applications which run test suites should provide alternate implementations.
1521
Georg Brandl9bc66822009-04-27 17:04:23 +00001522 .. method:: _makeResult()
1523
1524 This method returns the instance of ``TestResult`` used by :meth:`run`.
1525 It is not intended to be called directly, but can be overridden in
1526 subclasses to provide a custom ``TestResult``.
1527
Georg Brandl8ec7f652007-08-15 14:28:01 +00001528
Michael Foord5d31e052009-05-11 17:59:43 +00001529.. function:: main([module[, defaultTest[, argv[, testRunner[, testLoader[, exit, [verbosity]]]]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +00001530
1531 A command-line program that runs a set of tests; this is primarily for making
1532 test modules conveniently executable. The simplest use for this function is to
1533 include the following line at the end of a test script::
1534
1535 if __name__ == '__main__':
1536 unittest.main()
1537
Michael Foord5d31e052009-05-11 17:59:43 +00001538 You can run tests with more detailed information by passing in the verbosity
1539 argument::
1540
1541 if __name__ == '__main__':
1542 unittest.main(verbosity=2)
1543
Georg Brandl8ec7f652007-08-15 14:28:01 +00001544 The *testRunner* argument can either be a test runner class or an already
Michael Foord829f6b82009-05-02 11:43:06 +00001545 created instance of it. By default ``main`` calls :func:`sys.exit` with
1546 an exit code indicating success or failure of the tests run.
1547
1548 ``main`` supports being used from the interactive interpreter by passing in the
1549 argument ``exit=False``. This displays the result on standard output without
1550 calling :func:`sys.exit`::
1551
1552 >>> from unittest import main
1553 >>> main(module='test_module', exit=False)
1554
1555 Calling ``main`` actually returns an instance of the ``TestProgram`` class.
1556 This stores the result of the tests run as the ``result`` attribute.
1557
1558 .. versionchanged:: 2.7
Michael Foord5d31e052009-05-11 17:59:43 +00001559 The ``exit`` and ``verbosity`` parameters were added.
Michael Foordb4a81c82009-05-29 20:33:46 +00001560
1561
1562load_tests Protocol
1563###################
1564
Michael Foord17565e52009-09-27 20:08:23 +00001565
1566.. versionadded:: 2.7
1567
1568
Michael Foordb4a81c82009-05-29 20:33:46 +00001569Modules or packages can customize how tests are loaded from them during normal
1570test runs or test discovery by implementing a function called ``load_tests``.
1571
1572If a test module defines ``load_tests`` it will be called by
1573:meth:`TestLoader.loadTestsFromModule` with the following arguments::
1574
1575 load_tests(loader, standard_tests, None)
1576
1577It should return a :class:`TestSuite`.
1578
1579*loader* is the instance of :class:`TestLoader` doing the loading.
1580*standard_tests* are the tests that would be loaded by default from the
1581module. It is common for test modules to only want to add or remove tests
1582from the standard set of tests.
1583The third argument is used when loading packages as part of test discovery.
1584
1585A typical ``load_tests`` function that loads tests from a specific set of
1586:class:`TestCase` classes may look like::
1587
1588 test_cases = (TestCase1, TestCase2, TestCase3)
1589
1590 def load_tests(loader, tests, pattern):
1591 suite = TestSuite()
1592 for test_class in test_cases:
1593 tests = loader.loadTestsFromTestCase(test_class)
1594 suite.addTests(tests)
1595 return suite
1596
1597If discovery is started, either from the command line or by calling
1598:meth:`TestLoader.discover`, with a pattern that matches a package
1599name then the package :file:`__init__.py` will be checked for ``load_tests``.
1600
1601.. note::
1602
Ezio Melotti062d2b52009-12-19 22:41:49 +00001603 The default pattern is 'test*.py'. This matches all Python files
Michael Foordb4a81c82009-05-29 20:33:46 +00001604 that start with 'test' but *won't* match any test directories.
1605
1606 A pattern like 'test*' will match test packages as well as
1607 modules.
1608
1609If the package :file:`__init__.py` defines ``load_tests`` then it will be
1610called and discovery not continued into the package. ``load_tests``
1611is called with the following arguments::
1612
1613 load_tests(loader, standard_tests, pattern)
1614
1615This should return a :class:`TestSuite` representing all the tests
1616from the package. (``standard_tests`` will only contain tests
1617collected from :file:`__init__.py`.)
1618
1619Because the pattern is passed into ``load_tests`` the package is free to
1620continue (and potentially modify) test discovery. A 'do nothing'
1621``load_tests`` function for a test package would look like::
1622
1623 def load_tests(loader, standard_tests, pattern):
1624 # top level directory cached on loader instance
1625 this_dir = os.path.dirname(__file__)
1626 package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
1627 standard_tests.addTests(package_tests)
1628 return standard_tests