Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 1 | \section{\module{test} --- |
| 2 | Regression tests package for Python} |
| 3 | |
| 4 | \declaremodule{standard}{test} |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 5 | \sectionauthor{Brett Cannon}{brett@python.org} |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 6 | \modulesynopsis{Regression tests package containing the testing suite |
| 7 | for Python.} |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 8 | |
| 9 | |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 10 | The \module{test} package contains all regression tests for Python as |
| 11 | well as the modules \module{test.test_support} and |
| 12 | \module{test.regrtest}. \module{test.test_support} is used to enhance |
| 13 | your tests while \module{test.regrtest} drives the testing suite. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 14 | |
| 15 | Each module in the \module{test} package whose name starts with |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 16 | \samp{test_} is a testing suite for a specific module or feature. |
| 17 | All new tests should be written using the \refmodule{unittest} module; |
| 18 | using \refmodule{unittest} is not required but makes the tests more |
| 19 | flexible and maintenance of the tests easier. Some older tests are |
| 20 | written to use \refmodule{doctest} and a ``traditional'' testing |
| 21 | style; these styles of tests will not be covered. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 22 | |
| 23 | \begin{seealso} |
| 24 | \seemodule{unittest}{Writing PyUnit regression tests.} |
| 25 | \seemodule{doctest}{Tests embedded in documentation strings.} |
| 26 | \end{seealso} |
| 27 | |
| 28 | |
Fred Drake | 9f545c4 | 2003-05-09 19:10:12 +0000 | [diff] [blame] | 29 | \subsection{Writing Unit Tests for the \module{test} package% |
| 30 | \label{writing-tests}} |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 31 | |
| 32 | It is preferred that tests for the \module{test} package use the |
Fred Drake | 9f545c4 | 2003-05-09 19:10:12 +0000 | [diff] [blame] | 33 | \refmodule{unittest} module and follow a few guidelines. |
Brett Cannon | c8aa848 | 2004-12-06 06:08:59 +0000 | [diff] [blame] | 34 | One is to name the test module by starting it with \samp{test_} and end it with |
| 35 | the name of the module being tested. |
| 36 | The test methods in the test module should start with \samp{test_} and end with |
| 37 | a description of what the method is testing. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 38 | This is needed so that the methods are recognized by the test driver as |
| 39 | test methods. |
| 40 | Also, no documentation string for the method should be included. |
| 41 | A comment (such as |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 42 | \samp{\# Tests function returns only True or False}) should be used to provide |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 43 | documentation for test methods. |
| 44 | This is done because documentation strings get printed out if they exist and |
| 45 | thus what test is being run is not stated. |
| 46 | |
| 47 | A basic boilerplate is often used: |
| 48 | |
| 49 | \begin{verbatim} |
| 50 | import unittest |
| 51 | from test import test_support |
| 52 | |
| 53 | class MyTestCase1(unittest.TestCase): |
| 54 | |
| 55 | # Only use setUp() and tearDown() if necessary |
| 56 | |
| 57 | def setUp(self): |
| 58 | ... code to execute in preparation for tests ... |
| 59 | |
| 60 | def tearDown(self): |
| 61 | ... code to execute to clean up after tests ... |
| 62 | |
| 63 | def test_feature_one(self): |
| 64 | # Test feature one. |
| 65 | ... testing code ... |
| 66 | |
| 67 | def test_feature_two(self): |
| 68 | # Test feature two. |
| 69 | ... testing code ... |
| 70 | |
| 71 | ... more test methods ... |
| 72 | |
| 73 | class MyTestCase2(unittest.TestCase): |
| 74 | ... same structure as MyTestCase1 ... |
| 75 | |
| 76 | ... more test classes ... |
| 77 | |
| 78 | def test_main(): |
| 79 | test_support.run_unittest(MyTestCase1, |
| 80 | MyTestCase2, |
| 81 | ... list other tests ... |
| 82 | ) |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | test_main() |
| 86 | \end{verbatim} |
| 87 | |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 88 | This boilerplate code allows the testing suite to be run by |
| 89 | \module{test.regrtest} as well as on its own as a script. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 90 | |
| 91 | The goal for regression testing is to try to break code. |
| 92 | This leads to a few guidelines to be followed: |
| 93 | |
| 94 | \begin{itemize} |
| 95 | \item The testing suite should exercise all classes, functions, and |
| 96 | constants. |
| 97 | This includes not just the external API that is to be presented to the |
| 98 | outside world but also "private" code. |
| 99 | \item Whitebox testing (examining the code being tested when the tests are |
| 100 | being written) is preferred. |
| 101 | Blackbox testing (testing only the published user interface) is not |
| 102 | complete enough to make sure all boundary and edge cases are tested. |
| 103 | \item Make sure all possible values are tested including invalid ones. |
| 104 | This makes sure that not only all valid values are acceptable but also |
| 105 | that improper values are handled correctly. |
| 106 | \item Exhaust as many code paths as possible. |
| 107 | Test where branching occurs and thus tailor input to make sure as many |
| 108 | different paths through the code are taken. |
| 109 | \item Add an explicit test for any bugs discovered for the tested code. |
| 110 | This will make sure that the error does not crop up again if the code is |
| 111 | changed in the future. |
| 112 | \item Make sure to clean up after your tests (such as close and remove all |
| 113 | temporary files). |
Brett Cannon | c8aa848 | 2004-12-06 06:08:59 +0000 | [diff] [blame] | 114 | \item If a test is dependent on a specific condition of the operating system |
| 115 | then verify the condition already exists before attempting the test. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 116 | \item Import as few modules as possible and do it as soon as possible. |
| 117 | This minimizes external dependencies of tests and also minimizes possible |
| 118 | anomalous behavior from side-effects of importing a module. |
| 119 | \item Try to maximize code reuse. |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 120 | On occasion, tests will vary by something as small as what type |
| 121 | of input is used. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 122 | Minimize code duplication by subclassing a basic test class with a class |
| 123 | that specifies the input: |
| 124 | \begin{verbatim} |
| 125 | class TestFuncAcceptsSequences(unittest.TestCase): |
| 126 | |
| 127 | func = mySuperWhammyFunction |
| 128 | |
| 129 | def test_func(self): |
| 130 | self.func(self.arg) |
| 131 | |
| 132 | class AcceptLists(TestFuncAcceptsSequences): |
| 133 | arg = [1,2,3] |
| 134 | |
| 135 | class AcceptStrings(TestFuncAcceptsSequences): |
| 136 | arg = 'abc' |
| 137 | |
| 138 | class AcceptTuples(TestFuncAcceptsSequences): |
| 139 | arg = (1,2,3) |
| 140 | \end{verbatim} |
| 141 | \end{itemize} |
| 142 | |
| 143 | \begin{seealso} |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 144 | \seetitle{Test Driven Development} |
| 145 | {A book by Kent Beck on writing tests before code.} |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 146 | \end{seealso} |
| 147 | |
| 148 | |
Brett Cannon | d1de45f | 2004-03-18 07:37:15 +0000 | [diff] [blame] | 149 | \subsection{Running tests using \module{test.regrtest} \label{regrtest}} |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 150 | |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 151 | \module{test.regrtest} can be used as a script to drive Python's |
| 152 | regression test suite. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 153 | Running the script by itself automatically starts running all |
| 154 | regression tests in the \module{test} package. |
| 155 | It does this by finding all modules in the package whose name starts with |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 156 | \samp{test_}, importing them, and executing the function |
| 157 | \function{test_main()} if present. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 158 | The names of tests to execute may also be passed to the script. |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 159 | Specifying a single regression test (\program{python regrtest.py} |
| 160 | \programopt{test_spam.py}) will minimize output and only print whether |
| 161 | the test passed or failed and thus minimize output. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 162 | |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 163 | Running \module{test.regrtest} directly allows what resources are |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 164 | available for tests to use to be set. |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 165 | You do this by using the \programopt{-u} command-line option. |
| 166 | Run \program{python regrtest.py} \programopt{-uall} to turn on all |
| 167 | resources; specifying \programopt{all} as an option for |
| 168 | \programopt{-u} enables all possible resources. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 169 | If all but one resource is desired (a more common case), a |
| 170 | comma-separated list of resources that are not desired may be listed after |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 171 | \programopt{all}. |
| 172 | The command \program{python regrtest.py} |
| 173 | \programopt{-uall,-audio,-largefile} will run \module{test.regrtest} |
| 174 | with all resources except the \programopt{audio} and |
| 175 | \programopt{largefile} resources. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 176 | For a list of all resources and more command-line options, run |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 177 | \program{python regrtest.py} \programopt{-h}. |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 178 | |
| 179 | Some other ways to execute the regression tests depend on what platform the |
| 180 | tests are being executed on. |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 181 | On \UNIX{}, you can run \program{make} \programopt{test} at the |
| 182 | top-level directory where Python was built. |
| 183 | On Windows, executing \program{rt.bat} from your \file{PCBuild} |
| 184 | directory will run all regression tests. |
| 185 | |
| 186 | |
| 187 | \section{\module{test.test_support} --- |
| 188 | Utility functions for tests} |
| 189 | |
| 190 | \declaremodule[test.testsupport]{standard}{test.test_support} |
| 191 | \modulesynopsis{Support for Python regression tests.} |
| 192 | |
| 193 | The \module{test.test_support} module provides support for Python's |
Brett Cannon | 066f392 | 2003-05-07 22:02:17 +0000 | [diff] [blame] | 194 | regression tests. |
| 195 | |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 196 | This module defines the following exceptions: |
| 197 | |
| 198 | \begin{excdesc}{TestFailed} |
| 199 | Exception to be raised when a test fails. |
| 200 | \end{excdesc} |
| 201 | |
| 202 | \begin{excdesc}{TestSkipped} |
| 203 | Subclass of \exception{TestFailed}. |
| 204 | Raised when a test is skipped. |
| 205 | This occurs when a needed resource (such as a network connection) is not |
| 206 | available at the time of testing. |
| 207 | \end{excdesc} |
| 208 | |
| 209 | \begin{excdesc}{ResourceDenied} |
| 210 | Subclass of \exception{TestSkipped}. |
| 211 | Raised when a resource (such as a network connection) is not available. |
| 212 | Raised by the \function{requires()} function. |
| 213 | \end{excdesc} |
| 214 | |
| 215 | |
| 216 | The \module{test.test_support} module defines the following constants: |
| 217 | |
| 218 | \begin{datadesc}{verbose} |
| 219 | \constant{True} when verbose output is enabled. |
| 220 | Should be checked when more detailed information is desired about a running |
| 221 | test. |
| 222 | \var{verbose} is set by \module{test.regrtest}. |
| 223 | \end{datadesc} |
| 224 | |
| 225 | \begin{datadesc}{have_unicode} |
| 226 | \constant{True} when Unicode support is available. |
| 227 | \end{datadesc} |
| 228 | |
| 229 | \begin{datadesc}{is_jython} |
| 230 | \constant{True} if the running interpreter is Jython. |
| 231 | \end{datadesc} |
| 232 | |
| 233 | \begin{datadesc}{TESTFN} |
| 234 | Set to the path that a temporary file may be created at. |
| 235 | Any temporary that is created should be closed and unlinked (removed). |
| 236 | \end{datadesc} |
| 237 | |
| 238 | |
| 239 | The \module{test.test_support} module defines the following functions: |
| 240 | |
| 241 | \begin{funcdesc}{forget}{module_name} |
| 242 | Removes the module named \var{module_name} from \code{sys.modules} and deletes |
| 243 | any byte-compiled files of the module. |
| 244 | \end{funcdesc} |
| 245 | |
| 246 | \begin{funcdesc}{is_resource_enabled}{resource} |
| 247 | Returns \constant{True} if \var{resource} is enabled and available. |
| 248 | The list of available resources is only set when \module{test.regrtest} |
| 249 | is executing the tests. |
| 250 | \end{funcdesc} |
| 251 | |
| 252 | \begin{funcdesc}{requires}{resource\optional{, msg}} |
| 253 | Raises \exception{ResourceDenied} if \var{resource} is not available. |
| 254 | \var{msg} is the argument to \exception{ResourceDenied} if it is raised. |
| 255 | Always returns true if called by a function whose \code{__name__} is |
| 256 | \code{'__main__'}. |
| 257 | Used when tests are executed by \module{test.regrtest}. |
| 258 | \end{funcdesc} |
| 259 | |
| 260 | \begin{funcdesc}{findfile}{filename} |
| 261 | Return the path to the file named \var{filename}. |
| 262 | If no match is found \var{filename} is returned. |
| 263 | This does not equal a failure since it could be the path to the file. |
| 264 | \end{funcdesc} |
| 265 | |
Brett Cannon | 6d9520c | 2006-12-13 23:09:53 +0000 | [diff] [blame] | 266 | \begin{funcdesc}{guard_warnings_filter}{} |
| 267 | Returns a context manager that guards the \module{warnings} module's |
| 268 | filter settings. |
George Yoshida | 7366fce | 2006-12-14 02:22:44 +0000 | [diff] [blame] | 269 | \versionadded{2.6} |
| 270 | \end{funcdesc} |
Brett Cannon | 6d9520c | 2006-12-13 23:09:53 +0000 | [diff] [blame] | 271 | |
Fred Drake | 60e868a | 2003-09-06 17:51:16 +0000 | [diff] [blame] | 272 | \begin{funcdesc}{run_unittest}{*classes} |
| 273 | Execute \class{unittest.TestCase} subclasses passed to the function. |
| 274 | The function scans the classes for methods starting with the prefix |
| 275 | \samp{test_} and executes the tests individually. |
| 276 | This is the preferred way to execute tests. |
| 277 | \end{funcdesc} |
| 278 | |
| 279 | \begin{funcdesc}{run_suite}{suite\optional{, testclass}} |
| 280 | Execute the \class{unittest.TestSuite} instance \var{suite}. |
| 281 | The optional argument \var{testclass} accepts one of the test classes in the |
| 282 | suite so as to print out more detailed information on where the testing suite |
| 283 | originated from. |
Brett Cannon | 78a132b | 2007-01-12 07:27:52 +0000 | [diff] [blame^] | 284 | \end{funcdesc} |
Brett Cannon | 92d54d5 | 2007-01-04 00:23:49 +0000 | [diff] [blame] | 285 | |
| 286 | The \module{test.test_support} module defines the following classes: |
| 287 | |
| 288 | \begin{classdesc}{EnvironmentVarGuard}{} |
| 289 | Class used to temporarily set or unset environment variables. Instances can be |
| 290 | used as a context manager. |
| 291 | \versionadded{2.6} |
| 292 | \end{classdesc} |
| 293 | |
| 294 | \begin{methoddesc}{set}{envvar, value} |
| 295 | Temporarily set the environment variable \code{envvar} to the value of |
| 296 | \code{value}. |
| 297 | \end{methoddesc} |
| 298 | |
| 299 | \begin{methoddesc}{unset}{envvar} |
| 300 | Temporarily unset the environment variable \code{envvar}. |
| 301 | \end{methoddesc} |
| 302 | |