| 1 | \section{\module{test} ---
|
|---|
| 2 | Regression tests package for Python}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{test}
|
|---|
| 5 | \sectionauthor{Brett Cannon}{[email protected]}
|
|---|
| 6 | \modulesynopsis{Regression tests package containing the testing suite
|
|---|
| 7 | for Python.}
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 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.
|
|---|
| 14 |
|
|---|
| 15 | Each module in the \module{test} package whose name starts with
|
|---|
| 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.
|
|---|
| 22 |
|
|---|
| 23 | \begin{seealso}
|
|---|
| 24 | \seemodule{unittest}{Writing PyUnit regression tests.}
|
|---|
| 25 | \seemodule{doctest}{Tests embedded in documentation strings.}
|
|---|
| 26 | \end{seealso}
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | \subsection{Writing Unit Tests for the \module{test} package%
|
|---|
| 30 | \label{writing-tests}}
|
|---|
| 31 |
|
|---|
| 32 | It is preferred that tests for the \module{test} package use the
|
|---|
| 33 | \refmodule{unittest} module and follow a few guidelines.
|
|---|
| 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.
|
|---|
| 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
|
|---|
| 42 | \samp{\# Tests function returns only True or False}) should be used to provide
|
|---|
| 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 |
|
|---|
| 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.
|
|---|
| 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).
|
|---|
| 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.
|
|---|
| 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.
|
|---|
| 120 | On occasion, tests will vary by something as small as what type
|
|---|
| 121 | of input is used.
|
|---|
| 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}
|
|---|
| 144 | \seetitle{Test Driven Development}
|
|---|
| 145 | {A book by Kent Beck on writing tests before code.}
|
|---|
| 146 | \end{seealso}
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | \subsection{Running tests using \module{test.regrtest} \label{regrtest}}
|
|---|
| 150 |
|
|---|
| 151 | \module{test.regrtest} can be used as a script to drive Python's
|
|---|
| 152 | regression test suite.
|
|---|
| 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
|
|---|
| 156 | \samp{test_}, importing them, and executing the function
|
|---|
| 157 | \function{test_main()} if present.
|
|---|
| 158 | The names of tests to execute may also be passed to the script.
|
|---|
| 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.
|
|---|
| 162 |
|
|---|
| 163 | Running \module{test.regrtest} directly allows what resources are
|
|---|
| 164 | available for tests to use to be set.
|
|---|
| 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.
|
|---|
| 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
|
|---|
| 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.
|
|---|
| 176 | For a list of all resources and more command-line options, run
|
|---|
| 177 | \program{python regrtest.py} \programopt{-h}.
|
|---|
| 178 |
|
|---|
| 179 | Some other ways to execute the regression tests depend on what platform the
|
|---|
| 180 | tests are being executed on.
|
|---|
| 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
|
|---|
| 194 | regression tests.
|
|---|
| 195 |
|
|---|
| 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 |
|
|---|
| 266 | \begin{funcdesc}{run_unittest}{*classes}
|
|---|
| 267 | Execute \class{unittest.TestCase} subclasses passed to the function.
|
|---|
| 268 | The function scans the classes for methods starting with the prefix
|
|---|
| 269 | \samp{test_} and executes the tests individually.
|
|---|
| 270 | This is the preferred way to execute tests.
|
|---|
| 271 | \end{funcdesc}
|
|---|
| 272 |
|
|---|
| 273 | \begin{funcdesc}{run_suite}{suite\optional{, testclass}}
|
|---|
| 274 | Execute the \class{unittest.TestSuite} instance \var{suite}.
|
|---|
| 275 | The optional argument \var{testclass} accepts one of the test classes in the
|
|---|
| 276 | suite so as to print out more detailed information on where the testing suite
|
|---|
| 277 | originated from.
|
|---|
| 278 | \end{funcdesc}
|
|---|