Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 1 | \section{\module{unittest} --- |
| 2 | Unit testing framework} |
| 3 | |
| 4 | \declaremodule{standard}{unittest} |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 5 | \modulesynopsis{Unit testing framework for Python.} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 6 | \moduleauthor{Steve Purcell}{stephen\textunderscore{}purcell@yahoo.com} |
| 7 | \sectionauthor{Steve Purcell}{stephen\textunderscore{}purcell@yahoo.com} |
| 8 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
Raymond Hettinger | 9de3c21 | 2003-07-10 22:14:41 +0000 | [diff] [blame] | 9 | \sectionauthor{Raymond Hettinger}{python@rcn.com} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 10 | |
Fred Drake | 5430f4e | 2002-10-10 16:16:25 +0000 | [diff] [blame] | 11 | \versionadded{2.1} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 12 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 13 | The Python unit testing framework, sometimes referred to as ``PyUnit,'' is |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 14 | a Python language version of JUnit, by Kent Beck and Erich Gamma. |
| 15 | JUnit is, in turn, a Java version of Kent's Smalltalk testing |
| 16 | framework. Each is the de facto standard unit testing framework for |
| 17 | its respective language. |
| 18 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 19 | \module{unittest} supports test automation, sharing of setup and shutdown |
| 20 | code for tests, aggregation of tests into collections, and independence of |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 21 | the tests from the reporting framework. The \module{unittest} module |
| 22 | provides classes that make it easy to support these qualities for a |
| 23 | set of tests. |
| 24 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 25 | To achieve this, \module{unittest} supports some important concepts: |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 26 | |
| 27 | \begin{definitions} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 28 | \term{test fixture} |
| 29 | A \dfn{test fixture} represents the preparation needed to perform one |
| 30 | or more tests, and any associate cleanup actions. This may involve, |
| 31 | for example, creating temporary or proxy databases, directories, or |
| 32 | starting a server process. |
| 33 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 34 | \term{test case} |
| 35 | A \dfn{test case} is the smallest unit of testing. It checks for a |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 36 | specific response to a particular set of inputs. \module{unittest} |
| 37 | provides a base class, \class{TestCase}, which may be used to create |
| 38 | new test cases. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 39 | |
| 40 | \term{test suite} |
| 41 | A \dfn{test suite} is a collection of test cases, test suites, or |
| 42 | both. It is used to aggregate tests that should be executed |
| 43 | together. |
| 44 | |
| 45 | \term{test runner} |
| 46 | A \dfn{test runner} is a component which orchestrates the execution of |
| 47 | tests and provides the outcome to the user. The runner may use a |
| 48 | graphical interface, a textual interface, or return a special value to |
| 49 | indicate the results of executing the tests. |
| 50 | \end{definitions} |
| 51 | |
| 52 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 53 | The test case and test fixture concepts are supported through the |
| 54 | \class{TestCase} and \class{FunctionTestCase} classes; the former |
Just van Rossum | edd179e | 2002-12-15 13:14:22 +0000 | [diff] [blame] | 55 | should be used when creating new tests, and the latter can be used when |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 56 | integrating existing test code with a \module{unittest}-driven framework. |
| 57 | When building test fixtures using \class{TestCase}, the \method{setUp()} |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 58 | and \method{tearDown()} methods can be overridden to provide |
| 59 | initialization and cleanup for the fixture. With |
| 60 | \class{FunctionTestCase}, existing functions can be passed to the |
| 61 | constructor for these purposes. When the test is run, the |
| 62 | fixture initialization is run first; if it succeeds, the cleanup |
| 63 | method is run after the test has been executed, regardless of the |
| 64 | outcome of the test. Each instance of the \class{TestCase} will only |
| 65 | be used to run a single test method, so a new fixture is created for |
| 66 | each test. |
| 67 | |
| 68 | Test suites are implemented by the \class{TestSuite} class. This |
| 69 | class allows individual tests and test suites to be aggregated; when |
| 70 | the suite is executed, all tests added directly to the suite and in |
| 71 | ``child'' test suites are run. |
| 72 | |
| 73 | A test runner is an object that provides a single method, |
| 74 | \method{run()}, which accepts a \class{TestCase} or \class{TestSuite} |
| 75 | object as a parameter, and returns a result object. The class |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | \class{TestResult} is provided for use as the result object. |
| 77 | \module{unittest} provides the \class{TextTestRunner} as an example |
| 78 | test runner which reports test results on the standard error stream by |
| 79 | default. Alternate runners can be implemented for other environments |
| 80 | (such as graphical environments) without any need to derive from a |
| 81 | specific class. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 82 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 83 | |
| 84 | \begin{seealso} |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 85 | \seemodule{doctest}{Another test-support module with a very |
| 86 | different flavor.} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 87 | \seetitle[http://www.XProgramming.com/testfram.htm]{Simple Smalltalk |
| 88 | Testing: With Patterns}{Kent Beck's original paper on |
| 89 | testing frameworks using the pattern shared by |
| 90 | \module{unittest}.} |
| 91 | \end{seealso} |
| 92 | |
| 93 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 94 | \subsection{Basic example \label{unittest-minimal-example}} |
Raymond Hettinger | 9de3c21 | 2003-07-10 22:14:41 +0000 | [diff] [blame] | 95 | |
| 96 | The \module{unittest} module provides a rich set of tools for |
| 97 | constructing and running tests. This section demonstrates that a |
| 98 | small subset of the tools suffice to meet the needs of most users. |
| 99 | |
| 100 | Here is a short script to test three functions from the |
| 101 | \refmodule{random} module: |
| 102 | |
| 103 | \begin{verbatim} |
| 104 | import random |
| 105 | import unittest |
| 106 | |
| 107 | class TestSequenceFunctions(unittest.TestCase): |
| 108 | |
| 109 | def setUp(self): |
| 110 | self.seq = range(10) |
| 111 | |
| 112 | def testshuffle(self): |
| 113 | # make sure the shuffled sequence does not lose any elements |
| 114 | random.shuffle(self.seq) |
| 115 | self.seq.sort() |
| 116 | self.assertEqual(self.seq, range(10)) |
| 117 | |
| 118 | def testchoice(self): |
| 119 | element = random.choice(self.seq) |
| 120 | self.assert_(element in self.seq) |
| 121 | |
| 122 | def testsample(self): |
| 123 | self.assertRaises(ValueError, random.sample, self.seq, 20) |
| 124 | for element in random.sample(self.seq, 5): |
| 125 | self.assert_(element in self.seq) |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | unittest.main() |
| 129 | \end{verbatim} |
| 130 | |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 131 | A testcase is created by subclassing \class{unittest.TestCase}. |
Raymond Hettinger | 9de3c21 | 2003-07-10 22:14:41 +0000 | [diff] [blame] | 132 | The three individual tests are defined with methods whose names start with |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 133 | the letters \samp{test}. This naming convention informs the test runner |
Raymond Hettinger | 9de3c21 | 2003-07-10 22:14:41 +0000 | [diff] [blame] | 134 | about which methods represent tests. |
| 135 | |
| 136 | The crux of each test is a call to \method{assertEqual()} to |
| 137 | check for an expected result; \method{assert_()} to verify a condition; |
| 138 | or \method{assertRaises()} to verify that an expected exception gets |
| 139 | raised. These methods are used instead of the \keyword{assert} statement |
| 140 | so the test runner can accumulate all test results and produce a report. |
| 141 | |
| 142 | When a \method{setUp()} method is defined, the test runner will run that |
| 143 | method prior to each test. Likewise, if a \method{tearDown()} method is |
| 144 | defined, the test runner will invoke that method after each test. In the |
| 145 | example, \method{setUp()} was used to create a fresh sequence for each test. |
| 146 | |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 147 | The final block shows a simple way to run the tests. |
| 148 | \function{unittest.main()} provides a command line interface to the |
| 149 | test script. When run from the command line, the above script |
| 150 | produces an output that looks like this: |
Raymond Hettinger | 9de3c21 | 2003-07-10 22:14:41 +0000 | [diff] [blame] | 151 | |
| 152 | \begin{verbatim} |
| 153 | ... |
| 154 | ---------------------------------------------------------------------- |
| 155 | Ran 3 tests in 0.000s |
| 156 | |
| 157 | OK |
| 158 | \end{verbatim} |
| 159 | |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 160 | Instead of \function{unittest.main()}, there are other ways to run the tests |
Raymond Hettinger | 9de3c21 | 2003-07-10 22:14:41 +0000 | [diff] [blame] | 161 | with a finer level of control, less terse output, and no requirement to be |
| 162 | run from the command line. For example, the last two lines may be replaced |
| 163 | with: |
| 164 | |
| 165 | \begin{verbatim} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 166 | suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions) |
Raymond Hettinger | 9de3c21 | 2003-07-10 22:14:41 +0000 | [diff] [blame] | 167 | unittest.TextTestRunner(verbosity=2).run(suite) |
| 168 | \end{verbatim} |
| 169 | |
| 170 | Running the revised script from the interpreter or another script |
| 171 | produces the following output: |
| 172 | |
| 173 | \begin{verbatim} |
| 174 | testchoice (__main__.TestSequenceFunctions) ... ok |
| 175 | testsample (__main__.TestSequenceFunctions) ... ok |
| 176 | testshuffle (__main__.TestSequenceFunctions) ... ok |
| 177 | |
| 178 | ---------------------------------------------------------------------- |
| 179 | Ran 3 tests in 0.110s |
| 180 | |
| 181 | OK |
| 182 | \end{verbatim} |
| 183 | |
| 184 | The above examples show the most commonly used \module{unittest} features |
| 185 | which are sufficient to meet many everyday testing needs. The remainder |
| 186 | of the documentation explores the full feature set from first principles. |
| 187 | |
| 188 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 189 | \subsection{Organizing test code |
| 190 | \label{organizing-tests}} |
| 191 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 192 | The basic building blocks of unit testing are \dfn{test cases} --- |
| 193 | single scenarios that must be set up and checked for correctness. In |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 194 | \module{unittest}, test cases are represented by instances of |
| 195 | \module{unittest}'s \class{TestCase} class. To make |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 196 | your own test cases you must write subclasses of \class{TestCase}, or |
| 197 | use \class{FunctionTestCase}. |
| 198 | |
| 199 | An instance of a \class{TestCase}-derived class is an object that can |
| 200 | completely run a single test method, together with optional set-up |
| 201 | and tidy-up code. |
| 202 | |
| 203 | The testing code of a \class{TestCase} instance should be entirely |
| 204 | self contained, such that it can be run either in isolation or in |
| 205 | arbitrary combination with any number of other test cases. |
| 206 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 207 | The simplest \class{TestCase} subclass will simply override the |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 208 | \method{runTest()} method in order to perform specific testing code: |
| 209 | |
| 210 | \begin{verbatim} |
| 211 | import unittest |
| 212 | |
| 213 | class DefaultWidgetSizeTestCase(unittest.TestCase): |
| 214 | def runTest(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 215 | widget = Widget('The widget') |
| 216 | self.assertEqual(widget.size(), (50, 50), 'incorrect default size') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 217 | \end{verbatim} |
| 218 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 219 | Note that in order to test something, we use the one of the |
| 220 | \method{assert*()} or \method{fail*()} methods provided by the |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 221 | \class{TestCase} base class. If the test fails, an exception will be |
| 222 | raised, and \module{unittest} will identify the test case as a |
| 223 | \dfn{failure}. Any other exceptions will be treated as \dfn{errors}. |
| 224 | This helps you identify where the problem is: \dfn{failures} are caused by |
| 225 | incorrect results - a 5 where you expected a 6. \dfn{Errors} are caused by |
| 226 | incorrect code - e.g., a \exception{TypeError} caused by an incorrect |
| 227 | function call. |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 228 | |
| 229 | The way to run a test case will be described later. For now, note |
| 230 | that to construct an instance of such a test case, we call its |
| 231 | constructor without arguments: |
| 232 | |
| 233 | \begin{verbatim} |
| 234 | testCase = DefaultWidgetSizeTestCase() |
| 235 | \end{verbatim} |
| 236 | |
| 237 | Now, such test cases can be numerous, and their set-up can be |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 238 | repetitive. In the above case, constructing a \class{Widget} in each of |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 239 | 100 Widget test case subclasses would mean unsightly duplication. |
| 240 | |
| 241 | Luckily, we can factor out such set-up code by implementing a method |
| 242 | called \method{setUp()}, which the testing framework will |
| 243 | automatically call for us when we run the test: |
| 244 | |
| 245 | \begin{verbatim} |
| 246 | import unittest |
| 247 | |
| 248 | class SimpleWidgetTestCase(unittest.TestCase): |
| 249 | def setUp(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 250 | self.widget = Widget('The widget') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 251 | |
| 252 | class DefaultWidgetSizeTestCase(SimpleWidgetTestCase): |
| 253 | def runTest(self): |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 254 | self.failUnless(self.widget.size() == (50,50), |
| 255 | 'incorrect default size') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 256 | |
| 257 | class WidgetResizeTestCase(SimpleWidgetTestCase): |
| 258 | def runTest(self): |
| 259 | self.widget.resize(100,150) |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 260 | self.failUnless(self.widget.size() == (100,150), |
| 261 | 'wrong size after resize') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 262 | \end{verbatim} |
| 263 | |
| 264 | If the \method{setUp()} method raises an exception while the test is |
| 265 | running, the framework will consider the test to have suffered an |
| 266 | error, and the \method{runTest()} method will not be executed. |
| 267 | |
| 268 | Similarly, we can provide a \method{tearDown()} method that tidies up |
| 269 | after the \method{runTest()} method has been run: |
| 270 | |
| 271 | \begin{verbatim} |
| 272 | import unittest |
| 273 | |
| 274 | class SimpleWidgetTestCase(unittest.TestCase): |
| 275 | def setUp(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 276 | self.widget = Widget('The widget') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 277 | |
| 278 | def tearDown(self): |
| 279 | self.widget.dispose() |
| 280 | self.widget = None |
| 281 | \end{verbatim} |
| 282 | |
| 283 | If \method{setUp()} succeeded, the \method{tearDown()} method will be |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 284 | run whether \method{runTest()} succeeded or not. |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 285 | |
| 286 | Such a working environment for the testing code is called a |
| 287 | \dfn{fixture}. |
| 288 | |
| 289 | Often, many small test cases will use the same fixture. In this case, |
| 290 | we would end up subclassing \class{SimpleWidgetTestCase} into many |
| 291 | small one-method classes such as |
| 292 | \class{DefaultWidgetSizeTestCase}. This is time-consuming and |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 293 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 294 | discouraging, so in the same vein as JUnit, \module{unittest} provides |
| 295 | a simpler mechanism: |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 296 | |
| 297 | \begin{verbatim} |
| 298 | import unittest |
| 299 | |
| 300 | class WidgetTestCase(unittest.TestCase): |
| 301 | def setUp(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 302 | self.widget = Widget('The widget') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 303 | |
| 304 | def tearDown(self): |
| 305 | self.widget.dispose() |
| 306 | self.widget = None |
| 307 | |
| 308 | def testDefaultSize(self): |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 309 | self.failUnless(self.widget.size() == (50,50), |
| 310 | 'incorrect default size') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 311 | |
| 312 | def testResize(self): |
| 313 | self.widget.resize(100,150) |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 314 | self.failUnless(self.widget.size() == (100,150), |
| 315 | 'wrong size after resize') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 316 | \end{verbatim} |
| 317 | |
| 318 | Here we have not provided a \method{runTest()} method, but have |
| 319 | instead provided two different test methods. Class instances will now |
| 320 | each run one of the \method{test*()} methods, with \code{self.widget} |
| 321 | created and destroyed separately for each instance. When creating an |
| 322 | instance we must specify the test method it is to run. We do this by |
| 323 | passing the method name in the constructor: |
| 324 | |
| 325 | \begin{verbatim} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 326 | defaultSizeTestCase = WidgetTestCase('testDefaultSize') |
| 327 | resizeTestCase = WidgetTestCase('testResize') |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 328 | \end{verbatim} |
| 329 | |
| 330 | Test case instances are grouped together according to the features |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 331 | they test. \module{unittest} provides a mechanism for this: the |
| 332 | \dfn{test suite}, represented by \module{unittest}'s \class{TestSuite} |
| 333 | class: |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 334 | |
| 335 | \begin{verbatim} |
| 336 | widgetTestSuite = unittest.TestSuite() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 337 | widgetTestSuite.addTest(WidgetTestCase('testDefaultSize')) |
| 338 | widgetTestSuite.addTest(WidgetTestCase('testResize')) |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 339 | \end{verbatim} |
| 340 | |
| 341 | For the ease of running tests, as we will see later, it is a good |
| 342 | idea to provide in each test module a callable object that returns a |
| 343 | pre-built test suite: |
| 344 | |
| 345 | \begin{verbatim} |
| 346 | def suite(): |
| 347 | suite = unittest.TestSuite() |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 348 | suite.addTest(WidgetTestCase('testDefaultSize')) |
| 349 | suite.addTest(WidgetTestCase('testResize')) |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 350 | return suite |
| 351 | \end{verbatim} |
| 352 | |
| 353 | or even: |
| 354 | |
| 355 | \begin{verbatim} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 356 | def suite(): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 357 | tests = ['testDefaultSize', 'testResize'] |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 358 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 359 | return unittest.TestSuite(map(WidgetTestCase, tests)) |
| 360 | \end{verbatim} |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 361 | |
| 362 | Since it is a common pattern to create a \class{TestCase} subclass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 363 | with many similarly named test functions, \module{unittest} provides a |
| 364 | \class{TestLoader} class that can be used to automate the process of |
| 365 | creating a test suite and populating it with individual tests. |
| 366 | For example, |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 367 | |
| 368 | \begin{verbatim} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 369 | suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase) |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 370 | \end{verbatim} |
| 371 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 372 | will create a test suite that will run |
| 373 | \code{WidgetTestCase.testDefaultSize()} and \code{WidgetTestCase.testResize}. |
| 374 | \class{TestLoader} uses the \code{'test'} method name prefix to identify |
| 375 | test methods automatically. |
| 376 | |
| 377 | Note that the order in which the various test cases will be run is |
| 378 | determined by sorting the test function names with the built-in |
| 379 | \function{cmp()} function. |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 380 | |
| 381 | Often it is desirable to group suites of test cases together, so as to |
| 382 | run tests for the whole system at once. This is easy, since |
| 383 | \class{TestSuite} instances can be added to a \class{TestSuite} just |
| 384 | as \class{TestCase} instances can be added to a \class{TestSuite}: |
| 385 | |
| 386 | \begin{verbatim} |
| 387 | suite1 = module1.TheTestSuite() |
| 388 | suite2 = module2.TheTestSuite() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 389 | alltests = unittest.TestSuite([suite1, suite2]) |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 390 | \end{verbatim} |
| 391 | |
| 392 | You can place the definitions of test cases and test suites in the |
Raymond Hettinger | d21fd7b | 2003-09-16 22:04:31 +0000 | [diff] [blame] | 393 | same modules as the code they are to test (such as \file{widget.py}), |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 394 | but there are several advantages to placing the test code in a |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 395 | separate module, such as \file{test_widget.py}: |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 396 | |
| 397 | \begin{itemize} |
| 398 | \item The test module can be run standalone from the command line. |
| 399 | \item The test code can more easily be separated from shipped code. |
Michael W. Hudson | aab0260 | 2003-02-10 19:21:16 +0000 | [diff] [blame] | 400 | \item There is less temptation to change test code to fit the code |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 401 | it tests without a good reason. |
| 402 | \item Test code should be modified much less frequently than the |
| 403 | code it tests. |
| 404 | \item Tested code can be refactored more easily. |
| 405 | \item Tests for modules written in C must be in separate modules |
| 406 | anyway, so why not be consistent? |
| 407 | \item If the testing strategy changes, there is no need to change |
| 408 | the source code. |
| 409 | \end{itemize} |
| 410 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 411 | |
| 412 | \subsection{Re-using old test code |
| 413 | \label{legacy-unit-tests}} |
| 414 | |
| 415 | Some users will find that they have existing test code that they would |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 416 | like to run from \module{unittest}, without converting every old test |
| 417 | function to a \class{TestCase} subclass. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 418 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 419 | For this reason, \module{unittest} provides a \class{FunctionTestCase} |
| 420 | class. This subclass of \class{TestCase} can be used to wrap an existing |
| 421 | test function. Set-up and tear-down functions can also be provided. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 422 | |
| 423 | Given the following test function: |
| 424 | |
| 425 | \begin{verbatim} |
| 426 | def testSomething(): |
| 427 | something = makeSomething() |
| 428 | assert something.name is not None |
| 429 | # ... |
| 430 | \end{verbatim} |
| 431 | |
| 432 | one can create an equivalent test case instance as follows: |
| 433 | |
| 434 | \begin{verbatim} |
| 435 | testcase = unittest.FunctionTestCase(testSomething) |
| 436 | \end{verbatim} |
| 437 | |
| 438 | If there are additional set-up and tear-down methods that should be |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 439 | called as part of the test case's operation, they can also be provided |
| 440 | like so: |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 441 | |
| 442 | \begin{verbatim} |
| 443 | testcase = unittest.FunctionTestCase(testSomething, |
| 444 | setUp=makeSomethingDB, |
| 445 | tearDown=deleteSomethingDB) |
| 446 | \end{verbatim} |
| 447 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 448 | To make migrating existing test suites easier, \module{unittest} |
| 449 | supports tests raising \exception{AssertionError} to indicate test failure. |
| 450 | However, it is recommended that you use the explicit |
| 451 | \method{TestCase.fail*()} and \method{TestCase.assert*()} methods instead, |
| 452 | as future versions of \module{unittest} may treat \exception{AssertionError} |
| 453 | differently. |
| 454 | |
| 455 | \note{Even though \class{FunctionTestCase} can be used to quickly convert |
| 456 | an existing test base over to a \module{unittest}-based system, this |
| 457 | approach is not recommended. Taking the time to set up proper |
| 458 | \class{TestCase} subclasses will make future test refactorings infinitely |
| 459 | easier.} |
| 460 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 461 | |
| 462 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 463 | \subsection{Classes and functions |
| 464 | \label{unittest-contents}} |
| 465 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 466 | \begin{classdesc}{TestCase}{\optional{methodName}} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 467 | Instances of the \class{TestCase} class represent the smallest |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 468 | testable units in the \module{unittest} universe. This class is |
| 469 | intended to be used as a base class, with specific tests being |
| 470 | implemented by concrete subclasses. This class implements the |
| 471 | interface needed by the test runner to allow it to drive the |
| 472 | test, and methods that the test code can use to check for and |
| 473 | report various kinds of failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 474 | |
| 475 | Each instance of \class{TestCase} will run a single test method: |
| 476 | the method named \var{methodName}. If you remember, we had an |
| 477 | earlier example that went something like this: |
| 478 | |
| 479 | \begin{verbatim} |
| 480 | def suite(): |
| 481 | suite = unittest.TestSuite() |
| 482 | suite.addTest(WidgetTestCase('testDefaultSize')) |
| 483 | suite.addTest(WidgetTestCase('testResize')) |
| 484 | return suite |
| 485 | \end{verbatim} |
| 486 | |
| 487 | Here, we create two instances of \class{WidgetTestCase}, each of |
| 488 | which runs a single test. |
| 489 | |
| 490 | \var{methodName} defaults to \code{'runTest'}. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 491 | \end{classdesc} |
| 492 | |
| 493 | \begin{classdesc}{FunctionTestCase}{testFunc\optional{, |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 494 | setUp\optional{, tearDown\optional{, description}}}} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 495 | This class implements the portion of the \class{TestCase} interface |
| 496 | which allows the test runner to drive the test, but does not provide |
| 497 | the methods which test code can use to check and report errors. |
| 498 | This is used to create test cases using legacy test code, allowing |
| 499 | it to be integrated into a \refmodule{unittest}-based test |
| 500 | framework. |
| 501 | \end{classdesc} |
| 502 | |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 503 | \begin{classdesc}{TestSuite}{\optional{tests}} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 504 | This class represents an aggregation of individual tests cases and |
| 505 | test suites. The class presents the interface needed by the test |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 506 | runner to allow it to be run as any other test case. Running a |
| 507 | \class{TestSuite} instance is the same as iterating over the suite, |
| 508 | running each test individually. |
| 509 | |
| 510 | If \var{tests} is given, it must be an iterable of individual test cases or |
| 511 | other test suites that will be used to build the suite initially. |
| 512 | Additional methods are provided to add test cases and suites to the |
| 513 | collection later on. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 514 | \end{classdesc} |
| 515 | |
| 516 | \begin{classdesc}{TestLoader}{} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 517 | This class is responsible for loading tests according to various |
| 518 | criteria and returning them wrapped in a \class{TestSuite}. |
| 519 | It can load all tests within a given module or \class{TestCase} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 520 | subclass. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 521 | \end{classdesc} |
| 522 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 523 | \begin{classdesc}{TestResult}{} |
| 524 | This class is used to compile information about which tests have succeeded |
| 525 | and which have failed. |
| 526 | \end{classdesc} |
| 527 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 528 | \begin{datadesc}{defaultTestLoader} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 529 | Instance of the \class{TestLoader} class intended to be shared. If no |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 530 | customization of the \class{TestLoader} is needed, this instance can |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 531 | be used instead of repeatedly creating new instances. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 532 | \end{datadesc} |
| 533 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 534 | \begin{classdesc}{TextTestRunner}{\optional{stream\optional{, |
| 535 | descriptions\optional{, verbosity}}}} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 536 | A basic test runner implementation which prints results on standard |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 537 | error. It has a few configurable parameters, but is essentially |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 538 | very simple. Graphical applications which run test suites should |
| 539 | provide alternate implementations. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 540 | \end{classdesc} |
| 541 | |
| 542 | \begin{funcdesc}{main}{\optional{module\optional{, |
| 543 | defaultTest\optional{, argv\optional{, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 544 | testRunner\optional{, testLoader}}}}}} |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 545 | A command-line program that runs a set of tests; this is primarily |
| 546 | for making test modules conveniently executable. The simplest use |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 547 | for this function is to include the following line at the end of a |
| 548 | test script: |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 549 | |
| 550 | \begin{verbatim} |
| 551 | if __name__ == '__main__': |
| 552 | unittest.main() |
| 553 | \end{verbatim} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 554 | |
| 555 | The \var{testRunner} argument can either be a test runner class or |
| 556 | an already created instance of it. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 557 | \end{funcdesc} |
| 558 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 559 | In some cases, the existing tests may have been written using the |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 560 | \refmodule{doctest} module. If so, that module provides a |
Raymond Hettinger | d21fd7b | 2003-09-16 22:04:31 +0000 | [diff] [blame] | 561 | \class{DocTestSuite} class that can automatically build |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 562 | \class{unittest.TestSuite} instances from the existing |
| 563 | \module{doctest}-based tests. |
Raymond Hettinger | d21fd7b | 2003-09-16 22:04:31 +0000 | [diff] [blame] | 564 | \versionadded{2.3} |
| 565 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 566 | |
| 567 | \subsection{TestCase Objects |
| 568 | \label{testcase-objects}} |
| 569 | |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 570 | Each \class{TestCase} instance represents a single test, but each |
| 571 | concrete subclass may be used to define multiple tests --- the |
| 572 | concrete class represents a single test fixture. The fixture is |
| 573 | created and cleaned up for each test case. |
| 574 | |
| 575 | \class{TestCase} instances provide three groups of methods: one group |
| 576 | used to run the test, another used by the test implementation to |
| 577 | check conditions and report failures, and some inquiry methods |
| 578 | allowing information about the test itself to be gathered. |
| 579 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 580 | Methods in the first group (running the test) are: |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 581 | |
| 582 | \begin{methoddesc}[TestCase]{setUp}{} |
| 583 | Method called to prepare the test fixture. This is called |
| 584 | immediately before calling the test method; any exception raised by |
| 585 | this method will be considered an error rather than a test failure. |
| 586 | The default implementation does nothing. |
| 587 | \end{methoddesc} |
| 588 | |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 589 | \begin{methoddesc}[TestCase]{tearDown}{} |
| 590 | Method called immediately after the test method has been called and |
| 591 | the result recorded. This is called even if the test method raised |
| 592 | an exception, so the implementation in subclasses may need to be |
| 593 | particularly careful about checking internal state. Any exception |
| 594 | raised by this method will be considered an error rather than a test |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 595 | failure. This method will only be called if the \method{setUp()} |
| 596 | succeeds, regardless of the outcome of the test method. |
| 597 | The default implementation does nothing. |
| 598 | \end{methoddesc} |
| 599 | |
| 600 | \begin{methoddesc}[TestCase]{run}{\optional{result}} |
| 601 | Run the test, collecting the result into the test result object |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 602 | passed as \var{result}. If \var{result} is omitted or \constant{None}, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 603 | a temporary result object is created (by calling the |
| 604 | \method{defaultTestCase()} method) and used; this result object is not |
| 605 | returned to \method{run()}'s caller. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 606 | |
| 607 | The same effect may be had by simply calling the \class{TestCase} |
| 608 | instance. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 609 | \end{methoddesc} |
| 610 | |
| 611 | \begin{methoddesc}[TestCase]{debug}{} |
| 612 | Run the test without collecting the result. This allows exceptions |
Raymond Hettinger | 6880431 | 2005-01-01 00:28:46 +0000 | [diff] [blame] | 613 | raised by the test to be propagated to the caller, and can be used |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 614 | to support running tests under a debugger. |
| 615 | \end{methoddesc} |
| 616 | |
| 617 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 618 | The test code can use any of the following methods to check for and |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 619 | report failures. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 620 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 621 | \begin{methoddesc}[TestCase]{assert_}{expr\optional{, msg}} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 622 | \methodline[TestCase]{failUnless}{expr\optional{, msg}} |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 623 | Signal a test failure if \var{expr} is false; the explanation for |
| 624 | the error will be \var{msg} if given, otherwise it will be |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 625 | \constant{None}. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 626 | \end{methoddesc} |
| 627 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 628 | \begin{methoddesc}[TestCase]{assertEqual}{first, second\optional{, msg}} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 629 | \methodline[TestCase]{failUnlessEqual}{first, second\optional{, msg}} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 630 | Test that \var{first} and \var{second} are equal. If the values do |
| 631 | not compare equal, the test will fail with the explanation given by |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 632 | \var{msg}, or \constant{None}. Note that using \method{failUnlessEqual()} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 633 | improves upon doing the comparison as the first parameter to |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 634 | \method{failUnless()}: the default value for \var{msg} can be |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 635 | computed to include representations of both \var{first} and |
| 636 | \var{second}. |
| 637 | \end{methoddesc} |
| 638 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 639 | \begin{methoddesc}[TestCase]{assertNotEqual}{first, second\optional{, msg}} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 640 | \methodline[TestCase]{failIfEqual}{first, second\optional{, msg}} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 641 | Test that \var{first} and \var{second} are not equal. If the values |
| 642 | do compare equal, the test will fail with the explanation given by |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 643 | \var{msg}, or \constant{None}. Note that using \method{failIfEqual()} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 644 | improves upon doing the comparison as the first parameter to |
| 645 | \method{failUnless()} is that the default value for \var{msg} can be |
| 646 | computed to include representations of both \var{first} and |
| 647 | \var{second}. |
| 648 | \end{methoddesc} |
| 649 | |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 650 | \begin{methoddesc}[TestCase]{assertAlmostEqual}{first, second\optional{, |
| 651 | places\optional{, msg}}} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 652 | \methodline[TestCase]{failUnlessAlmostEqual}{first, second\optional{, |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 653 | places\optional{, msg}}} |
| 654 | Test that \var{first} and \var{second} are approximately equal |
| 655 | by computing the difference, rounding to the given number of \var{places}, |
| 656 | and comparing to zero. Note that comparing a given number of decimal places |
| 657 | is not the same as comparing a given number of significant digits. |
| 658 | If the values do not compare equal, the test will fail with the explanation |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 659 | given by \var{msg}, or \constant{None}. |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 660 | \end{methoddesc} |
| 661 | |
| 662 | \begin{methoddesc}[TestCase]{assertNotAlmostEqual}{first, second\optional{, |
| 663 | places\optional{, msg}}} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 664 | \methodline[TestCase]{failIfAlmostEqual}{first, second\optional{, |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 665 | places\optional{, msg}}} |
| 666 | Test that \var{first} and \var{second} are not approximately equal |
| 667 | by computing the difference, rounding to the given number of \var{places}, |
| 668 | and comparing to zero. Note that comparing a given number of decimal places |
| 669 | is not the same as comparing a given number of significant digits. |
| 670 | If the values do not compare equal, the test will fail with the explanation |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 671 | given by \var{msg}, or \constant{None}. |
Raymond Hettinger | c7b0769 | 2002-12-29 17:59:24 +0000 | [diff] [blame] | 672 | \end{methoddesc} |
| 673 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 674 | \begin{methoddesc}[TestCase]{assertRaises}{exception, callable, \moreargs} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame^] | 675 | \methodline[TestCase]{failUnlessRaises}{exception, callable, \moreargs} |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 676 | Test that an exception is raised when \var{callable} is called with |
| 677 | any positional or keyword arguments that are also passed to |
| 678 | \method{assertRaises()}. The test passes if \var{exception} is |
| 679 | raised, is an error if another exception is raised, or fails if no |
| 680 | exception is raised. To catch any of a group of exceptions, a tuple |
| 681 | containing the exception classes may be passed as \var{exception}. |
| 682 | \end{methoddesc} |
| 683 | |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 684 | \begin{methoddesc}[TestCase]{failIf}{expr\optional{, msg}} |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 685 | The inverse of the \method{failUnless()} method is the |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 686 | \method{failIf()} method. This signals a test failure if \var{expr} |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 687 | is true, with \var{msg} or \constant{None} for the error message. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 688 | \end{methoddesc} |
| 689 | |
| 690 | \begin{methoddesc}[TestCase]{fail}{\optional{msg}} |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 691 | Signals a test failure unconditionally, with \var{msg} or |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 692 | \constant{None} for the error message. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 693 | \end{methoddesc} |
| 694 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 695 | \begin{memberdesc}[TestCase]{failureException} |
| 696 | This class attribute gives the exception raised by the |
| 697 | \method{test()} method. If a test framework needs to use a |
| 698 | specialized exception, possibly to carry additional information, it |
| 699 | must subclass this exception in order to ``play fair'' with the |
| 700 | framework. The initial value of this attribute is |
| 701 | \exception{AssertionError}. |
| 702 | \end{memberdesc} |
| 703 | |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 704 | |
| 705 | Testing frameworks can use the following methods to collect |
| 706 | information on the test: |
| 707 | |
| 708 | \begin{methoddesc}[TestCase]{countTestCases}{} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 709 | Return the number of tests represented by this test object. For |
| 710 | \class{TestCase} instances, this will always be \code{1}. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 711 | \end{methoddesc} |
| 712 | |
| 713 | \begin{methoddesc}[TestCase]{defaultTestResult}{} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 714 | Return an instance of the test result class that should be used |
| 715 | for this test case class (if no other result instance is provided |
| 716 | to the \method{run()} method). |
| 717 | |
| 718 | For \class{TestCase} instances, this will always be an instance of |
| 719 | \class{TestResult}; subclasses of \class{TestCase} should |
| 720 | override this as necessary. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 721 | \end{methoddesc} |
| 722 | |
| 723 | \begin{methoddesc}[TestCase]{id}{} |
| 724 | Return a string identifying the specific test case. This is usually |
| 725 | the full name of the test method, including the module and class |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 726 | name. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 727 | \end{methoddesc} |
| 728 | |
| 729 | \begin{methoddesc}[TestCase]{shortDescription}{} |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 730 | Returns a one-line description of the test, or \constant{None} if no |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 731 | description has been provided. The default implementation of this |
| 732 | method returns the first line of the test method's docstring, if |
Fred Drake | ae55d5f | 2003-12-31 04:34:50 +0000 | [diff] [blame] | 733 | available, or \constant{None}. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 734 | \end{methoddesc} |
| 735 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 736 | |
| 737 | \subsection{TestSuite Objects |
| 738 | \label{testsuite-objects}} |
| 739 | |
| 740 | \class{TestSuite} objects behave much like \class{TestCase} objects, |
| 741 | except they do not actually implement a test. Instead, they are used |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 742 | to aggregate tests into groups of tests that should be run together. |
| 743 | Some additional methods are available to add tests to \class{TestSuite} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 744 | instances: |
| 745 | |
| 746 | \begin{methoddesc}[TestSuite]{addTest}{test} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 747 | Add a \class{TestCase} or \class{TestSuite} to the suite. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 748 | \end{methoddesc} |
| 749 | |
| 750 | \begin{methoddesc}[TestSuite]{addTests}{tests} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 751 | Add all the tests from an iterable of \class{TestCase} and |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 752 | \class{TestSuite} instances to this test suite. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 753 | |
| 754 | This is equivalent to iterating over \var{tests}, calling |
| 755 | \method{addTest()} for each element. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 756 | \end{methoddesc} |
| 757 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 758 | \class{TestSuite} shares the following methods with \class{TestCase}: |
Fred Drake | 4d17b30 | 2001-09-06 15:51:56 +0000 | [diff] [blame] | 759 | |
| 760 | \begin{methoddesc}[TestSuite]{run}{result} |
| 761 | Run the tests associated with this suite, collecting the result into |
| 762 | the test result object passed as \var{result}. Note that unlike |
| 763 | \method{TestCase.run()}, \method{TestSuite.run()} requires the |
| 764 | result object to be passed in. |
| 765 | \end{methoddesc} |
| 766 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 767 | \begin{methoddesc}[TestSuite]{debug}{} |
| 768 | Run the tests associated with this suite without collecting the result. |
| 769 | This allows exceptions raised by the test to be propagated to the caller |
| 770 | and can be used to support running tests under a debugger. |
| 771 | \end{methoddesc} |
| 772 | |
| 773 | \begin{methoddesc}[TestSuite]{countTestCases}{} |
| 774 | Return the number of tests represented by this test object, including |
| 775 | all individual tests and sub-suites. |
| 776 | \end{methoddesc} |
| 777 | |
Fred Drake | 4d17b30 | 2001-09-06 15:51:56 +0000 | [diff] [blame] | 778 | In the typical usage of a \class{TestSuite} object, the \method{run()} |
| 779 | method is invoked by a \class{TestRunner} rather than by the end-user |
| 780 | test harness. |
| 781 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 782 | |
| 783 | \subsection{TestResult Objects |
| 784 | \label{testresult-objects}} |
| 785 | |
| 786 | A \class{TestResult} object stores the results of a set of tests. The |
| 787 | \class{TestCase} and \class{TestSuite} classes ensure that results are |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 788 | properly recorded; test authors do not need to worry about recording the |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 789 | outcome of tests. |
| 790 | |
| 791 | Testing frameworks built on top of \refmodule{unittest} may want |
| 792 | access to the \class{TestResult} object generated by running a set of |
| 793 | tests for reporting purposes; a \class{TestResult} instance is |
| 794 | returned by the \method{TestRunner.run()} method for this purpose. |
| 795 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 796 | \class{TestResult} instances have the following attributes that will |
| 797 | be of interest when inspecting the results of running a set of tests: |
| 798 | |
| 799 | \begin{memberdesc}[TestResult]{errors} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 800 | A list containing 2-tuples of \class{TestCase} instances and |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 801 | strings holding formatted tracebacks. Each tuple represents a test which |
| 802 | raised an unexpected exception. |
Fred Drake | c412617 | 2002-07-02 22:46:42 +0000 | [diff] [blame] | 803 | \versionchanged[Contains formatted tracebacks instead of |
| 804 | \function{sys.exc_info()} results]{2.2} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 805 | \end{memberdesc} |
| 806 | |
| 807 | \begin{memberdesc}[TestResult]{failures} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 808 | A list containing 2-tuples of \class{TestCase} instances and strings |
| 809 | holding formatted tracebacks. Each tuple represents a test where a failure |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 810 | was explicitly signalled using the \method{TestCase.fail*()} or |
| 811 | \method{TestCase.assert*()} methods. |
Fred Drake | c412617 | 2002-07-02 22:46:42 +0000 | [diff] [blame] | 812 | \versionchanged[Contains formatted tracebacks instead of |
| 813 | \function{sys.exc_info()} results]{2.2} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 814 | \end{memberdesc} |
| 815 | |
| 816 | \begin{memberdesc}[TestResult]{testsRun} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 817 | The total number of tests run so far. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 818 | \end{memberdesc} |
| 819 | |
| 820 | \begin{methoddesc}[TestResult]{wasSuccessful}{} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 821 | Returns \constant{True} if all tests run so far have passed, |
| 822 | otherwise returns \constant{False}. |
| 823 | \end{methoddesc} |
| 824 | |
| 825 | \begin{methoddesc}[TestResult]{stop}{} |
| 826 | This method can be called to signal that the set of tests being run |
| 827 | should be aborted by setting the \class{TestResult}'s \code{shouldStop} |
| 828 | attribute to \constant{True}. \class{TestRunner} objects should respect |
| 829 | this flag and return without running any additional tests. |
| 830 | |
| 831 | For example, this feature is used by the \class{TextTestRunner} class |
| 832 | to stop the test framework when the user signals an interrupt from |
| 833 | the keyboard. Interactive tools which provide \class{TestRunner} |
| 834 | implementations can use this in a similar manner. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 835 | \end{methoddesc} |
| 836 | |
| 837 | |
| 838 | The following methods of the \class{TestResult} class are used to |
Martin v. Löwis | 7bdc484 | 2003-09-20 11:09:28 +0000 | [diff] [blame] | 839 | maintain the internal data structures, and may be extended in |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 840 | subclasses to support additional reporting requirements. This is |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 841 | particularly useful in building tools which support interactive |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 842 | reporting while tests are being run. |
| 843 | |
| 844 | \begin{methoddesc}[TestResult]{startTest}{test} |
| 845 | Called when the test case \var{test} is about to be run. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 846 | |
| 847 | The default implementation simply increments the instance's |
| 848 | \code{testsRun} counter. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 849 | \end{methoddesc} |
| 850 | |
| 851 | \begin{methoddesc}[TestResult]{stopTest}{test} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 852 | Called after the test case \var{test} has been executed, regardless |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 853 | of the outcome. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 854 | |
| 855 | The default implementation does nothing. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 856 | \end{methoddesc} |
| 857 | |
| 858 | \begin{methoddesc}[TestResult]{addError}{test, err} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 859 | Called when the test case \var{test} raises an unexpected exception |
| 860 | \var{err} is a tuple of the form returned by \function{sys.exc_info()}: |
| 861 | \code{(\var{type}, \var{value}, \var{traceback})}. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 862 | |
| 863 | The default implementation appends \code{(\var{test}, \var{err})} to |
| 864 | the instance's \code{errors} attribute. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 865 | \end{methoddesc} |
| 866 | |
| 867 | \begin{methoddesc}[TestResult]{addFailure}{test, err} |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 868 | Called when the test case \var{test} signals a failure. |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame] | 869 | \var{err} is a tuple of the form returned by |
| 870 | \function{sys.exc_info()}: \code{(\var{type}, \var{value}, |
| 871 | \var{traceback})}. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 872 | |
| 873 | The default implementation appends \code{(\var{test}, \var{err})} to |
| 874 | the instance's \code{failures} attribute. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 875 | \end{methoddesc} |
| 876 | |
| 877 | \begin{methoddesc}[TestResult]{addSuccess}{test} |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 878 | Called when the test case \var{test} succeeds. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 879 | |
| 880 | The default implementation does nothing. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 881 | \end{methoddesc} |
| 882 | |
| 883 | |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 884 | |
| 885 | \subsection{TestLoader Objects |
| 886 | \label{testloader-objects}} |
| 887 | |
| 888 | The \class{TestLoader} class is used to create test suites from |
| 889 | classes and modules. Normally, there is no need to create an instance |
| 890 | of this class; the \refmodule{unittest} module provides an instance |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 891 | that can be shared as \code{unittest.defaultTestLoader}. |
| 892 | Using a subclass or instance, however, allows customization of some |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 893 | configurable properties. |
| 894 | |
| 895 | \class{TestLoader} objects have the following methods: |
| 896 | |
| 897 | \begin{methoddesc}[TestLoader]{loadTestsFromTestCase}{testCaseClass} |
| 898 | Return a suite of all tests cases contained in the |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 899 | \class{TestCase}-derived \class{testCaseClass}. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 900 | \end{methoddesc} |
| 901 | |
| 902 | \begin{methoddesc}[TestLoader]{loadTestsFromModule}{module} |
| 903 | Return a suite of all tests cases contained in the given module. |
| 904 | This method searches \var{module} for classes derived from |
| 905 | \class{TestCase} and creates an instance of the class for each test |
| 906 | method defined for the class. |
| 907 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 908 | \warning{While using a hierarchy of |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 909 | \class{TestCase}-derived classes can be convenient in sharing |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 910 | fixtures and helper functions, defining test methods on base classes |
| 911 | that are not intended to be instantiated directly does not play well |
| 912 | with this method. Doing so, however, can be useful when the |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 913 | fixtures are different and defined in subclasses.} |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 914 | \end{methoddesc} |
| 915 | |
| 916 | \begin{methoddesc}[TestLoader]{loadTestsFromName}{name\optional{, module}} |
| 917 | Return a suite of all tests cases given a string specifier. |
| 918 | |
Fred Drake | 4d17b30 | 2001-09-06 15:51:56 +0000 | [diff] [blame] | 919 | The specifier \var{name} is a ``dotted name'' that may resolve |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 920 | either to a module, a test case class, a test method within a test |
| 921 | case class, a \class{TestSuite} instance, or a callable object which |
| 922 | returns a \class{TestCase} or \class{TestSuite} instance. These checks |
| 923 | are applied in the order listed here; that is, a method on a possible |
| 924 | test case class will be picked up as ``a test method within a test |
| 925 | case class'', rather than ``a callable object''. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 926 | |
| 927 | For example, if you have a module \module{SampleTests} containing a |
| 928 | \class{TestCase}-derived class \class{SampleTestCase} with three test |
| 929 | methods (\method{test_one()}, \method{test_two()}, and |
| 930 | \method{test_three()}), the specifier \code{'SampleTests.SampleTestCase'} |
| 931 | would cause this method to return a suite which will run all three test |
| 932 | methods. Using the specifier \code{'SampleTests.SampleTestCase.test_two'} |
| 933 | would cause it to return a test suite which will run only the |
Fred Drake | 4d17b30 | 2001-09-06 15:51:56 +0000 | [diff] [blame] | 934 | \method{test_two()} test method. The specifier can refer to modules |
| 935 | and packages which have not been imported; they will be imported as |
| 936 | a side-effect. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 937 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 938 | The method optionally resolves \var{name} relative to the given |
| 939 | \var{module}. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 940 | \end{methoddesc} |
| 941 | |
| 942 | \begin{methoddesc}[TestLoader]{loadTestsFromNames}{names\optional{, module}} |
| 943 | Similar to \method{loadTestsFromName()}, but takes a sequence of |
| 944 | names rather than a single name. The return value is a test suite |
| 945 | which supports all the tests defined for each name. |
| 946 | \end{methoddesc} |
| 947 | |
| 948 | \begin{methoddesc}[TestLoader]{getTestCaseNames}{testCaseClass} |
| 949 | Return a sorted sequence of method names found within |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 950 | \var{testCaseClass}; this should be a subclass of \class{TestCase}. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 951 | \end{methoddesc} |
| 952 | |
| 953 | |
| 954 | The following attributes of a \class{TestLoader} can be configured |
| 955 | either by subclassing or assignment on an instance: |
| 956 | |
| 957 | \begin{memberdesc}[TestLoader]{testMethodPrefix} |
| 958 | String giving the prefix of method names which will be interpreted |
| 959 | as test methods. The default value is \code{'test'}. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 960 | |
| 961 | This affects \method{getTestCaseNames()} and all the |
| 962 | \method{loadTestsFrom*()} methods. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 963 | \end{memberdesc} |
| 964 | |
| 965 | \begin{memberdesc}[TestLoader]{sortTestMethodsUsing} |
| 966 | Function to be used to compare method names when sorting them in |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 967 | \method{getTestCaseNames()} and all the \method{loadTestsFrom*()} methods. |
| 968 | The default value is the built-in \function{cmp()} function; the attribute |
| 969 | can also be set to \constant{None} to disable the sort. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 970 | \end{memberdesc} |
| 971 | |
| 972 | \begin{memberdesc}[TestLoader]{suiteClass} |
| 973 | Callable object that constructs a test suite from a list of tests. |
| 974 | No methods on the resulting object are needed. The default value is |
| 975 | the \class{TestSuite} class. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 976 | |
| 977 | This affects all the \method{loadTestsFrom*()} methods. |
Fred Drake | 62a2669 | 2001-04-12 19:34:38 +0000 | [diff] [blame] | 978 | \end{memberdesc} |