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} |
| 5 | \moduleauthor{Steve Purcell}{stephen\textunderscore{}purcell@yahoo.com} |
| 6 | \sectionauthor{Steve Purcell}{stephen\textunderscore{}purcell@yahoo.com} |
| 7 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 8 | |
| 9 | |
| 10 | The Python unit testing framework, often referred to as ``PyUnit,'' is |
| 11 | a Python language version of JUnit, by Kent Beck and Erich Gamma. |
| 12 | JUnit is, in turn, a Java version of Kent's Smalltalk testing |
| 13 | framework. Each is the de facto standard unit testing framework for |
| 14 | its respective language. |
| 15 | |
| 16 | PyUnit supports test automation, sharing of setup and shutdown code |
| 17 | for tests, aggregation of tests into collections, and independence of |
| 18 | the tests from the reporting framework. The \module{unittest} module |
| 19 | provides classes that make it easy to support these qualities for a |
| 20 | set of tests. |
| 21 | |
| 22 | To achieve this, PyUnit supports three major concepts: |
| 23 | |
| 24 | \begin{definitions} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 25 | \term{test fixture} |
| 26 | A \dfn{test fixture} represents the preparation needed to perform one |
| 27 | or more tests, and any associate cleanup actions. This may involve, |
| 28 | for example, creating temporary or proxy databases, directories, or |
| 29 | starting a server process. |
| 30 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 31 | \term{test case} |
| 32 | A \dfn{test case} is the smallest unit of testing. It checks for a |
| 33 | specific response to a particular set of inputs. PyUnit provides a |
| 34 | base class, \class{TestCase}, which may be used to create new test |
| 35 | cases. |
| 36 | |
| 37 | \term{test suite} |
| 38 | A \dfn{test suite} is a collection of test cases, test suites, or |
| 39 | both. It is used to aggregate tests that should be executed |
| 40 | together. |
| 41 | |
| 42 | \term{test runner} |
| 43 | A \dfn{test runner} is a component which orchestrates the execution of |
| 44 | tests and provides the outcome to the user. The runner may use a |
| 45 | graphical interface, a textual interface, or return a special value to |
| 46 | indicate the results of executing the tests. |
| 47 | \end{definitions} |
| 48 | |
| 49 | |
| 50 | |
| 51 | \begin{seealso} |
| 52 | \seetitle[http://pyunit.sourceforge.net/]{PyUnit Web Site}{The |
| 53 | source for further information on PyUnit.} |
| 54 | \seetitle[http://www.XProgramming.com/testfram.htm]{Simple Smalltalk |
| 55 | Testing: With Patterns}{Kent Beck's original paper on |
| 56 | testing frameworks using the pattern shared by |
| 57 | \module{unittest}.} |
| 58 | \end{seealso} |
| 59 | |
| 60 | |
| 61 | \subsection{Mapping concepts to classes |
| 62 | \label{test-concept-classes}} |
| 63 | |
| 64 | |
| 65 | \subsection{Organizing test code |
| 66 | \label{organizing-tests}} |
| 67 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 68 | The basic building blocks of unit testing are \dfn{test cases} --- |
| 69 | single scenarios that must be set up and checked for correctness. In |
| 70 | PyUnit, test cases are represented by instances of the |
| 71 | \class{TestCase} class in the \refmodule{unittest} module. To make |
| 72 | your own test cases you must write subclasses of \class{TestCase}, or |
| 73 | use \class{FunctionTestCase}. |
| 74 | |
| 75 | An instance of a \class{TestCase}-derived class is an object that can |
| 76 | completely run a single test method, together with optional set-up |
| 77 | and tidy-up code. |
| 78 | |
| 79 | The testing code of a \class{TestCase} instance should be entirely |
| 80 | self contained, such that it can be run either in isolation or in |
| 81 | arbitrary combination with any number of other test cases. |
| 82 | |
| 83 | The simplest test case subclass will simply override the |
| 84 | \method{runTest()} method in order to perform specific testing code: |
| 85 | |
| 86 | \begin{verbatim} |
| 87 | import unittest |
| 88 | |
| 89 | class DefaultWidgetSizeTestCase(unittest.TestCase): |
| 90 | def runTest(self): |
| 91 | widget = Widget("The widget") |
| 92 | assert widget.size() == (50,50), 'incorrect default size' |
| 93 | \end{verbatim} |
| 94 | |
| 95 | Note that in order to test something, we just use the built-in 'assert' |
| 96 | statement of Python. If the test fails when the test case runs, |
| 97 | \class{TestFailed} will be raised, and the testing framework |
| 98 | will identify the test case as a \dfn{failure}. Other exceptions that |
| 99 | do not arise from explicit 'assert' checks are identified by the testing |
| 100 | framework as dfn{errors}. |
| 101 | |
| 102 | The way to run a test case will be described later. For now, note |
| 103 | that to construct an instance of such a test case, we call its |
| 104 | constructor without arguments: |
| 105 | |
| 106 | \begin{verbatim} |
| 107 | testCase = DefaultWidgetSizeTestCase() |
| 108 | \end{verbatim} |
| 109 | |
| 110 | Now, such test cases can be numerous, and their set-up can be |
| 111 | repetitive. In the above case, constructing a ``Widget'' in each of |
| 112 | 100 Widget test case subclasses would mean unsightly duplication. |
| 113 | |
| 114 | Luckily, we can factor out such set-up code by implementing a method |
| 115 | called \method{setUp()}, which the testing framework will |
| 116 | automatically call for us when we run the test: |
| 117 | |
| 118 | \begin{verbatim} |
| 119 | import unittest |
| 120 | |
| 121 | class SimpleWidgetTestCase(unittest.TestCase): |
| 122 | def setUp(self): |
| 123 | self.widget = Widget("The widget") |
| 124 | |
| 125 | class DefaultWidgetSizeTestCase(SimpleWidgetTestCase): |
| 126 | def runTest(self): |
| 127 | assert self.widget.size() == (50,50), 'incorrect default size' |
| 128 | |
| 129 | class WidgetResizeTestCase(SimpleWidgetTestCase): |
| 130 | def runTest(self): |
| 131 | self.widget.resize(100,150) |
| 132 | assert self.widget.size() == (100,150), \ |
| 133 | 'wrong size after resize' |
| 134 | \end{verbatim} |
| 135 | |
| 136 | If the \method{setUp()} method raises an exception while the test is |
| 137 | running, the framework will consider the test to have suffered an |
| 138 | error, and the \method{runTest()} method will not be executed. |
| 139 | |
| 140 | Similarly, we can provide a \method{tearDown()} method that tidies up |
| 141 | after the \method{runTest()} method has been run: |
| 142 | |
| 143 | \begin{verbatim} |
| 144 | import unittest |
| 145 | |
| 146 | class SimpleWidgetTestCase(unittest.TestCase): |
| 147 | def setUp(self): |
| 148 | self.widget = Widget("The widget") |
| 149 | |
| 150 | def tearDown(self): |
| 151 | self.widget.dispose() |
| 152 | self.widget = None |
| 153 | \end{verbatim} |
| 154 | |
| 155 | If \method{setUp()} succeeded, the \method{tearDown()} method will be |
| 156 | run regardless of whether or not \method{runTest()} succeeded. |
| 157 | |
| 158 | Such a working environment for the testing code is called a |
| 159 | \dfn{fixture}. |
| 160 | |
| 161 | Often, many small test cases will use the same fixture. In this case, |
| 162 | we would end up subclassing \class{SimpleWidgetTestCase} into many |
| 163 | small one-method classes such as |
| 164 | \class{DefaultWidgetSizeTestCase}. This is time-consuming and |
| 165 | discouraging, so in the same vein as JUnit, PyUnit provides a simpler |
| 166 | mechanism: |
| 167 | |
| 168 | \begin{verbatim} |
| 169 | import unittest |
| 170 | |
| 171 | class WidgetTestCase(unittest.TestCase): |
| 172 | def setUp(self): |
| 173 | self.widget = Widget("The widget") |
| 174 | |
| 175 | def tearDown(self): |
| 176 | self.widget.dispose() |
| 177 | self.widget = None |
| 178 | |
| 179 | def testDefaultSize(self): |
| 180 | assert self.widget.size() == (50,50), \ |
| 181 | 'incorrect default size' |
| 182 | |
| 183 | def testResize(self): |
| 184 | self.widget.resize(100,150) |
| 185 | assert self.widget.size() == (100,150), \ |
| 186 | 'wrong size after resize' |
| 187 | \end{verbatim} |
| 188 | |
| 189 | Here we have not provided a \method{runTest()} method, but have |
| 190 | instead provided two different test methods. Class instances will now |
| 191 | each run one of the \method{test*()} methods, with \code{self.widget} |
| 192 | created and destroyed separately for each instance. When creating an |
| 193 | instance we must specify the test method it is to run. We do this by |
| 194 | passing the method name in the constructor: |
| 195 | |
| 196 | \begin{verbatim} |
| 197 | defaultSizeTestCase = WidgetTestCase("testDefaultSize") |
| 198 | resizeTestCase = WidgetTestCase("testResize") |
| 199 | \end{verbatim} |
| 200 | |
| 201 | Test case instances are grouped together according to the features |
| 202 | they test. PyUnit provides a mechanism for this: the \class{test |
| 203 | suite}, represented by the class \class{TestSuite} in the |
| 204 | \refmodule{unittest} module: |
| 205 | |
| 206 | \begin{verbatim} |
| 207 | widgetTestSuite = unittest.TestSuite() |
| 208 | widgetTestSuite.addTest(WidgetTestCase("testDefaultSize")) |
| 209 | widgetTestSuite.addTest(WidgetTestCase("testResize")) |
| 210 | \end{verbatim} |
| 211 | |
| 212 | For the ease of running tests, as we will see later, it is a good |
| 213 | idea to provide in each test module a callable object that returns a |
| 214 | pre-built test suite: |
| 215 | |
| 216 | \begin{verbatim} |
| 217 | def suite(): |
| 218 | suite = unittest.TestSuite() |
| 219 | suite.addTest(WidgetTestCase("testDefaultSize")) |
| 220 | suite.addTest(WidgetTestCase("testResize")) |
| 221 | return suite |
| 222 | \end{verbatim} |
| 223 | |
| 224 | or even: |
| 225 | |
| 226 | \begin{verbatim} |
| 227 | class WidgetTestSuite(unittest.TestSuite): |
| 228 | def __init__(self): |
| 229 | unittest.TestSuite.__init__(self,map(WidgetTestCase, |
| 230 | ("testDefaultSize", |
| 231 | "testResize"))) |
| 232 | \end{verbatim} |
| 233 | |
| 234 | (The latter is admittedly not for the faint-hearted!) |
| 235 | |
| 236 | Since it is a common pattern to create a \class{TestCase} subclass |
| 237 | with many similarly named test functions, there is a convenience |
| 238 | function called \function{makeSuite()} provided in the |
| 239 | \refmodule{unittest} module that constructs a test suite that |
| 240 | comprises all of the test cases in a test case class: |
| 241 | |
| 242 | \begin{verbatim} |
| 243 | suite = unittest.makeSuite(WidgetTestCase,'test') |
| 244 | \end{verbatim} |
| 245 | |
| 246 | Note that when using the \function{makeSuite()} function, the order in |
| 247 | which the various test cases will be run by the test suite is the |
| 248 | order determined by sorting the test function names using the |
| 249 | \function{cmp()} built-in function. |
| 250 | |
| 251 | Often it is desirable to group suites of test cases together, so as to |
| 252 | run tests for the whole system at once. This is easy, since |
| 253 | \class{TestSuite} instances can be added to a \class{TestSuite} just |
| 254 | as \class{TestCase} instances can be added to a \class{TestSuite}: |
| 255 | |
| 256 | \begin{verbatim} |
| 257 | suite1 = module1.TheTestSuite() |
| 258 | suite2 = module2.TheTestSuite() |
| 259 | alltests = unittest.TestSuite((suite1, suite2)) |
| 260 | \end{verbatim} |
| 261 | |
| 262 | You can place the definitions of test cases and test suites in the |
| 263 | same modules as the code they are to test (e.g.\ \file{widget.py}), |
| 264 | but there are several advantages to placing the test code in a |
| 265 | separate module, such as \file{widgettests.py}: |
| 266 | |
| 267 | \begin{itemize} |
| 268 | \item The test module can be run standalone from the command line. |
| 269 | \item The test code can more easily be separated from shipped code. |
| 270 | \item There is less temptation to change test code to fit the code. |
| 271 | it tests without a good reason. |
| 272 | \item Test code should be modified much less frequently than the |
| 273 | code it tests. |
| 274 | \item Tested code can be refactored more easily. |
| 275 | \item Tests for modules written in C must be in separate modules |
| 276 | anyway, so why not be consistent? |
| 277 | \item If the testing strategy changes, there is no need to change |
| 278 | the source code. |
| 279 | \end{itemize} |
| 280 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 281 | |
| 282 | \subsection{Re-using old test code |
| 283 | \label{legacy-unit-tests}} |
| 284 | |
| 285 | Some users will find that they have existing test code that they would |
| 286 | like to run from PyUnit, without converting every old test function to |
| 287 | a \class{TestCase} subclass. |
| 288 | |
| 289 | For this reason, PyUnit provides a \class{FunctionTestCase} class. |
| 290 | This subclass of \class{TestCase} can be used to wrap an existing test |
| 291 | function. Set-up and tear-down functions can also optionally be |
| 292 | wrapped. |
| 293 | |
| 294 | Given the following test function: |
| 295 | |
| 296 | \begin{verbatim} |
| 297 | def testSomething(): |
| 298 | something = makeSomething() |
| 299 | assert something.name is not None |
| 300 | # ... |
| 301 | \end{verbatim} |
| 302 | |
| 303 | one can create an equivalent test case instance as follows: |
| 304 | |
| 305 | \begin{verbatim} |
| 306 | testcase = unittest.FunctionTestCase(testSomething) |
| 307 | \end{verbatim} |
| 308 | |
| 309 | If there are additional set-up and tear-down methods that should be |
| 310 | called as part of the test case's operation, they can also be provided: |
| 311 | |
| 312 | \begin{verbatim} |
| 313 | testcase = unittest.FunctionTestCase(testSomething, |
| 314 | setUp=makeSomethingDB, |
| 315 | tearDown=deleteSomethingDB) |
| 316 | \end{verbatim} |
| 317 | |
| 318 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 319 | \strong{Note:} PyUnit supports the use of \exception{AssertionError} |
| 320 | as an indicator of test failure, but does not recommend it. Future |
| 321 | versions may treat \exception{AssertionError} differently. |
| 322 | |
| 323 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 324 | \subsection{Classes and functions |
| 325 | \label{unittest-contents}} |
| 326 | |
| 327 | \begin{classdesc}{TestCase}{} |
| 328 | Instances of the \class{TestCase} class represent the smallest |
| 329 | testable units in a set of tests. This class is intended to be used |
| 330 | as a base class, with specific tests being implemented by concrete |
| 331 | subclasses. This class implements the interface needed by the test |
| 332 | runner to allow it to drive the test, and methods that the test code |
| 333 | can use to check for and report various kinds of failures. |
| 334 | \end{classdesc} |
| 335 | |
| 336 | \begin{classdesc}{FunctionTestCase}{testFunc\optional{, |
| 337 | setup\optional{, tearDown\optional{, description}}}} |
| 338 | This class implements the portion of the \class{TestCase} interface |
| 339 | which allows the test runner to drive the test, but does not provide |
| 340 | the methods which test code can use to check and report errors. |
| 341 | This is used to create test cases using legacy test code, allowing |
| 342 | it to be integrated into a \refmodule{unittest}-based test |
| 343 | framework. |
| 344 | \end{classdesc} |
| 345 | |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 346 | \begin{classdesc}{TestSuite}{\optional{tests}} |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 347 | This class represents an aggregation of individual tests cases and |
| 348 | test suites. The class presents the interface needed by the test |
| 349 | runner to allow it to be run as any other test case, but all the |
| 350 | contained tests and test suites are executed. Additional methods |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 351 | are provided to add test cases and suites to the aggregation. If |
| 352 | \var{tests} is given, it must be a sequence of individual tests that |
| 353 | will be added to the suite. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 354 | \end{classdesc} |
| 355 | |
| 356 | \begin{classdesc}{TestLoader}{} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 357 | This class is responsible for loading tests according to various |
| 358 | criteria and returning them wrapped in a \class{TestSuite}. |
| 359 | It can load all tests within a given module or \class{TestCase} |
| 360 | class. When loading from a module, it considers all |
| 361 | \class{TestCase}-derived classes. For each such class, it creates |
| 362 | an instance for each method with a name beginning with the string |
| 363 | \samp{test}. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 364 | \end{classdesc} |
| 365 | |
| 366 | \begin{classdesc}{TextTestRunner}{\optional{stream\optional{, |
| 367 | descriptions\optional{, verbosity}}}} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 368 | A basic test runner implementation which prints results on standard |
| 369 | output. It has a few configurable parameters, but is essentially |
| 370 | very simple. Graphical applications which run test suites should |
| 371 | provide alternate implementations. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 372 | \end{classdesc} |
| 373 | |
| 374 | \begin{funcdesc}{main}{\optional{module\optional{, |
| 375 | defaultTest\optional{, argv\optional{, |
| 376 | testRunner\optional{, testRunner}}}}}} |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 377 | A command-line program that runs a set of tests; this is primarily |
| 378 | for making test modules conveniently executable. The simplest use |
| 379 | for this function is: |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 380 | |
| 381 | \begin{verbatim} |
| 382 | if __name__ == '__main__': |
| 383 | unittest.main() |
| 384 | \end{verbatim} |
| 385 | \end{funcdesc} |
| 386 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 387 | \begin{excdesc}{TestFailed} |
| 388 | Exception raised to indicate that a test failed. The |
| 389 | \method{TestCase.fail()} method is responsible for creating and |
| 390 | raising this exception. |
| 391 | \end{excdesc} |
| 392 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 393 | |
| 394 | \subsection{TestCase Objects |
| 395 | \label{testcase-objects}} |
| 396 | |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 397 | Each \class{TestCase} instance represents a single test, but each |
| 398 | concrete subclass may be used to define multiple tests --- the |
| 399 | concrete class represents a single test fixture. The fixture is |
| 400 | created and cleaned up for each test case. |
| 401 | |
| 402 | \class{TestCase} instances provide three groups of methods: one group |
| 403 | used to run the test, another used by the test implementation to |
| 404 | check conditions and report failures, and some inquiry methods |
| 405 | allowing information about the test itself to be gathered. |
| 406 | |
| 407 | Methods in the first group are: |
| 408 | |
| 409 | \begin{methoddesc}[TestCase]{setUp}{} |
| 410 | Method called to prepare the test fixture. This is called |
| 411 | immediately before calling the test method; any exception raised by |
| 412 | this method will be considered an error rather than a test failure. |
| 413 | The default implementation does nothing. |
| 414 | \end{methoddesc} |
| 415 | |
| 416 | \begin{methoddesc}[TestCase]{run}{\optional{result}} |
| 417 | Run the test, collecting the result into the test result object |
| 418 | passed as \var{result}. If \var{result} is omitted or \code{None}, |
| 419 | a temporary result object is created and used, but is not made |
| 420 | available to the caller. This is equivalent to simply calling the |
| 421 | \class{TestCase} instance. |
| 422 | \end{methoddesc} |
| 423 | |
| 424 | \begin{methoddesc}[TestCase]{tearDown}{} |
| 425 | Method called immediately after the test method has been called and |
| 426 | the result recorded. This is called even if the test method raised |
| 427 | an exception, so the implementation in subclasses may need to be |
| 428 | particularly careful about checking internal state. Any exception |
| 429 | raised by this method will be considered an error rather than a test |
| 430 | failure. The default implementation does nothing. |
| 431 | \end{methoddesc} |
| 432 | |
| 433 | \begin{methoddesc}[TestCase]{debug}{} |
| 434 | Run the test without collecting the result. This allows exceptions |
| 435 | raised by the test to be propogated to the caller, and can be used |
| 436 | to support running tests under a debugger. |
| 437 | \end{methoddesc} |
| 438 | |
| 439 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 440 | The test code can use any of the following methods to check for and |
| 441 | report failures: |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 442 | |
| 443 | \begin{methoddesc}[TestCase]{failUnless}{expr\optional{, msg}} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 444 | This method is similar to the \keyword{assert} statement, except it |
| 445 | works even when Python is executed in ``optimizing'' mode (using the |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 446 | \programopt{-O} command line switch), and raises the |
| 447 | \exception{TestFailed} exception. If \var{expr} is false, |
| 448 | \exception{TestFailed} will be raised with \var{msg} as the |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 449 | message describing the failure; \code{None} will be used for the |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 450 | message if \var{msg} is omitted. |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 451 | \end{methoddesc} |
| 452 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 453 | \begin{methoddesc}[TestCase]{failUnlessEqual}{first, second\optional{, msg}} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 454 | Test that \var{first} and \var{second} are equal. If the values do |
| 455 | not compare equal, the test will fail with the explanation given by |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 456 | \var{msg}, or \code{None}. Note that using \method{failUnlessEqual()} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 457 | improves upon doing the comparison as the first parameter to |
| 458 | \method{failUnless()} is that the default value for \var{msg} can be |
| 459 | computed to include representations of both \var{first} and |
| 460 | \var{second}. |
| 461 | \end{methoddesc} |
| 462 | |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 463 | \begin{methoddesc}[TestCase]{failIfEqual}{first, second\optional{, msg}} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 464 | Test that \var{first} and \var{second} are not equal. If the values |
| 465 | do compare equal, the test will fail with the explanation given by |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 466 | \var{msg}, or \code{None}. Note that using \method{failIfEqual()} |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 467 | improves upon doing the comparison as the first parameter to |
| 468 | \method{failUnless()} is that the default value for \var{msg} can be |
| 469 | computed to include representations of both \var{first} and |
| 470 | \var{second}. |
| 471 | \end{methoddesc} |
| 472 | |
| 473 | \begin{methoddesc}[TestCase]{failIf}{expr\optional{, msg}} |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 474 | The inverse of the \method{failUnless()} method is the |
| 475 | \method{failIf()} method. This raises \exception{TestFailed} if |
Fred Drake | 29be701 | 2001-04-10 22:25:06 +0000 | [diff] [blame] | 476 | \var{expr} is true, with \var{msg} or \code{None} for the error |
| 477 | message. |
| 478 | \end{methoddesc} |
| 479 | |
| 480 | \begin{methoddesc}[TestCase]{fail}{\optional{msg}} |
| 481 | Fail unconditionally, with \var{msg} or \code{None} for the error |
| 482 | message. |
| 483 | \end{methoddesc} |
| 484 | |
| 485 | |
| 486 | Testing frameworks can use the following methods to collect |
| 487 | information on the test: |
| 488 | |
| 489 | \begin{methoddesc}[TestCase]{countTestCases}{} |
| 490 | Return the number of tests represented by the this test object. For |
| 491 | \class{TestCase} instances, this will always be \code{1}, but this |
| 492 | method is also implemented by the \class{TestSuite} class, which can |
| 493 | return larger values. |
| 494 | \end{methoddesc} |
| 495 | |
| 496 | \begin{methoddesc}[TestCase]{defaultTestResult}{} |
| 497 | Return the default type of test result object to be used to run this |
| 498 | test. |
| 499 | \end{methoddesc} |
| 500 | |
| 501 | \begin{methoddesc}[TestCase]{id}{} |
| 502 | Return a string identifying the specific test case. This is usually |
| 503 | the full name of the test method, including the module and class |
| 504 | names. |
| 505 | \end{methoddesc} |
| 506 | |
| 507 | \begin{methoddesc}[TestCase]{shortDescription}{} |
| 508 | Returns a one-line description of the test, or \code{None} if no |
| 509 | description has been provided. The default implementation of this |
| 510 | method returns the first line of the test method's docstring, if |
| 511 | available, or \code{None}. |
| 512 | \end{methoddesc} |
| 513 | |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 514 | |
| 515 | \subsection{TestSuite Objects |
| 516 | \label{testsuite-objects}} |
| 517 | |
| 518 | \class{TestSuite} objects behave much like \class{TestCase} objects, |
| 519 | except they do not actually implement a test. Instead, they are used |
| 520 | to aggregate tests into groups that should be run together. Some |
| 521 | additional methods are available to add tests to \class{TestSuite} |
| 522 | instances: |
| 523 | |
| 524 | \begin{methoddesc}[TestSuite]{addTest}{test} |
| 525 | Add a \class{TestCase} or \class{TestSuite} to the set of tests that |
| 526 | make up the suite. |
| 527 | \end{methoddesc} |
| 528 | |
| 529 | \begin{methoddesc}[TestSuite]{addTests}{tests} |
| 530 | Add all the tests from a sequence of \class{TestCase} and |
| 531 | \class{TestSuite} instances to this test suite. |
| 532 | \end{methoddesc} |
| 533 | |
| 534 | |
| 535 | \subsection{TestResult Objects |
| 536 | \label{testresult-objects}} |
| 537 | |
| 538 | A \class{TestResult} object stores the results of a set of tests. The |
| 539 | \class{TestCase} and \class{TestSuite} classes ensure that results are |
| 540 | properly stored; test authors do not need to worry about recording the |
| 541 | outcome of tests. |
| 542 | |
| 543 | Testing frameworks built on top of \refmodule{unittest} may want |
| 544 | access to the \class{TestResult} object generated by running a set of |
| 545 | tests for reporting purposes; a \class{TestResult} instance is |
| 546 | returned by the \method{TestRunner.run()} method for this purpose. |
| 547 | |
| 548 | Each instance holds the total number of tests run, and collections of |
| 549 | failures and errors that occurred among those test runs. The |
| 550 | collections contain tuples of \code{(\var{testcase}, |
| 551 | \var{exceptioninfo})}, where \var{exceptioninfo} is a tuple as |
| 552 | returned by \function{sys.exc_info()}. |
| 553 | |
| 554 | \class{TestResult} instances have the following attributes that will |
| 555 | be of interest when inspecting the results of running a set of tests: |
| 556 | |
| 557 | \begin{memberdesc}[TestResult]{errors} |
| 558 | A list containing pairs of \class{TestCase} instances and the |
| 559 | \function{sys.exc_info()} results for tests which raised exceptions |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 560 | other than \exception{AssertionError} and \exception{TestFailed}. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 561 | \end{memberdesc} |
| 562 | |
| 563 | \begin{memberdesc}[TestResult]{failures} |
| 564 | A list containing pairs of \class{TestCase} instances and the |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 565 | \function{sys.exc_info()} results for tests which raised either |
| 566 | \exception{TestFailed} or \exception{AssertionError}. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 567 | \end{memberdesc} |
| 568 | |
| 569 | \begin{memberdesc}[TestResult]{testsRun} |
| 570 | The number of tests which have been started. |
| 571 | \end{memberdesc} |
| 572 | |
| 573 | \begin{methoddesc}[TestResult]{wasSuccessful}{} |
| 574 | Returns true if all tests run so far have passed, otherwise returns |
| 575 | false. |
| 576 | \end{methoddesc} |
| 577 | |
| 578 | |
| 579 | The following methods of the \class{TestResult} class are used to |
| 580 | maintain the internal data structures, and mmay be extended in |
| 581 | subclasses to support additional reporting requirements. This is |
| 582 | particularly useful in building GUI tools which support interactive |
| 583 | reporting while tests are being run. |
| 584 | |
| 585 | \begin{methoddesc}[TestResult]{startTest}{test} |
| 586 | Called when the test case \var{test} is about to be run. |
| 587 | \end{methoddesc} |
| 588 | |
| 589 | \begin{methoddesc}[TestResult]{stopTest}{test} |
| 590 | Called when the test case \var{test} has been executed, regardless |
| 591 | of the outcome. |
| 592 | \end{methoddesc} |
| 593 | |
| 594 | \begin{methoddesc}[TestResult]{addError}{test, err} |
| 595 | Called when the test case \var{test} results in an exception other |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 596 | than \exception{TestFailed} or \exception{AssertionError}. |
| 597 | \var{err} is a tuple of the form returned by |
| 598 | \function{sys.exc_info()}: \code{(\var{type}, \var{value}, |
| 599 | \var{traceback})}. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 600 | \end{methoddesc} |
| 601 | |
| 602 | \begin{methoddesc}[TestResult]{addFailure}{test, err} |
| 603 | Called when the test case \var{test} results in an |
| 604 | \exception{AssertionError} exception; the assumption is that the |
Fred Drake | 0056a42 | 2001-04-12 04:50:06 +0000 | [diff] [blame^] | 605 | test raised either \exception{TestFailed} or |
| 606 | \exception{AssertionError} and not the implementation being tested. |
| 607 | \var{err} is a tuple of the form returned by |
| 608 | \function{sys.exc_info()}: \code{(\var{type}, \var{value}, |
| 609 | \var{traceback})}. |
Fred Drake | b9ad228 | 2001-04-07 05:41:39 +0000 | [diff] [blame] | 610 | \end{methoddesc} |
| 611 | |
| 612 | \begin{methoddesc}[TestResult]{addSuccess}{test} |
| 613 | This method is called for a test that does not fail; \var{test} is |
| 614 | the test case object. |
| 615 | \end{methoddesc} |
| 616 | |
| 617 | |
| 618 | One additional method is available for \class{TestResult} objects: |
| 619 | |
| 620 | \begin{methoddesc}[TestResult]{stop}{} |
| 621 | This method can be called to signal that the set of tests being run |
| 622 | should be aborted. Once this has been called, the |
| 623 | \class{TestRunner} object return to its caller without running any |
| 624 | additional tests. This is used by the \class{TextTestRunner} class |
| 625 | to stop the test framework when the user signals an interrupt from |
| 626 | the keyboard. GUI tools which provide runners can use this in a |
| 627 | similar manner. |
| 628 | \end{methoddesc} |