Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Brett Cannon | 9ae2cb7 | 2010-07-23 12:07:27 +0000 | [diff] [blame] | 9 | .. note:: |
| 10 | The :mod:`test` package is meant for internal use by Python only. It is |
| 11 | documented for the benefit of the core developers of Python. Any use of |
| 12 | this package outside of Python's standard library is discouraged as code |
| 13 | mentioned here can change or be removed without notice between releases of |
| 14 | Python. |
| 15 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 16 | |
| 17 | The :mod:`test` package contains all regression tests for Python as well as the |
| 18 | modules :mod:`test.test_support` and :mod:`test.regrtest`. |
| 19 | :mod:`test.test_support` is used to enhance your tests while |
| 20 | :mod:`test.regrtest` drives the testing suite. |
| 21 | |
| 22 | Each module in the :mod:`test` package whose name starts with ``test_`` is a |
| 23 | testing suite for a specific module or feature. All new tests should be written |
| 24 | using the :mod:`unittest` or :mod:`doctest` module. Some older tests are |
| 25 | written using a "traditional" testing style that compares output printed to |
| 26 | ``sys.stdout``; this style of test is considered deprecated. |
| 27 | |
| 28 | |
| 29 | .. seealso:: |
| 30 | |
| 31 | Module :mod:`unittest` |
| 32 | Writing PyUnit regression tests. |
| 33 | |
| 34 | Module :mod:`doctest` |
| 35 | Tests embedded in documentation strings. |
| 36 | |
| 37 | |
| 38 | .. _writing-tests: |
| 39 | |
| 40 | Writing Unit Tests for the :mod:`test` package |
| 41 | ---------------------------------------------- |
| 42 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 43 | It is preferred that tests that use the :mod:`unittest` module follow a few |
| 44 | guidelines. One is to name the test module by starting it with ``test_`` and end |
| 45 | it with the name of the module being tested. The test methods in the test module |
| 46 | should start with ``test_`` and end with a description of what the method is |
| 47 | testing. This is needed so that the methods are recognized by the test driver as |
| 48 | test methods. Also, no documentation string for the method should be included. A |
| 49 | comment (such as ``# Tests function returns only True or False``) should be used |
| 50 | to provide documentation for test methods. This is done because documentation |
| 51 | strings get printed out if they exist and thus what test is being run is not |
| 52 | stated. |
| 53 | |
| 54 | A basic boilerplate is often used:: |
| 55 | |
| 56 | import unittest |
| 57 | from test import test_support |
| 58 | |
| 59 | class MyTestCase1(unittest.TestCase): |
| 60 | |
| 61 | # Only use setUp() and tearDown() if necessary |
| 62 | |
| 63 | def setUp(self): |
| 64 | ... code to execute in preparation for tests ... |
| 65 | |
| 66 | def tearDown(self): |
| 67 | ... code to execute to clean up after tests ... |
| 68 | |
| 69 | def test_feature_one(self): |
| 70 | # Test feature one. |
| 71 | ... testing code ... |
| 72 | |
| 73 | def test_feature_two(self): |
| 74 | # Test feature two. |
| 75 | ... testing code ... |
| 76 | |
| 77 | ... more test methods ... |
| 78 | |
| 79 | class MyTestCase2(unittest.TestCase): |
| 80 | ... same structure as MyTestCase1 ... |
| 81 | |
| 82 | ... more test classes ... |
| 83 | |
| 84 | def test_main(): |
| 85 | test_support.run_unittest(MyTestCase1, |
| 86 | MyTestCase2, |
| 87 | ... list other tests ... |
| 88 | ) |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | test_main() |
| 92 | |
| 93 | This boilerplate code allows the testing suite to be run by :mod:`test.regrtest` |
| 94 | as well as on its own as a script. |
| 95 | |
| 96 | The goal for regression testing is to try to break code. This leads to a few |
| 97 | guidelines to be followed: |
| 98 | |
| 99 | * The testing suite should exercise all classes, functions, and constants. This |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 100 | includes not just the external API that is to be presented to the outside |
| 101 | world but also "private" code. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 102 | |
| 103 | * Whitebox testing (examining the code being tested when the tests are being |
| 104 | written) is preferred. Blackbox testing (testing only the published user |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 105 | interface) is not complete enough to make sure all boundary and edge cases |
| 106 | are tested. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 107 | |
| 108 | * Make sure all possible values are tested including invalid ones. This makes |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 109 | sure that not only all valid values are acceptable but also that improper |
| 110 | values are handled correctly. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 111 | |
| 112 | * Exhaust as many code paths as possible. Test where branching occurs and thus |
| 113 | tailor input to make sure as many different paths through the code are taken. |
| 114 | |
| 115 | * Add an explicit test for any bugs discovered for the tested code. This will |
| 116 | make sure that the error does not crop up again if the code is changed in the |
| 117 | future. |
| 118 | |
| 119 | * Make sure to clean up after your tests (such as close and remove all temporary |
| 120 | files). |
| 121 | |
| 122 | * If a test is dependent on a specific condition of the operating system then |
| 123 | verify the condition already exists before attempting the test. |
| 124 | |
| 125 | * Import as few modules as possible and do it as soon as possible. This |
| 126 | minimizes external dependencies of tests and also minimizes possible anomalous |
| 127 | behavior from side-effects of importing a module. |
| 128 | |
| 129 | * Try to maximize code reuse. On occasion, tests will vary by something as small |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 130 | as what type of input is used. Minimize code duplication by subclassing a |
| 131 | basic test class with a class that specifies the input:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 132 | |
| 133 | class TestFuncAcceptsSequences(unittest.TestCase): |
| 134 | |
| 135 | func = mySuperWhammyFunction |
| 136 | |
| 137 | def test_func(self): |
| 138 | self.func(self.arg) |
| 139 | |
| 140 | class AcceptLists(TestFuncAcceptsSequences): |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 141 | arg = [1, 2, 3] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 142 | |
| 143 | class AcceptStrings(TestFuncAcceptsSequences): |
| 144 | arg = 'abc' |
| 145 | |
| 146 | class AcceptTuples(TestFuncAcceptsSequences): |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 147 | arg = (1, 2, 3) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | .. seealso:: |
| 151 | |
| 152 | Test Driven Development |
| 153 | A book by Kent Beck on writing tests before code. |
| 154 | |
| 155 | |
| 156 | .. _regrtest: |
| 157 | |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 158 | Running tests using the command-line interface |
| 159 | ---------------------------------------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 160 | |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 161 | The :mod:`test.regrtest` module can be run as a script to drive Python's regression |
| 162 | test suite, thanks to the :option:`-m` option: :program:`python -m test.regrtest`. |
| 163 | Running the script by itself automatically starts running all regression |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 164 | tests in the :mod:`test` package. It does this by finding all modules in the |
| 165 | package whose name starts with ``test_``, importing them, and executing the |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 166 | function :func:`test_main` if present. The names of tests to execute may also |
| 167 | be passed to the script. Specifying a single regression test (:program:`python |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 168 | -m test.regrtest test_spam`) will minimize output and only print whether |
| 169 | the test passed or failed and thus minimize output. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 170 | |
| 171 | Running :mod:`test.regrtest` directly allows what resources are available for |
R David Murray | 142ab32 | 2012-04-11 20:38:45 -0400 | [diff] [blame] | 172 | tests to use to be set. You do this by using the ``-u`` command-line |
| 173 | option. Specifying ``all`` as the value for the ``-u`` option enables all |
| 174 | possible resources: :program:`python -m test -uall`. |
| 175 | If all but one resource is desired (a more common case), a |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 176 | comma-separated list of resources that are not desired may be listed after |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 177 | ``all``. The command :program:`python -m test.regrtest -uall,-audio,-largefile` |
| 178 | will run :mod:`test.regrtest` with all resources except the ``audio`` and |
| 179 | ``largefile`` resources. For a list of all resources and more command-line |
| 180 | options, run :program:`python -m test.regrtest -h`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 181 | |
| 182 | Some other ways to execute the regression tests depend on what platform the |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 183 | tests are being executed on. On Unix, you can run :program:`make test` at the |
| 184 | top-level directory where Python was built. On Windows, executing |
| 185 | :program:`rt.bat` from your :file:`PCBuild` directory will run all regression |
| 186 | tests. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | :mod:`test.test_support` --- Utility functions for tests |
| 190 | ======================================================== |
| 191 | |
| 192 | .. module:: test.test_support |
| 193 | :synopsis: Support for Python regression tests. |
| 194 | |
Brett Cannon | e76e4d7 | 2008-05-20 22:08:20 +0000 | [diff] [blame] | 195 | .. note:: |
| 196 | |
| 197 | The :mod:`test.test_support` module has been renamed to :mod:`test.support` |
Benjamin Peterson | 9b14ae8 | 2010-05-28 03:10:31 +0000 | [diff] [blame] | 198 | in Python 3.x. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 199 | |
| 200 | The :mod:`test.test_support` module provides support for Python's regression |
| 201 | tests. |
| 202 | |
| 203 | This module defines the following exceptions: |
| 204 | |
| 205 | |
| 206 | .. exception:: TestFailed |
| 207 | |
| 208 | Exception to be raised when a test fails. This is deprecated in favor of |
| 209 | :mod:`unittest`\ -based tests and :class:`unittest.TestCase`'s assertion |
| 210 | methods. |
| 211 | |
| 212 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 213 | .. exception:: ResourceDenied |
| 214 | |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 215 | Subclass of :exc:`unittest.SkipTest`. Raised when a resource (such as a |
| 216 | network connection) is not available. Raised by the :func:`requires` |
| 217 | function. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 218 | |
| 219 | The :mod:`test.test_support` module defines the following constants: |
| 220 | |
| 221 | |
| 222 | .. data:: verbose |
| 223 | |
| 224 | :const:`True` when verbose output is enabled. Should be checked when more |
| 225 | detailed information is desired about a running test. *verbose* is set by |
| 226 | :mod:`test.regrtest`. |
| 227 | |
| 228 | |
| 229 | .. data:: have_unicode |
| 230 | |
| 231 | :const:`True` when Unicode support is available. |
| 232 | |
| 233 | |
| 234 | .. data:: is_jython |
| 235 | |
| 236 | :const:`True` if the running interpreter is Jython. |
| 237 | |
| 238 | |
| 239 | .. data:: TESTFN |
| 240 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 241 | Set to a name that is safe to use as the name of a temporary file. Any |
| 242 | temporary file that is created should be closed and unlinked (removed). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 243 | |
| 244 | The :mod:`test.test_support` module defines the following functions: |
| 245 | |
| 246 | |
| 247 | .. function:: forget(module_name) |
| 248 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 249 | Remove the module named *module_name* from ``sys.modules`` and delete any |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 250 | byte-compiled files of the module. |
| 251 | |
| 252 | |
| 253 | .. function:: is_resource_enabled(resource) |
| 254 | |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 255 | Return :const:`True` if *resource* is enabled and available. The list of |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 256 | available resources is only set when :mod:`test.regrtest` is executing the |
| 257 | tests. |
| 258 | |
| 259 | |
| 260 | .. function:: requires(resource[, msg]) |
| 261 | |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 262 | Raise :exc:`ResourceDenied` if *resource* is not available. *msg* is the |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 263 | argument to :exc:`ResourceDenied` if it is raised. Always returns |
| 264 | :const:`True` if called by a function whose ``__name__`` is ``'__main__'``. |
| 265 | Used when tests are executed by :mod:`test.regrtest`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 266 | |
| 267 | |
| 268 | .. function:: findfile(filename) |
| 269 | |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 270 | Return the path to the file named *filename*. If no match is found |
| 271 | *filename* is returned. This does not equal a failure since it could be the |
| 272 | path to the file. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 273 | |
| 274 | |
| 275 | .. function:: run_unittest(*classes) |
| 276 | |
| 277 | Execute :class:`unittest.TestCase` subclasses passed to the function. The |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 278 | function scans the classes for methods starting with the prefix ``test_`` |
| 279 | and executes the tests individually. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 280 | |
| 281 | It is also legal to pass strings as parameters; these should be keys in |
| 282 | ``sys.modules``. Each associated module will be scanned by |
| 283 | ``unittest.TestLoader.loadTestsFromModule()``. This is usually seen in the |
| 284 | following :func:`test_main` function:: |
| 285 | |
| 286 | def test_main(): |
| 287 | test_support.run_unittest(__name__) |
| 288 | |
| 289 | This will run all tests defined in the named module. |
| 290 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 291 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 292 | .. function:: check_warnings(*filters, quiet=True) |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 293 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 294 | A convenience wrapper for :func:`warnings.catch_warnings()` that makes it |
| 295 | easier to test that a warning was correctly raised. It is approximately |
| 296 | equivalent to calling ``warnings.catch_warnings(record=True)`` with |
| 297 | :meth:`warnings.simplefilter` set to ``always`` and with the option to |
| 298 | automatically validate the results that are recorded. |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 299 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 300 | ``check_warnings`` accepts 2-tuples of the form ``("message regexp", |
| 301 | WarningCategory)`` as positional arguments. If one or more *filters* are |
| 302 | provided, or if the optional keyword argument *quiet* is :const:`False`, |
| 303 | it checks to make sure the warnings are as expected: each specified filter |
| 304 | must match at least one of the warnings raised by the enclosed code or the |
| 305 | test fails, and if any warnings are raised that do not match any of the |
| 306 | specified filters the test fails. To disable the first of these checks, |
| 307 | set *quiet* to :const:`True`. |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 308 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 309 | If no arguments are specified, it defaults to:: |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 310 | |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 311 | check_warnings(("", Warning), quiet=True) |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 312 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 313 | In this case all warnings are caught and no errors are raised. |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 314 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 315 | On entry to the context manager, a :class:`WarningRecorder` instance is |
| 316 | returned. The underlying warnings list from |
| 317 | :func:`~warnings.catch_warnings` is available via the recorder object's |
| 318 | :attr:`warnings` attribute. As a convenience, the attributes of the object |
| 319 | representing the most recent warning can also be accessed directly through |
| 320 | the recorder object (see example below). If no warning has been raised, |
| 321 | then any of the attributes that would otherwise be expected on an object |
| 322 | representing a warning will return :const:`None`. |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 323 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 324 | The recorder object also has a :meth:`reset` method, which clears the |
| 325 | warnings list. |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 326 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 327 | The context manager is designed to be used like this:: |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 328 | |
| 329 | with check_warnings(("assertion is always true", SyntaxWarning), |
| 330 | ("", UserWarning)): |
| 331 | exec('assert(False, "Hey!")') |
| 332 | warnings.warn(UserWarning("Hide me!")) |
| 333 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 334 | In this case if either warning was not raised, or some other warning was |
| 335 | raised, :func:`check_warnings` would raise an error. |
| 336 | |
| 337 | When a test needs to look more deeply into the warnings, rather than |
| 338 | just checking whether or not they occurred, code like this can be used:: |
| 339 | |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 340 | with check_warnings(quiet=True) as w: |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 341 | warnings.warn("foo") |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 342 | assert str(w.args[0]) == "foo" |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 343 | warnings.warn("bar") |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 344 | assert str(w.args[0]) == "bar" |
| 345 | assert str(w.warnings[0].args[0]) == "foo" |
| 346 | assert str(w.warnings[1].args[0]) == "bar" |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 347 | w.reset() |
| 348 | assert len(w.warnings) == 0 |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 349 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 350 | Here all warnings will be caught, and the test code tests the captured |
| 351 | warnings directly. |
| 352 | |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 353 | .. versionadded:: 2.6 |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 354 | .. versionchanged:: 2.7 |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 355 | New optional arguments *filters* and *quiet*. |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 356 | |
| 357 | |
| 358 | .. function:: check_py3k_warnings(*filters, quiet=False) |
| 359 | |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 360 | Similar to :func:`check_warnings`, but for Python 3 compatibility warnings. |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 361 | If ``sys.py3kwarning == 1``, it checks if the warning is effectively raised. |
R. David Murray | 6c9fc4c | 2010-04-01 01:28:39 +0000 | [diff] [blame] | 362 | If ``sys.py3kwarning == 0``, it checks that no warning is raised. It |
| 363 | accepts 2-tuples of the form ``("message regexp", WarningCategory)`` as |
| 364 | positional arguments. When the optional keyword argument *quiet* is |
| 365 | :const:`True`, it does not fail if a filter catches nothing. Without |
| 366 | arguments, it defaults to:: |
Florent Xicluna | fc8a1ed | 2010-03-07 19:14:12 +0000 | [diff] [blame] | 367 | |
| 368 | check_py3k_warnings(("", DeprecationWarning), quiet=False) |
| 369 | |
| 370 | .. versionadded:: 2.7 |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 371 | |
| 372 | |
| 373 | .. function:: captured_stdout() |
| 374 | |
Senthil Kumaran | 71a6324 | 2010-05-18 03:20:43 +0000 | [diff] [blame] | 375 | This is a context manager that runs the :keyword:`with` statement body using |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 376 | a :class:`StringIO.StringIO` object as sys.stdout. That object can be |
Andrew M. Kuchling | f15ff46 | 2008-01-15 01:29:44 +0000 | [diff] [blame] | 377 | retrieved using the ``as`` clause of the :keyword:`with` statement. |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 378 | |
| 379 | Example use:: |
| 380 | |
| 381 | with captured_stdout() as s: |
| 382 | print "hello" |
Sandro Tosi | 466a5f2 | 2012-04-24 18:38:59 +0200 | [diff] [blame^] | 383 | assert s.getvalue() == "hello\n" |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 384 | |
| 385 | .. versionadded:: 2.6 |
| 386 | |
| 387 | |
Nick Coghlan | cd2e704 | 2009-04-11 13:31:31 +0000 | [diff] [blame] | 388 | .. function:: import_module(name, deprecated=False) |
| 389 | |
| 390 | This function imports and returns the named module. Unlike a normal |
| 391 | import, this function raises :exc:`unittest.SkipTest` if the module |
| 392 | cannot be imported. |
| 393 | |
| 394 | Module and package deprecation messages are suppressed during this import |
| 395 | if *deprecated* is :const:`True`. |
| 396 | |
| 397 | .. versionadded:: 2.7 |
| 398 | |
| 399 | |
Nick Coghlan | 5533ff6 | 2009-04-22 15:26:04 +0000 | [diff] [blame] | 400 | .. function:: import_fresh_module(name, fresh=(), blocked=(), deprecated=False) |
Nick Coghlan | cd2e704 | 2009-04-11 13:31:31 +0000 | [diff] [blame] | 401 | |
Nick Coghlan | 5533ff6 | 2009-04-22 15:26:04 +0000 | [diff] [blame] | 402 | This function imports and returns a fresh copy of the named Python module |
| 403 | by removing the named module from ``sys.modules`` before doing the import. |
| 404 | Note that unlike :func:`reload`, the original module is not affected by |
| 405 | this operation. |
| 406 | |
| 407 | *fresh* is an iterable of additional module names that are also removed |
| 408 | from the ``sys.modules`` cache before doing the import. |
| 409 | |
| 410 | *blocked* is an iterable of module names that are replaced with :const:`0` |
| 411 | in the module cache during the import to ensure that attempts to import |
| 412 | them raise :exc:`ImportError`. |
| 413 | |
| 414 | The named module and any modules named in the *fresh* and *blocked* |
| 415 | parameters are saved before starting the import and then reinserted into |
| 416 | ``sys.modules`` when the fresh import is complete. |
Nick Coghlan | cd2e704 | 2009-04-11 13:31:31 +0000 | [diff] [blame] | 417 | |
| 418 | Module and package deprecation messages are suppressed during this import |
| 419 | if *deprecated* is :const:`True`. |
| 420 | |
Nick Coghlan | 5533ff6 | 2009-04-22 15:26:04 +0000 | [diff] [blame] | 421 | This function will raise :exc:`unittest.SkipTest` is the named module |
| 422 | cannot be imported. |
| 423 | |
| 424 | Example use:: |
| 425 | |
| 426 | # Get copies of the warnings module for testing without |
| 427 | # affecting the version being used by the rest of the test suite |
| 428 | # One copy uses the C implementation, the other is forced to use |
| 429 | # the pure Python fallback implementation |
| 430 | py_warnings = import_fresh_module('warnings', blocked=['_warnings']) |
| 431 | c_warnings = import_fresh_module('warnings', fresh=['_warnings']) |
| 432 | |
Nick Coghlan | cd2e704 | 2009-04-11 13:31:31 +0000 | [diff] [blame] | 433 | .. versionadded:: 2.7 |
| 434 | |
| 435 | |
Georg Brandl | d558f67 | 2007-08-24 18:27:43 +0000 | [diff] [blame] | 436 | The :mod:`test.test_support` module defines the following classes: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 437 | |
| 438 | .. class:: TransientResource(exc[, **kwargs]) |
| 439 | |
| 440 | Instances are a context manager that raises :exc:`ResourceDenied` if the |
| 441 | specified exception type is raised. Any keyword arguments are treated as |
| 442 | attribute/value pairs to be compared against any exception raised within the |
| 443 | :keyword:`with` statement. Only if all pairs match properly against |
| 444 | attributes on the exception is :exc:`ResourceDenied` raised. |
| 445 | |
| 446 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 447 | .. class:: EnvironmentVarGuard() |
| 448 | |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 449 | Class used to temporarily set or unset environment variables. Instances can |
| 450 | be used as a context manager and have a complete dictionary interface for |
| 451 | querying/modifying the underlying ``os.environ``. After exit from the |
| 452 | context manager all changes to environment variables done through this |
| 453 | instance will be rolled back. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 454 | |
| 455 | .. versionadded:: 2.6 |
Walter Dörwald | 6733bed | 2009-05-01 17:35:37 +0000 | [diff] [blame] | 456 | .. versionchanged:: 2.7 |
| 457 | Added dictionary interface. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 458 | |
| 459 | |
| 460 | .. method:: EnvironmentVarGuard.set(envvar, value) |
| 461 | |
Florent Xicluna | 7358854 | 2010-03-18 19:51:47 +0000 | [diff] [blame] | 462 | Temporarily set the environment variable ``envvar`` to the value of |
| 463 | ``value``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 464 | |
| 465 | |
| 466 | .. method:: EnvironmentVarGuard.unset(envvar) |
| 467 | |
| 468 | Temporarily unset the environment variable ``envvar``. |
Nick Coghlan | 38469e2 | 2008-07-13 12:23:47 +0000 | [diff] [blame] | 469 | |
Walter Dörwald | 6733bed | 2009-05-01 17:35:37 +0000 | [diff] [blame] | 470 | |
Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 471 | .. class:: WarningsRecorder() |
| 472 | |
| 473 | Class used to record warnings for unit tests. See documentation of |
| 474 | :func:`check_warnings` above for more details. |
| 475 | |
| 476 | .. versionadded:: 2.6 |