| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 |  | 
 | 2 | :mod:`test` --- Regression tests package for Python | 
 | 3 | =================================================== | 
 | 4 |  | 
 | 5 | .. module:: test | 
 | 6 |    :synopsis: Regression tests package containing the testing suite for Python. | 
 | 7 | .. sectionauthor:: Brett Cannon <brett@python.org> | 
 | 8 |  | 
 | 9 |  | 
 | 10 | The :mod:`test` package contains all regression tests for Python as well as the | 
 | 11 | modules :mod:`test.test_support` and :mod:`test.regrtest`. | 
 | 12 | :mod:`test.test_support` is used to enhance your tests while | 
 | 13 | :mod:`test.regrtest` drives the testing suite. | 
 | 14 |  | 
 | 15 | Each module in the :mod:`test` package whose name starts with ``test_`` is a | 
 | 16 | testing suite for a specific module or feature. All new tests should be written | 
 | 17 | using the :mod:`unittest` or :mod:`doctest` module.  Some older tests are | 
 | 18 | written using a "traditional" testing style that compares output printed to | 
 | 19 | ``sys.stdout``; this style of test is considered deprecated. | 
 | 20 |  | 
 | 21 |  | 
 | 22 | .. seealso:: | 
 | 23 |  | 
 | 24 |    Module :mod:`unittest` | 
 | 25 |       Writing PyUnit regression tests. | 
 | 26 |  | 
 | 27 |    Module :mod:`doctest` | 
 | 28 |       Tests embedded in documentation strings. | 
 | 29 |  | 
 | 30 |  | 
 | 31 | .. _writing-tests: | 
 | 32 |  | 
 | 33 | Writing Unit Tests for the :mod:`test` package | 
 | 34 | ---------------------------------------------- | 
 | 35 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | It is preferred that tests that use the :mod:`unittest` module follow a few | 
 | 37 | guidelines. One is to name the test module by starting it with ``test_`` and end | 
 | 38 | it with the name of the module being tested. The test methods in the test module | 
 | 39 | should start with ``test_`` and end with a description of what the method is | 
 | 40 | testing. This is needed so that the methods are recognized by the test driver as | 
 | 41 | test methods. Also, no documentation string for the method should be included. A | 
 | 42 | comment (such as ``# Tests function returns only True or False``) should be used | 
 | 43 | to provide documentation for test methods. This is done because documentation | 
 | 44 | strings get printed out if they exist and thus what test is being run is not | 
 | 45 | stated. | 
 | 46 |  | 
 | 47 | A basic boilerplate is often used:: | 
 | 48 |  | 
 | 49 |    import unittest | 
 | 50 |    from test import test_support | 
 | 51 |  | 
 | 52 |    class MyTestCase1(unittest.TestCase): | 
 | 53 |  | 
 | 54 |        # Only use setUp() and tearDown() if necessary | 
 | 55 |  | 
 | 56 |        def setUp(self): | 
 | 57 |            ... code to execute in preparation for tests ... | 
 | 58 |  | 
 | 59 |        def tearDown(self): | 
 | 60 |            ... code to execute to clean up after tests ... | 
 | 61 |  | 
 | 62 |        def test_feature_one(self): | 
 | 63 |            # Test feature one. | 
 | 64 |            ... testing code ... | 
 | 65 |  | 
 | 66 |        def test_feature_two(self): | 
 | 67 |            # Test feature two. | 
 | 68 |            ... testing code ... | 
 | 69 |  | 
 | 70 |        ... more test methods ... | 
 | 71 |  | 
 | 72 |    class MyTestCase2(unittest.TestCase): | 
 | 73 |        ... same structure as MyTestCase1 ... | 
 | 74 |  | 
 | 75 |    ... more test classes ... | 
 | 76 |  | 
 | 77 |    def test_main(): | 
 | 78 |        test_support.run_unittest(MyTestCase1, | 
 | 79 |                                  MyTestCase2, | 
 | 80 |                                  ... list other tests ... | 
 | 81 |                                 ) | 
 | 82 |  | 
 | 83 |    if __name__ == '__main__': | 
 | 84 |        test_main() | 
 | 85 |  | 
 | 86 | This boilerplate code allows the testing suite to be run by :mod:`test.regrtest` | 
 | 87 | as well as on its own as a script. | 
 | 88 |  | 
 | 89 | The goal for regression testing is to try to break code. This leads to a few | 
 | 90 | guidelines to be followed: | 
 | 91 |  | 
 | 92 | * The testing suite should exercise all classes, functions, and constants. This | 
 | 93 |   includes not just the external API that is to be presented to the outside world | 
 | 94 |   but also "private" code. | 
 | 95 |  | 
 | 96 | * Whitebox testing (examining the code being tested when the tests are being | 
 | 97 |   written) is preferred. Blackbox testing (testing only the published user | 
 | 98 |   interface) is not complete enough to make sure all boundary and edge cases are | 
 | 99 |   tested. | 
 | 100 |  | 
 | 101 | * Make sure all possible values are tested including invalid ones. This makes | 
 | 102 |   sure that not only all valid values are acceptable but also that improper values | 
 | 103 |   are handled correctly. | 
 | 104 |  | 
 | 105 | * Exhaust as many code paths as possible. Test where branching occurs and thus | 
 | 106 |   tailor input to make sure as many different paths through the code are taken. | 
 | 107 |  | 
 | 108 | * Add an explicit test for any bugs discovered for the tested code. This will | 
 | 109 |   make sure that the error does not crop up again if the code is changed in the | 
 | 110 |   future. | 
 | 111 |  | 
 | 112 | * Make sure to clean up after your tests (such as close and remove all temporary | 
 | 113 |   files). | 
 | 114 |  | 
 | 115 | * If a test is dependent on a specific condition of the operating system then | 
 | 116 |   verify the condition already exists before attempting the test. | 
 | 117 |  | 
 | 118 | * Import as few modules as possible and do it as soon as possible. This | 
 | 119 |   minimizes external dependencies of tests and also minimizes possible anomalous | 
 | 120 |   behavior from side-effects of importing a module. | 
 | 121 |  | 
 | 122 | * Try to maximize code reuse. On occasion, tests will vary by something as small | 
 | 123 |   as what type of input is used. Minimize code duplication by subclassing a basic | 
 | 124 |   test class with a class that specifies the input:: | 
 | 125 |  | 
 | 126 |      class TestFuncAcceptsSequences(unittest.TestCase): | 
 | 127 |  | 
 | 128 |          func = mySuperWhammyFunction | 
 | 129 |  | 
 | 130 |          def test_func(self): | 
 | 131 |              self.func(self.arg) | 
 | 132 |  | 
 | 133 |      class AcceptLists(TestFuncAcceptsSequences): | 
 | 134 |          arg = [1,2,3] | 
 | 135 |  | 
 | 136 |      class AcceptStrings(TestFuncAcceptsSequences): | 
 | 137 |          arg = 'abc' | 
 | 138 |  | 
 | 139 |      class AcceptTuples(TestFuncAcceptsSequences): | 
 | 140 |          arg = (1,2,3) | 
 | 141 |  | 
 | 142 |  | 
 | 143 | .. seealso:: | 
 | 144 |  | 
 | 145 |    Test Driven Development | 
 | 146 |       A book by Kent Beck on writing tests before code. | 
 | 147 |  | 
 | 148 |  | 
 | 149 | .. _regrtest: | 
 | 150 |  | 
 | 151 | Running tests using :mod:`test.regrtest` | 
 | 152 | ---------------------------------------- | 
 | 153 |  | 
 | 154 | :mod:`test.regrtest` can be used as a script to drive Python's regression test | 
 | 155 | suite. Running the script by itself automatically starts running all regression | 
 | 156 | tests in the :mod:`test` package. It does this by finding all modules in the | 
 | 157 | package whose name starts with ``test_``, importing them, and executing the | 
 | 158 | function :func:`test_main` if present. The names of tests to execute may also be | 
 | 159 | passed to the script. Specifying a single regression test (:program:`python | 
 | 160 | regrtest.py` :option:`test_spam.py`) will minimize output and only print whether | 
 | 161 | the test passed or failed and thus minimize output. | 
 | 162 |  | 
 | 163 | Running :mod:`test.regrtest` directly allows what resources are available for | 
 | 164 | tests to use to be set. You do this by using the :option:`-u` command-line | 
 | 165 | option. Run :program:`python regrtest.py` :option:`-uall` to turn on all | 
 | 166 | resources; specifying :option:`all` as an option for :option:`-u` enables all | 
 | 167 | possible resources. If all but one resource is desired (a more common case), a | 
 | 168 | comma-separated list of resources that are not desired may be listed after | 
 | 169 | :option:`all`. The command :program:`python regrtest.py` | 
 | 170 | :option:`-uall,-audio,-largefile` will run :mod:`test.regrtest` with all | 
 | 171 | resources except the :option:`audio` and :option:`largefile` resources. For a | 
 | 172 | list of all resources and more command-line options, run :program:`python | 
 | 173 | regrtest.py` :option:`-h`. | 
 | 174 |  | 
 | 175 | Some other ways to execute the regression tests depend on what platform the | 
 | 176 | tests are being executed on. On Unix, you can run :program:`make` :option:`test` | 
 | 177 | at the top-level directory where Python was built. On Windows, executing | 
 | 178 | :program:`rt.bat` from your :file:`PCBuild` directory will run all regression | 
 | 179 | tests. | 
 | 180 |  | 
 | 181 |  | 
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 182 | :mod:`test.support` --- Utility functions for tests | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | ======================================================== | 
 | 184 |  | 
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 185 | .. module:: test.support | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 186 |    :synopsis: Support for Python regression tests. | 
 | 187 |  | 
 | 188 |  | 
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 189 | The :mod:`test.support` module provides support for Python's regression | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | tests. | 
 | 191 |  | 
 | 192 | This module defines the following exceptions: | 
 | 193 |  | 
 | 194 |  | 
 | 195 | .. exception:: TestFailed | 
 | 196 |  | 
 | 197 |    Exception to be raised when a test fails. This is deprecated in favor of | 
 | 198 |    :mod:`unittest`\ -based tests and :class:`unittest.TestCase`'s assertion | 
 | 199 |    methods. | 
 | 200 |  | 
 | 201 |  | 
 | 202 | .. exception:: TestSkipped | 
 | 203 |  | 
 | 204 |    Subclass of :exc:`TestFailed`. Raised when a test is skipped. This occurs when a | 
 | 205 |    needed resource (such as a network connection) is not available at the time of | 
 | 206 |    testing. | 
 | 207 |  | 
 | 208 |  | 
 | 209 | .. exception:: ResourceDenied | 
 | 210 |  | 
 | 211 |    Subclass of :exc:`TestSkipped`. Raised when a resource (such as a network | 
 | 212 |    connection) is not available. Raised by the :func:`requires` function. | 
 | 213 |  | 
 | 214 | The :mod:`test.test_support` module defines the following constants: | 
 | 215 |  | 
 | 216 |  | 
 | 217 | .. data:: verbose | 
 | 218 |  | 
 | 219 |    :const:`True` when verbose output is enabled. Should be checked when more | 
 | 220 |    detailed information is desired about a running test. *verbose* is set by | 
 | 221 |    :mod:`test.regrtest`. | 
 | 222 |  | 
 | 223 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | .. data:: is_jython | 
 | 225 |  | 
 | 226 |    :const:`True` if the running interpreter is Jython. | 
 | 227 |  | 
 | 228 |  | 
 | 229 | .. data:: TESTFN | 
 | 230 |  | 
 | 231 |    Set to the path that a temporary file may be created at. Any temporary that is | 
 | 232 |    created should be closed and unlinked (removed). | 
 | 233 |  | 
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 234 | The :mod:`test.support` module defines the following functions: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 235 |  | 
 | 236 |  | 
 | 237 | .. function:: forget(module_name) | 
 | 238 |  | 
 | 239 |    Removes the module named *module_name* from ``sys.modules`` and deletes any | 
 | 240 |    byte-compiled files of the module. | 
 | 241 |  | 
 | 242 |  | 
 | 243 | .. function:: is_resource_enabled(resource) | 
 | 244 |  | 
 | 245 |    Returns :const:`True` if *resource* is enabled and available. The list of | 
 | 246 |    available resources is only set when :mod:`test.regrtest` is executing the | 
 | 247 |    tests. | 
 | 248 |  | 
 | 249 |  | 
 | 250 | .. function:: requires(resource[, msg]) | 
 | 251 |  | 
 | 252 |    Raises :exc:`ResourceDenied` if *resource* is not available. *msg* is the | 
 | 253 |    argument to :exc:`ResourceDenied` if it is raised. Always returns true if called | 
 | 254 |    by a function whose ``__name__`` is ``'__main__'``. Used when tests are executed | 
 | 255 |    by :mod:`test.regrtest`. | 
 | 256 |  | 
 | 257 |  | 
 | 258 | .. function:: findfile(filename) | 
 | 259 |  | 
 | 260 |    Return the path to the file named *filename*. If no match is found *filename* is | 
 | 261 |    returned. This does not equal a failure since it could be the path to the file. | 
 | 262 |  | 
 | 263 |  | 
 | 264 | .. function:: run_unittest(*classes) | 
 | 265 |  | 
 | 266 |    Execute :class:`unittest.TestCase` subclasses passed to the function. The | 
 | 267 |    function scans the classes for methods starting with the prefix ``test_`` and | 
 | 268 |    executes the tests individually. | 
 | 269 |  | 
 | 270 |    It is also legal to pass strings as parameters; these should be keys in | 
 | 271 |    ``sys.modules``. Each associated module will be scanned by | 
 | 272 |    ``unittest.TestLoader.loadTestsFromModule()``. This is usually seen in the | 
 | 273 |    following :func:`test_main` function:: | 
 | 274 |  | 
 | 275 |       def test_main(): | 
 | 276 |           test_support.run_unittest(__name__) | 
 | 277 |  | 
 | 278 |    This will run all tests defined in the named module. | 
 | 279 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 280 |  | 
| Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 281 | .. function:: catch_warning(record=True) | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 282 |  | 
| Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 283 |    Return a context manager that guards the warnings filter from being | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 284 |    permanently changed and records the data of the last warning that has been | 
| Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 285 |    issued. The ``record`` argument specifies whether any raised warnings are | 
 | 286 |    captured by the object returned by :func:`warnings.catch_warning` or allowed | 
 | 287 |    to propagate as normal. | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 288 |  | 
| Benjamin Peterson | 699adb9 | 2008-05-08 22:27:58 +0000 | [diff] [blame] | 289 |    The context manager is typically used like this:: | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 290 |  | 
 | 291 |       with catch_warning() as w: | 
 | 292 |           warnings.warn("foo") | 
 | 293 |           assert str(w.message) == "foo" | 
 | 294 |  | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 295 |  | 
 | 296 | .. function:: captured_stdout() | 
 | 297 |  | 
 | 298 |    This is a context manager than runs the :keyword:`with` statement body using | 
 | 299 |    a :class:`StringIO.StringIO` object as sys.stdout.  That object can be | 
| Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 300 |    retrieved using the ``as`` clause of the :keyword:`with` statement. | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 301 |  | 
 | 302 |    Example use:: | 
 | 303 |  | 
 | 304 |       with captured_stdout() as s: | 
| Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 305 |           print("hello") | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 306 |       assert s.getvalue() == "hello" | 
 | 307 |  | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 308 |  | 
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 309 | The :mod:`test.support` module defines the following classes: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 310 |  | 
 | 311 | .. class:: TransientResource(exc[, **kwargs]) | 
 | 312 |  | 
 | 313 |    Instances are a context manager that raises :exc:`ResourceDenied` if the | 
 | 314 |    specified exception type is raised.  Any keyword arguments are treated as | 
 | 315 |    attribute/value pairs to be compared against any exception raised within the | 
 | 316 |    :keyword:`with` statement.  Only if all pairs match properly against | 
 | 317 |    attributes on the exception is :exc:`ResourceDenied` raised. | 
 | 318 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 |  | 
 | 320 | .. class:: EnvironmentVarGuard() | 
 | 321 |  | 
 | 322 |    Class used to temporarily set or unset environment variables.  Instances can be | 
 | 323 |    used as a context manager. | 
 | 324 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 |  | 
 | 326 | .. method:: EnvironmentVarGuard.set(envvar, value) | 
 | 327 |  | 
 | 328 |    Temporarily set the environment variable ``envvar`` to the value of ``value``. | 
 | 329 |  | 
 | 330 |  | 
 | 331 | .. method:: EnvironmentVarGuard.unset(envvar) | 
 | 332 |  | 
 | 333 |    Temporarily unset the environment variable ``envvar``. |