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