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