blob: 350abae7863bc7bdfd054dd196e748f9313498a2 [file] [log] [blame]
Fred Drakeb9ad2282001-04-07 05:41:39 +00001\section{\module{unittest} ---
2 Unit testing framework}
3
4\declaremodule{standard}{unittest}
Fred Drake62a26692001-04-12 19:34:38 +00005\modulesynopsis{Unit testing framework for Python.}
Fred Drakeb9ad2282001-04-07 05:41:39 +00006\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 Hettinger9de3c212003-07-10 22:14:41 +00009\sectionauthor{Raymond Hettinger}{python@rcn.com}
Fred Drakeb9ad2282001-04-07 05:41:39 +000010
Fred Drake5430f4e2002-10-10 16:16:25 +000011\versionadded{2.1}
Fred Drakeb9ad2282001-04-07 05:41:39 +000012
Thomas Wouters0e3f5912006-08-11 14:57:12 +000013The Python unit testing framework, sometimes referred to as ``PyUnit,'' is
Fred Drakeb9ad2282001-04-07 05:41:39 +000014a Python language version of JUnit, by Kent Beck and Erich Gamma.
15JUnit is, in turn, a Java version of Kent's Smalltalk testing
16framework. Each is the de facto standard unit testing framework for
17its respective language.
18
Thomas Wouters0e3f5912006-08-11 14:57:12 +000019\module{unittest} supports test automation, sharing of setup and shutdown
20code for tests, aggregation of tests into collections, and independence of
Fred Drakeb9ad2282001-04-07 05:41:39 +000021the tests from the reporting framework. The \module{unittest} module
22provides classes that make it easy to support these qualities for a
23set of tests.
24
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025To achieve this, \module{unittest} supports some important concepts:
Fred Drakeb9ad2282001-04-07 05:41:39 +000026
27\begin{definitions}
Fred Drake29be7012001-04-10 22:25:06 +000028\term{test fixture}
29A \dfn{test fixture} represents the preparation needed to perform one
30or more tests, and any associate cleanup actions. This may involve,
31for example, creating temporary or proxy databases, directories, or
32starting a server process.
33
Fred Drakeb9ad2282001-04-07 05:41:39 +000034\term{test case}
35A \dfn{test case} is the smallest unit of testing. It checks for a
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036specific response to a particular set of inputs. \module{unittest}
37provides a base class, \class{TestCase}, which may be used to create
38new test cases.
Fred Drakeb9ad2282001-04-07 05:41:39 +000039
40\term{test suite}
41A \dfn{test suite} is a collection of test cases, test suites, or
42both. It is used to aggregate tests that should be executed
43together.
44
45\term{test runner}
46A \dfn{test runner} is a component which orchestrates the execution of
47tests and provides the outcome to the user. The runner may use a
48graphical interface, a textual interface, or return a special value to
49indicate the results of executing the tests.
50\end{definitions}
51
52
Fred Drake62a26692001-04-12 19:34:38 +000053The test case and test fixture concepts are supported through the
54\class{TestCase} and \class{FunctionTestCase} classes; the former
Just van Rossumedd179e2002-12-15 13:14:22 +000055should be used when creating new tests, and the latter can be used when
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056integrating existing test code with a \module{unittest}-driven framework.
57When building test fixtures using \class{TestCase}, the \method{setUp()}
Fred Drake62a26692001-04-12 19:34:38 +000058and \method{tearDown()} methods can be overridden to provide
59initialization and cleanup for the fixture. With
60\class{FunctionTestCase}, existing functions can be passed to the
61constructor for these purposes. When the test is run, the
62fixture initialization is run first; if it succeeds, the cleanup
63method is run after the test has been executed, regardless of the
64outcome of the test. Each instance of the \class{TestCase} will only
65be used to run a single test method, so a new fixture is created for
66each test.
67
68Test suites are implemented by the \class{TestSuite} class. This
69class allows individual tests and test suites to be aggregated; when
70the suite is executed, all tests added directly to the suite and in
71``child'' test suites are run.
72
73A test runner is an object that provides a single method,
74\method{run()}, which accepts a \class{TestCase} or \class{TestSuite}
75object as a parameter, and returns a result object. The class
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076\class{TestResult} is provided for use as the result object.
77\module{unittest} provides the \class{TextTestRunner} as an example
78test runner which reports test results on the standard error stream by
79default. Alternate runners can be implemented for other environments
80(such as graphical environments) without any need to derive from a
81specific class.
Fred Drake62a26692001-04-12 19:34:38 +000082
Fred Drakeb9ad2282001-04-07 05:41:39 +000083
84\begin{seealso}
Fred Drakeae55d5f2003-12-31 04:34:50 +000085 \seemodule{doctest}{Another test-support module with a very
86 different flavor.}
Fred Drakeb9ad2282001-04-07 05:41:39 +000087 \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
Raymond Hettingerd21fd7b2003-09-16 22:04:31 +000094\subsection{Basic example \label{minimal-example}}
Raymond Hettinger9de3c212003-07-10 22:14:41 +000095
96The \module{unittest} module provides a rich set of tools for
97constructing and running tests. This section demonstrates that a
98small subset of the tools suffice to meet the needs of most users.
99
100Here is a short script to test three functions from the
101\refmodule{random} module:
102
103\begin{verbatim}
104import random
105import unittest
106
107class 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
127if __name__ == '__main__':
128 unittest.main()
129\end{verbatim}
130
Fred Drakeae55d5f2003-12-31 04:34:50 +0000131A testcase is created by subclassing \class{unittest.TestCase}.
Raymond Hettinger9de3c212003-07-10 22:14:41 +0000132The three individual tests are defined with methods whose names start with
Fred Drakeae55d5f2003-12-31 04:34:50 +0000133the letters \samp{test}. This naming convention informs the test runner
Raymond Hettinger9de3c212003-07-10 22:14:41 +0000134about which methods represent tests.
135
136The crux of each test is a call to \method{assertEqual()} to
137check for an expected result; \method{assert_()} to verify a condition;
138or \method{assertRaises()} to verify that an expected exception gets
139raised. These methods are used instead of the \keyword{assert} statement
140so the test runner can accumulate all test results and produce a report.
141
142When a \method{setUp()} method is defined, the test runner will run that
143method prior to each test. Likewise, if a \method{tearDown()} method is
144defined, the test runner will invoke that method after each test. In the
145example, \method{setUp()} was used to create a fresh sequence for each test.
146
Fred Drakeae55d5f2003-12-31 04:34:50 +0000147The final block shows a simple way to run the tests.
148\function{unittest.main()} provides a command line interface to the
149test script. When run from the command line, the above script
150produces an output that looks like this:
Raymond Hettinger9de3c212003-07-10 22:14:41 +0000151
152\begin{verbatim}
153...
154----------------------------------------------------------------------
155Ran 3 tests in 0.000s
156
157OK
158\end{verbatim}
159
Fred Drakeae55d5f2003-12-31 04:34:50 +0000160Instead of \function{unittest.main()}, there are other ways to run the tests
Raymond Hettinger9de3c212003-07-10 22:14:41 +0000161with a finer level of control, less terse output, and no requirement to be
162run from the command line. For example, the last two lines may be replaced
163with:
164
165\begin{verbatim}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000166suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
Raymond Hettinger9de3c212003-07-10 22:14:41 +0000167unittest.TextTestRunner(verbosity=2).run(suite)
168\end{verbatim}
169
170Running the revised script from the interpreter or another script
171produces the following output:
172
173\begin{verbatim}
174testchoice (__main__.TestSequenceFunctions) ... ok
175testsample (__main__.TestSequenceFunctions) ... ok
176testshuffle (__main__.TestSequenceFunctions) ... ok
177
178----------------------------------------------------------------------
179Ran 3 tests in 0.110s
180
181OK
182\end{verbatim}
183
184The above examples show the most commonly used \module{unittest} features
185which are sufficient to meet many everyday testing needs. The remainder
186of the documentation explores the full feature set from first principles.
187
188
Fred Drakeb9ad2282001-04-07 05:41:39 +0000189\subsection{Organizing test code
190 \label{organizing-tests}}
191
Fred Drake0056a422001-04-12 04:50:06 +0000192The basic building blocks of unit testing are \dfn{test cases} ---
193single scenarios that must be set up and checked for correctness. In
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000194\module{unittest}, test cases are represented by instances of
195\module{unittest}'s \class{TestCase} class. To make
Fred Drake0056a422001-04-12 04:50:06 +0000196your own test cases you must write subclasses of \class{TestCase}, or
197use \class{FunctionTestCase}.
198
199An instance of a \class{TestCase}-derived class is an object that can
200completely run a single test method, together with optional set-up
201and tidy-up code.
202
203The testing code of a \class{TestCase} instance should be entirely
204self contained, such that it can be run either in isolation or in
205arbitrary combination with any number of other test cases.
206
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000207The simplest \class{TestCase} subclass will simply override the
Fred Drake0056a422001-04-12 04:50:06 +0000208\method{runTest()} method in order to perform specific testing code:
209
210\begin{verbatim}
211import unittest
212
213class DefaultWidgetSizeTestCase(unittest.TestCase):
214 def runTest(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000215 widget = Widget('The widget')
216 self.assertEqual(widget.size(), (50, 50), 'incorrect default size')
Fred Drake0056a422001-04-12 04:50:06 +0000217\end{verbatim}
218
Fred Drake62a26692001-04-12 19:34:38 +0000219Note that in order to test something, we use the one of the
220\method{assert*()} or \method{fail*()} methods provided by the
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221\class{TestCase} base class. If the test fails, an exception will be
222raised, and \module{unittest} will identify the test case as a
223\dfn{failure}. Any other exceptions will be treated as \dfn{errors}.
224This helps you identify where the problem is: \dfn{failures} are caused by
225incorrect results - a 5 where you expected a 6. \dfn{Errors} are caused by
226incorrect code - e.g., a \exception{TypeError} caused by an incorrect
227function call.
Fred Drake0056a422001-04-12 04:50:06 +0000228
229The way to run a test case will be described later. For now, note
230that to construct an instance of such a test case, we call its
231constructor without arguments:
232
233\begin{verbatim}
234testCase = DefaultWidgetSizeTestCase()
235\end{verbatim}
236
237Now, such test cases can be numerous, and their set-up can be
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238repetitive. In the above case, constructing a \class{Widget} in each of
Fred Drake0056a422001-04-12 04:50:06 +0000239100 Widget test case subclasses would mean unsightly duplication.
240
241Luckily, we can factor out such set-up code by implementing a method
242called \method{setUp()}, which the testing framework will
243automatically call for us when we run the test:
244
245\begin{verbatim}
246import unittest
247
248class SimpleWidgetTestCase(unittest.TestCase):
249 def setUp(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250 self.widget = Widget('The widget')
Fred Drake0056a422001-04-12 04:50:06 +0000251
252class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
253 def runTest(self):
Fred Drake62a26692001-04-12 19:34:38 +0000254 self.failUnless(self.widget.size() == (50,50),
255 'incorrect default size')
Fred Drake0056a422001-04-12 04:50:06 +0000256
257class WidgetResizeTestCase(SimpleWidgetTestCase):
258 def runTest(self):
259 self.widget.resize(100,150)
Fred Drake62a26692001-04-12 19:34:38 +0000260 self.failUnless(self.widget.size() == (100,150),
261 'wrong size after resize')
Fred Drake0056a422001-04-12 04:50:06 +0000262\end{verbatim}
263
264If the \method{setUp()} method raises an exception while the test is
265running, the framework will consider the test to have suffered an
266error, and the \method{runTest()} method will not be executed.
267
268Similarly, we can provide a \method{tearDown()} method that tidies up
269after the \method{runTest()} method has been run:
270
271\begin{verbatim}
272import unittest
273
274class SimpleWidgetTestCase(unittest.TestCase):
275 def setUp(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000276 self.widget = Widget('The widget')
Fred Drake0056a422001-04-12 04:50:06 +0000277
278 def tearDown(self):
279 self.widget.dispose()
280 self.widget = None
281\end{verbatim}
282
283If \method{setUp()} succeeded, the \method{tearDown()} method will be
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284run whether \method{runTest()} succeeded or not.
Fred Drake0056a422001-04-12 04:50:06 +0000285
286Such a working environment for the testing code is called a
287\dfn{fixture}.
288
289Often, many small test cases will use the same fixture. In this case,
290we would end up subclassing \class{SimpleWidgetTestCase} into many
291small one-method classes such as
292\class{DefaultWidgetSizeTestCase}. This is time-consuming and
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000293discouraging, so in the same vein as JUnit, \module{unittest} provides
294a simpler mechanism:
Fred Drake0056a422001-04-12 04:50:06 +0000295
296\begin{verbatim}
297import unittest
298
299class WidgetTestCase(unittest.TestCase):
300 def setUp(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000301 self.widget = Widget('The widget')
Fred Drake0056a422001-04-12 04:50:06 +0000302
303 def tearDown(self):
304 self.widget.dispose()
305 self.widget = None
306
307 def testDefaultSize(self):
Fred Drake62a26692001-04-12 19:34:38 +0000308 self.failUnless(self.widget.size() == (50,50),
309 'incorrect default size')
Fred Drake0056a422001-04-12 04:50:06 +0000310
311 def testResize(self):
312 self.widget.resize(100,150)
Fred Drake62a26692001-04-12 19:34:38 +0000313 self.failUnless(self.widget.size() == (100,150),
314 'wrong size after resize')
Fred Drake0056a422001-04-12 04:50:06 +0000315\end{verbatim}
316
317Here we have not provided a \method{runTest()} method, but have
318instead provided two different test methods. Class instances will now
319each run one of the \method{test*()} methods, with \code{self.widget}
320created and destroyed separately for each instance. When creating an
321instance we must specify the test method it is to run. We do this by
322passing the method name in the constructor:
323
324\begin{verbatim}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000325defaultSizeTestCase = WidgetTestCase('testDefaultSize')
326resizeTestCase = WidgetTestCase('testResize')
Fred Drake0056a422001-04-12 04:50:06 +0000327\end{verbatim}
328
329Test case instances are grouped together according to the features
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000330they test. \module{unittest} provides a mechanism for this: the
331\dfn{test suite}, represented by \module{unittest}'s \class{TestSuite}
332class:
Fred Drake0056a422001-04-12 04:50:06 +0000333
334\begin{verbatim}
335widgetTestSuite = unittest.TestSuite()
Thomas Wouters89f507f2006-12-13 04:49:30 +0000336widgetTestSuite.addTest(WidgetTestCase('testDefaultSize'))
337widgetTestSuite.addTest(WidgetTestCase('testResize'))
Fred Drake0056a422001-04-12 04:50:06 +0000338\end{verbatim}
339
340For the ease of running tests, as we will see later, it is a good
341idea to provide in each test module a callable object that returns a
342pre-built test suite:
343
344\begin{verbatim}
345def suite():
346 suite = unittest.TestSuite()
Thomas Wouters89f507f2006-12-13 04:49:30 +0000347 suite.addTest(WidgetTestCase('testDefaultSize'))
348 suite.addTest(WidgetTestCase('testResize'))
Fred Drake0056a422001-04-12 04:50:06 +0000349 return suite
350\end{verbatim}
351
352or even:
353
354\begin{verbatim}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000355def suite():
Thomas Wouters89f507f2006-12-13 04:49:30 +0000356 tests = ['testDefaultSize', 'testResize']
Fred Drake0056a422001-04-12 04:50:06 +0000357
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000358 return unittest.TestSuite(map(WidgetTestCase, tests))
359\end{verbatim}
Fred Drake0056a422001-04-12 04:50:06 +0000360
361Since it is a common pattern to create a \class{TestCase} subclass
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000362with many similarly named test functions, \module{unittest} provides a
363\class{TestLoader} class that can be used to automate the process of
364creating a test suite and populating it with individual tests.
365For example,
Fred Drake0056a422001-04-12 04:50:06 +0000366
367\begin{verbatim}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000368suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
Fred Drake0056a422001-04-12 04:50:06 +0000369\end{verbatim}
370
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000371will create a test suite that will run
372\code{WidgetTestCase.testDefaultSize()} and \code{WidgetTestCase.testResize}.
373\class{TestLoader} uses the \code{'test'} method name prefix to identify
374test methods automatically.
375
376Note that the order in which the various test cases will be run is
377determined by sorting the test function names with the built-in
378\function{cmp()} function.
Fred Drake0056a422001-04-12 04:50:06 +0000379
380Often it is desirable to group suites of test cases together, so as to
381run tests for the whole system at once. This is easy, since
382\class{TestSuite} instances can be added to a \class{TestSuite} just
383as \class{TestCase} instances can be added to a \class{TestSuite}:
384
385\begin{verbatim}
386suite1 = module1.TheTestSuite()
387suite2 = module2.TheTestSuite()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000388alltests = unittest.TestSuite([suite1, suite2])
Fred Drake0056a422001-04-12 04:50:06 +0000389\end{verbatim}
390
391You can place the definitions of test cases and test suites in the
Raymond Hettingerd21fd7b2003-09-16 22:04:31 +0000392same modules as the code they are to test (such as \file{widget.py}),
Fred Drake0056a422001-04-12 04:50:06 +0000393but there are several advantages to placing the test code in a
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000394separate module, such as \file{test_widget.py}:
Fred Drake0056a422001-04-12 04:50:06 +0000395
396\begin{itemize}
397 \item The test module can be run standalone from the command line.
398 \item The test code can more easily be separated from shipped code.
Michael W. Hudsonaab02602003-02-10 19:21:16 +0000399 \item There is less temptation to change test code to fit the code
Fred Drake0056a422001-04-12 04:50:06 +0000400 it tests without a good reason.
401 \item Test code should be modified much less frequently than the
402 code it tests.
403 \item Tested code can be refactored more easily.
404 \item Tests for modules written in C must be in separate modules
405 anyway, so why not be consistent?
406 \item If the testing strategy changes, there is no need to change
407 the source code.
408\end{itemize}
409
Fred Drakeb9ad2282001-04-07 05:41:39 +0000410
411\subsection{Re-using old test code
412 \label{legacy-unit-tests}}
413
414Some users will find that they have existing test code that they would
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000415like to run from \module{unittest}, without converting every old test
416function to a \class{TestCase} subclass.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000417
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000418For this reason, \module{unittest} provides a \class{FunctionTestCase}
419class. This subclass of \class{TestCase} can be used to wrap an existing
420test function. Set-up and tear-down functions can also be provided.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000421
422Given the following test function:
423
424\begin{verbatim}
425def testSomething():
426 something = makeSomething()
427 assert something.name is not None
428 # ...
429\end{verbatim}
430
431one can create an equivalent test case instance as follows:
432
433\begin{verbatim}
434testcase = unittest.FunctionTestCase(testSomething)
435\end{verbatim}
436
437If there are additional set-up and tear-down methods that should be
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000438called as part of the test case's operation, they can also be provided
439like so:
Fred Drakeb9ad2282001-04-07 05:41:39 +0000440
441\begin{verbatim}
442testcase = unittest.FunctionTestCase(testSomething,
443 setUp=makeSomethingDB,
444 tearDown=deleteSomethingDB)
445\end{verbatim}
446
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000447To make migrating existing test suites easier, \module{unittest}
448supports tests raising \exception{AssertionError} to indicate test failure.
449However, it is recommended that you use the explicit
450\method{TestCase.fail*()} and \method{TestCase.assert*()} methods instead,
451as future versions of \module{unittest} may treat \exception{AssertionError}
452differently.
453
454\note{Even though \class{FunctionTestCase} can be used to quickly convert
455an existing test base over to a \module{unittest}-based system, this
456approach is not recommended. Taking the time to set up proper
457\class{TestCase} subclasses will make future test refactorings infinitely
458easier.}
459
Fred Drake0056a422001-04-12 04:50:06 +0000460
461
Fred Drakeb9ad2282001-04-07 05:41:39 +0000462\subsection{Classes and functions
463 \label{unittest-contents}}
464
Thomas Wouters89f507f2006-12-13 04:49:30 +0000465\begin{classdesc}{TestCase}{\optional{methodName}}
Fred Drakeb9ad2282001-04-07 05:41:39 +0000466 Instances of the \class{TestCase} class represent the smallest
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000467 testable units in the \module{unittest} universe. This class is
468 intended to be used as a base class, with specific tests being
469 implemented by concrete subclasses. This class implements the
470 interface needed by the test runner to allow it to drive the
471 test, and methods that the test code can use to check for and
472 report various kinds of failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000473
474 Each instance of \class{TestCase} will run a single test method:
475 the method named \var{methodName}. If you remember, we had an
476 earlier example that went something like this:
477
478 \begin{verbatim}
479 def suite():
480 suite = unittest.TestSuite()
481 suite.addTest(WidgetTestCase('testDefaultSize'))
482 suite.addTest(WidgetTestCase('testResize'))
483 return suite
484 \end{verbatim}
485
486 Here, we create two instances of \class{WidgetTestCase}, each of
487 which runs a single test.
488
489 \var{methodName} defaults to \code{'runTest'}.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000490\end{classdesc}
491
492\begin{classdesc}{FunctionTestCase}{testFunc\optional{,
Fred Drake62a26692001-04-12 19:34:38 +0000493 setUp\optional{, tearDown\optional{, description}}}}
Fred Drakeb9ad2282001-04-07 05:41:39 +0000494 This class implements the portion of the \class{TestCase} interface
495 which allows the test runner to drive the test, but does not provide
496 the methods which test code can use to check and report errors.
497 This is used to create test cases using legacy test code, allowing
498 it to be integrated into a \refmodule{unittest}-based test
499 framework.
500\end{classdesc}
501
Fred Drake29be7012001-04-10 22:25:06 +0000502\begin{classdesc}{TestSuite}{\optional{tests}}
Fred Drakeb9ad2282001-04-07 05:41:39 +0000503 This class represents an aggregation of individual tests cases and
504 test suites. The class presents the interface needed by the test
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505 runner to allow it to be run as any other test case. Running a
506 \class{TestSuite} instance is the same as iterating over the suite,
507 running each test individually.
508
509 If \var{tests} is given, it must be an iterable of individual test cases or
510 other test suites that will be used to build the suite initially.
511 Additional methods are provided to add test cases and suites to the
512 collection later on.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000513\end{classdesc}
514
515\begin{classdesc}{TestLoader}{}
Fred Drake29be7012001-04-10 22:25:06 +0000516 This class is responsible for loading tests according to various
517 criteria and returning them wrapped in a \class{TestSuite}.
518 It can load all tests within a given module or \class{TestCase}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000519 subclass.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000520\end{classdesc}
521
Thomas Wouters89f507f2006-12-13 04:49:30 +0000522\begin{classdesc}{TestResult}{}
523 This class is used to compile information about which tests have succeeded
524 and which have failed.
525\end{classdesc}
526
Fred Drake62a26692001-04-12 19:34:38 +0000527\begin{datadesc}{defaultTestLoader}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 Instance of the \class{TestLoader} class intended to be shared. If no
Fred Drake62a26692001-04-12 19:34:38 +0000529 customization of the \class{TestLoader} is needed, this instance can
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530 be used instead of repeatedly creating new instances.
Fred Drake62a26692001-04-12 19:34:38 +0000531\end{datadesc}
532
Fred Drakeb9ad2282001-04-07 05:41:39 +0000533\begin{classdesc}{TextTestRunner}{\optional{stream\optional{,
534 descriptions\optional{, verbosity}}}}
Fred Drake29be7012001-04-10 22:25:06 +0000535 A basic test runner implementation which prints results on standard
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536 error. It has a few configurable parameters, but is essentially
Fred Drake29be7012001-04-10 22:25:06 +0000537 very simple. Graphical applications which run test suites should
538 provide alternate implementations.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000539\end{classdesc}
540
541\begin{funcdesc}{main}{\optional{module\optional{,
542 defaultTest\optional{, argv\optional{,
543 testRunner\optional{, testRunner}}}}}}
Fred Drake0056a422001-04-12 04:50:06 +0000544 A command-line program that runs a set of tests; this is primarily
545 for making test modules conveniently executable. The simplest use
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000546 for this function is to include the following line at the end of a
547 test script:
Fred Drakeb9ad2282001-04-07 05:41:39 +0000548
549\begin{verbatim}
550if __name__ == '__main__':
551 unittest.main()
552\end{verbatim}
553\end{funcdesc}
554
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000555In some cases, the existing tests may have been written using the
Fred Drakeae55d5f2003-12-31 04:34:50 +0000556\refmodule{doctest} module. If so, that module provides a
Raymond Hettingerd21fd7b2003-09-16 22:04:31 +0000557\class{DocTestSuite} class that can automatically build
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000558\class{unittest.TestSuite} instances from the existing
559\module{doctest}-based tests.
Raymond Hettingerd21fd7b2003-09-16 22:04:31 +0000560\versionadded{2.3}
561
Fred Drakeb9ad2282001-04-07 05:41:39 +0000562
563\subsection{TestCase Objects
564 \label{testcase-objects}}
565
Fred Drake29be7012001-04-10 22:25:06 +0000566Each \class{TestCase} instance represents a single test, but each
567concrete subclass may be used to define multiple tests --- the
568concrete class represents a single test fixture. The fixture is
569created and cleaned up for each test case.
570
571\class{TestCase} instances provide three groups of methods: one group
572used to run the test, another used by the test implementation to
573check conditions and report failures, and some inquiry methods
574allowing information about the test itself to be gathered.
575
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000576Methods in the first group (running the test) are:
Fred Drake29be7012001-04-10 22:25:06 +0000577
578\begin{methoddesc}[TestCase]{setUp}{}
579 Method called to prepare the test fixture. This is called
580 immediately before calling the test method; any exception raised by
581 this method will be considered an error rather than a test failure.
582 The default implementation does nothing.
583\end{methoddesc}
584
Fred Drake29be7012001-04-10 22:25:06 +0000585\begin{methoddesc}[TestCase]{tearDown}{}
586 Method called immediately after the test method has been called and
587 the result recorded. This is called even if the test method raised
588 an exception, so the implementation in subclasses may need to be
589 particularly careful about checking internal state. Any exception
590 raised by this method will be considered an error rather than a test
Fred Drake62a26692001-04-12 19:34:38 +0000591 failure. This method will only be called if the \method{setUp()}
592 succeeds, regardless of the outcome of the test method.
593 The default implementation does nothing.
594\end{methoddesc}
595
596\begin{methoddesc}[TestCase]{run}{\optional{result}}
597 Run the test, collecting the result into the test result object
Fred Drakeae55d5f2003-12-31 04:34:50 +0000598 passed as \var{result}. If \var{result} is omitted or \constant{None},
Thomas Wouters89f507f2006-12-13 04:49:30 +0000599 a temporary result object is created (by calling the
600 \method{defaultTestCase()} method) and used; this result object is not
601 returned to \method{run()}'s caller.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000602
603 The same effect may be had by simply calling the \class{TestCase}
604 instance.
Fred Drake29be7012001-04-10 22:25:06 +0000605\end{methoddesc}
606
607\begin{methoddesc}[TestCase]{debug}{}
608 Run the test without collecting the result. This allows exceptions
Raymond Hettinger68804312005-01-01 00:28:46 +0000609 raised by the test to be propagated to the caller, and can be used
Fred Drake29be7012001-04-10 22:25:06 +0000610 to support running tests under a debugger.
611\end{methoddesc}
612
613
Fred Drake0056a422001-04-12 04:50:06 +0000614The test code can use any of the following methods to check for and
Fred Drake62a26692001-04-12 19:34:38 +0000615report failures.
Fred Drake29be7012001-04-10 22:25:06 +0000616
Fred Drake62a26692001-04-12 19:34:38 +0000617\begin{methoddesc}[TestCase]{assert_}{expr\optional{, msg}}
618\methodline{failUnless}{expr\optional{, msg}}
619 Signal a test failure if \var{expr} is false; the explanation for
620 the error will be \var{msg} if given, otherwise it will be
Fred Drakeae55d5f2003-12-31 04:34:50 +0000621 \constant{None}.
Fred Drake29be7012001-04-10 22:25:06 +0000622\end{methoddesc}
623
Fred Drake62a26692001-04-12 19:34:38 +0000624\begin{methoddesc}[TestCase]{assertEqual}{first, second\optional{, msg}}
625\methodline{failUnlessEqual}{first, second\optional{, msg}}
Fred Drake29be7012001-04-10 22:25:06 +0000626 Test that \var{first} and \var{second} are equal. If the values do
627 not compare equal, the test will fail with the explanation given by
Fred Drakeae55d5f2003-12-31 04:34:50 +0000628 \var{msg}, or \constant{None}. Note that using \method{failUnlessEqual()}
Fred Drake29be7012001-04-10 22:25:06 +0000629 improves upon doing the comparison as the first parameter to
Fred Drake62a26692001-04-12 19:34:38 +0000630 \method{failUnless()}: the default value for \var{msg} can be
Fred Drake29be7012001-04-10 22:25:06 +0000631 computed to include representations of both \var{first} and
632 \var{second}.
633\end{methoddesc}
634
Fred Drake62a26692001-04-12 19:34:38 +0000635\begin{methoddesc}[TestCase]{assertNotEqual}{first, second\optional{, msg}}
636\methodline{failIfEqual}{first, second\optional{, msg}}
Fred Drake29be7012001-04-10 22:25:06 +0000637 Test that \var{first} and \var{second} are not equal. If the values
638 do compare equal, the test will fail with the explanation given by
Fred Drakeae55d5f2003-12-31 04:34:50 +0000639 \var{msg}, or \constant{None}. Note that using \method{failIfEqual()}
Fred Drake29be7012001-04-10 22:25:06 +0000640 improves upon doing the comparison as the first parameter to
641 \method{failUnless()} is that the default value for \var{msg} can be
642 computed to include representations of both \var{first} and
643 \var{second}.
644\end{methoddesc}
645
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000646\begin{methoddesc}[TestCase]{assertAlmostEqual}{first, second\optional{,
647 places\optional{, msg}}}
648\methodline{failUnlessAlmostEqual}{first, second\optional{,
649 places\optional{, msg}}}
650 Test that \var{first} and \var{second} are approximately equal
651 by computing the difference, rounding to the given number of \var{places},
652 and comparing to zero. Note that comparing a given number of decimal places
653 is not the same as comparing a given number of significant digits.
654 If the values do not compare equal, the test will fail with the explanation
Fred Drakeae55d5f2003-12-31 04:34:50 +0000655 given by \var{msg}, or \constant{None}.
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000656\end{methoddesc}
657
658\begin{methoddesc}[TestCase]{assertNotAlmostEqual}{first, second\optional{,
659 places\optional{, msg}}}
660\methodline{failIfAlmostEqual}{first, second\optional{,
661 places\optional{, msg}}}
662 Test that \var{first} and \var{second} are not approximately equal
663 by computing the difference, rounding to the given number of \var{places},
664 and comparing to zero. Note that comparing a given number of decimal places
665 is not the same as comparing a given number of significant digits.
666 If the values do not compare equal, the test will fail with the explanation
Fred Drakeae55d5f2003-12-31 04:34:50 +0000667 given by \var{msg}, or \constant{None}.
Raymond Hettingerc7b07692002-12-29 17:59:24 +0000668\end{methoddesc}
669
Fred Drake62a26692001-04-12 19:34:38 +0000670\begin{methoddesc}[TestCase]{assertRaises}{exception, callable, \moreargs}
671\methodline{failUnlessRaises}{exception, callable, \moreargs}
672 Test that an exception is raised when \var{callable} is called with
673 any positional or keyword arguments that are also passed to
674 \method{assertRaises()}. The test passes if \var{exception} is
675 raised, is an error if another exception is raised, or fails if no
676 exception is raised. To catch any of a group of exceptions, a tuple
677 containing the exception classes may be passed as \var{exception}.
678\end{methoddesc}
679
Fred Drake29be7012001-04-10 22:25:06 +0000680\begin{methoddesc}[TestCase]{failIf}{expr\optional{, msg}}
Fred Drake0056a422001-04-12 04:50:06 +0000681 The inverse of the \method{failUnless()} method is the
Fred Drake62a26692001-04-12 19:34:38 +0000682 \method{failIf()} method. This signals a test failure if \var{expr}
Fred Drakeae55d5f2003-12-31 04:34:50 +0000683 is true, with \var{msg} or \constant{None} for the error message.
Fred Drake29be7012001-04-10 22:25:06 +0000684\end{methoddesc}
685
686\begin{methoddesc}[TestCase]{fail}{\optional{msg}}
Fred Drake62a26692001-04-12 19:34:38 +0000687 Signals a test failure unconditionally, with \var{msg} or
Fred Drakeae55d5f2003-12-31 04:34:50 +0000688 \constant{None} for the error message.
Fred Drake29be7012001-04-10 22:25:06 +0000689\end{methoddesc}
690
Fred Drake62a26692001-04-12 19:34:38 +0000691\begin{memberdesc}[TestCase]{failureException}
692 This class attribute gives the exception raised by the
693 \method{test()} method. If a test framework needs to use a
694 specialized exception, possibly to carry additional information, it
695 must subclass this exception in order to ``play fair'' with the
696 framework. The initial value of this attribute is
697 \exception{AssertionError}.
698\end{memberdesc}
699
Fred Drake29be7012001-04-10 22:25:06 +0000700
701Testing frameworks can use the following methods to collect
702information on the test:
703
704\begin{methoddesc}[TestCase]{countTestCases}{}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000705 Return the number of tests represented by this test object. For
706 \class{TestCase} instances, this will always be \code{1}.
Fred Drake29be7012001-04-10 22:25:06 +0000707\end{methoddesc}
708
709\begin{methoddesc}[TestCase]{defaultTestResult}{}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000710 Return an instance of the test result class that should be used
711 for this test case class (if no other result instance is provided
712 to the \method{run()} method).
713
714 For \class{TestCase} instances, this will always be an instance of
715 \class{TestResult}; subclasses of \class{TestCase} should
716 override this as necessary.
Fred Drake29be7012001-04-10 22:25:06 +0000717\end{methoddesc}
718
719\begin{methoddesc}[TestCase]{id}{}
720 Return a string identifying the specific test case. This is usually
721 the full name of the test method, including the module and class
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000722 name.
Fred Drake29be7012001-04-10 22:25:06 +0000723\end{methoddesc}
724
725\begin{methoddesc}[TestCase]{shortDescription}{}
Fred Drakeae55d5f2003-12-31 04:34:50 +0000726 Returns a one-line description of the test, or \constant{None} if no
Fred Drake29be7012001-04-10 22:25:06 +0000727 description has been provided. The default implementation of this
728 method returns the first line of the test method's docstring, if
Fred Drakeae55d5f2003-12-31 04:34:50 +0000729 available, or \constant{None}.
Fred Drake29be7012001-04-10 22:25:06 +0000730\end{methoddesc}
731
Fred Drakeb9ad2282001-04-07 05:41:39 +0000732
733\subsection{TestSuite Objects
734 \label{testsuite-objects}}
735
736\class{TestSuite} objects behave much like \class{TestCase} objects,
737except they do not actually implement a test. Instead, they are used
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000738to aggregate tests into groups of tests that should be run together.
739Some additional methods are available to add tests to \class{TestSuite}
Fred Drakeb9ad2282001-04-07 05:41:39 +0000740instances:
741
742\begin{methoddesc}[TestSuite]{addTest}{test}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000743 Add a \class{TestCase} or \class{TestSuite} to the suite.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000744\end{methoddesc}
745
746\begin{methoddesc}[TestSuite]{addTests}{tests}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000747 Add all the tests from an iterable of \class{TestCase} and
Fred Drakeb9ad2282001-04-07 05:41:39 +0000748 \class{TestSuite} instances to this test suite.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000749
750 This is equivalent to iterating over \var{tests}, calling
751 \method{addTest()} for each element.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000752\end{methoddesc}
753
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000754\class{TestSuite} shares the following methods with \class{TestCase}:
Fred Drake4d17b302001-09-06 15:51:56 +0000755
756\begin{methoddesc}[TestSuite]{run}{result}
757 Run the tests associated with this suite, collecting the result into
758 the test result object passed as \var{result}. Note that unlike
759 \method{TestCase.run()}, \method{TestSuite.run()} requires the
760 result object to be passed in.
761\end{methoddesc}
762
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000763\begin{methoddesc}[TestSuite]{debug}{}
764 Run the tests associated with this suite without collecting the result.
765 This allows exceptions raised by the test to be propagated to the caller
766 and can be used to support running tests under a debugger.
767\end{methoddesc}
768
769\begin{methoddesc}[TestSuite]{countTestCases}{}
770 Return the number of tests represented by this test object, including
771 all individual tests and sub-suites.
772\end{methoddesc}
773
Fred Drake4d17b302001-09-06 15:51:56 +0000774In the typical usage of a \class{TestSuite} object, the \method{run()}
775method is invoked by a \class{TestRunner} rather than by the end-user
776test harness.
777
Fred Drakeb9ad2282001-04-07 05:41:39 +0000778
779\subsection{TestResult Objects
780 \label{testresult-objects}}
781
782A \class{TestResult} object stores the results of a set of tests. The
783\class{TestCase} and \class{TestSuite} classes ensure that results are
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000784properly recorded; test authors do not need to worry about recording the
Fred Drakeb9ad2282001-04-07 05:41:39 +0000785outcome of tests.
786
787Testing frameworks built on top of \refmodule{unittest} may want
788access to the \class{TestResult} object generated by running a set of
789tests for reporting purposes; a \class{TestResult} instance is
790returned by the \method{TestRunner.run()} method for this purpose.
791
Fred Drakeb9ad2282001-04-07 05:41:39 +0000792\class{TestResult} instances have the following attributes that will
793be of interest when inspecting the results of running a set of tests:
794
795\begin{memberdesc}[TestResult]{errors}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000796 A list containing 2-tuples of \class{TestCase} instances and
Thomas Wouters89f507f2006-12-13 04:49:30 +0000797 strings holding formatted tracebacks. Each tuple represents a test which
798 raised an unexpected exception.
Fred Drakec4126172002-07-02 22:46:42 +0000799 \versionchanged[Contains formatted tracebacks instead of
800 \function{sys.exc_info()} results]{2.2}
Fred Drakeb9ad2282001-04-07 05:41:39 +0000801\end{memberdesc}
802
803\begin{memberdesc}[TestResult]{failures}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000804 A list containing 2-tuples of \class{TestCase} instances and strings
805 holding formatted tracebacks. Each tuple represents a test where a failure
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806 was explicitly signalled using the \method{TestCase.fail*()} or
807 \method{TestCase.assert*()} methods.
Fred Drakec4126172002-07-02 22:46:42 +0000808 \versionchanged[Contains formatted tracebacks instead of
809 \function{sys.exc_info()} results]{2.2}
Fred Drakeb9ad2282001-04-07 05:41:39 +0000810\end{memberdesc}
811
812\begin{memberdesc}[TestResult]{testsRun}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000813 The total number of tests run so far.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000814\end{memberdesc}
815
816\begin{methoddesc}[TestResult]{wasSuccessful}{}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000817 Returns \constant{True} if all tests run so far have passed,
818 otherwise returns \constant{False}.
819\end{methoddesc}
820
821\begin{methoddesc}[TestResult]{stop}{}
822 This method can be called to signal that the set of tests being run
823 should be aborted by setting the \class{TestResult}'s \code{shouldStop}
824 attribute to \constant{True}. \class{TestRunner} objects should respect
825 this flag and return without running any additional tests.
826
827 For example, this feature is used by the \class{TextTestRunner} class
828 to stop the test framework when the user signals an interrupt from
829 the keyboard. Interactive tools which provide \class{TestRunner}
830 implementations can use this in a similar manner.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000831\end{methoddesc}
832
833
834The following methods of the \class{TestResult} class are used to
Martin v. Löwis7bdc4842003-09-20 11:09:28 +0000835maintain the internal data structures, and may be extended in
Fred Drakeb9ad2282001-04-07 05:41:39 +0000836subclasses to support additional reporting requirements. This is
Fred Drake8ee679f2001-07-14 02:50:55 +0000837particularly useful in building tools which support interactive
Fred Drakeb9ad2282001-04-07 05:41:39 +0000838reporting while tests are being run.
839
840\begin{methoddesc}[TestResult]{startTest}{test}
841 Called when the test case \var{test} is about to be run.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000842
843 The default implementation simply increments the instance's
844 \code{testsRun} counter.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000845\end{methoddesc}
846
847\begin{methoddesc}[TestResult]{stopTest}{test}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000848 Called after the test case \var{test} has been executed, regardless
Fred Drakeb9ad2282001-04-07 05:41:39 +0000849 of the outcome.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000850
851 The default implementation does nothing.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000852\end{methoddesc}
853
854\begin{methoddesc}[TestResult]{addError}{test, err}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000855 Called when the test case \var{test} raises an unexpected exception
856 \var{err} is a tuple of the form returned by \function{sys.exc_info()}:
857 \code{(\var{type}, \var{value}, \var{traceback})}.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000858
859 The default implementation appends \code{(\var{test}, \var{err})} to
860 the instance's \code{errors} attribute.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000861\end{methoddesc}
862
863\begin{methoddesc}[TestResult]{addFailure}{test, err}
Fred Drake62a26692001-04-12 19:34:38 +0000864 Called when the test case \var{test} signals a failure.
Fred Drake0056a422001-04-12 04:50:06 +0000865 \var{err} is a tuple of the form returned by
866 \function{sys.exc_info()}: \code{(\var{type}, \var{value},
867 \var{traceback})}.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000868
869 The default implementation appends \code{(\var{test}, \var{err})} to
870 the instance's \code{failures} attribute.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000871\end{methoddesc}
872
873\begin{methoddesc}[TestResult]{addSuccess}{test}
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000874 Called when the test case \var{test} succeeds.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000875
876 The default implementation does nothing.
Fred Drakeb9ad2282001-04-07 05:41:39 +0000877\end{methoddesc}
878
879
Fred Drake62a26692001-04-12 19:34:38 +0000880
881\subsection{TestLoader Objects
882 \label{testloader-objects}}
883
884The \class{TestLoader} class is used to create test suites from
885classes and modules. Normally, there is no need to create an instance
886of this class; the \refmodule{unittest} module provides an instance
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000887that can be shared as \code{unittest.defaultTestLoader}.
888Using a subclass or instance, however, allows customization of some
Fred Drake62a26692001-04-12 19:34:38 +0000889configurable properties.
890
891\class{TestLoader} objects have the following methods:
892
893\begin{methoddesc}[TestLoader]{loadTestsFromTestCase}{testCaseClass}
894 Return a suite of all tests cases contained in the
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000895 \class{TestCase}-derived \class{testCaseClass}.
Fred Drake62a26692001-04-12 19:34:38 +0000896\end{methoddesc}
897
898\begin{methoddesc}[TestLoader]{loadTestsFromModule}{module}
899 Return a suite of all tests cases contained in the given module.
900 This method searches \var{module} for classes derived from
901 \class{TestCase} and creates an instance of the class for each test
902 method defined for the class.
903
Fred Drake0aa811c2001-10-20 04:24:09 +0000904 \warning{While using a hierarchy of
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000905 \class{TestCase}-derived classes can be convenient in sharing
Fred Drake62a26692001-04-12 19:34:38 +0000906 fixtures and helper functions, defining test methods on base classes
907 that are not intended to be instantiated directly does not play well
908 with this method. Doing so, however, can be useful when the
Fred Drake0aa811c2001-10-20 04:24:09 +0000909 fixtures are different and defined in subclasses.}
Fred Drake62a26692001-04-12 19:34:38 +0000910\end{methoddesc}
911
912\begin{methoddesc}[TestLoader]{loadTestsFromName}{name\optional{, module}}
913 Return a suite of all tests cases given a string specifier.
914
Fred Drake4d17b302001-09-06 15:51:56 +0000915 The specifier \var{name} is a ``dotted name'' that may resolve
Thomas Wouters89f507f2006-12-13 04:49:30 +0000916 either to a module, a test case class, a test method within a test
917 case class, a \class{TestSuite} instance, or a callable object which
918 returns a \class{TestCase} or \class{TestSuite} instance. These checks
919 are applied in the order listed here; that is, a method on a possible
920 test case class will be picked up as ``a test method within a test
921 case class'', rather than ``a callable object''.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000922
923 For example, if you have a module \module{SampleTests} containing a
924 \class{TestCase}-derived class \class{SampleTestCase} with three test
925 methods (\method{test_one()}, \method{test_two()}, and
926 \method{test_three()}), the specifier \code{'SampleTests.SampleTestCase'}
927 would cause this method to return a suite which will run all three test
928 methods. Using the specifier \code{'SampleTests.SampleTestCase.test_two'}
929 would cause it to return a test suite which will run only the
Fred Drake4d17b302001-09-06 15:51:56 +0000930 \method{test_two()} test method. The specifier can refer to modules
931 and packages which have not been imported; they will be imported as
932 a side-effect.
Fred Drake62a26692001-04-12 19:34:38 +0000933
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000934 The method optionally resolves \var{name} relative to the given
935 \var{module}.
Fred Drake62a26692001-04-12 19:34:38 +0000936\end{methoddesc}
937
938\begin{methoddesc}[TestLoader]{loadTestsFromNames}{names\optional{, module}}
939 Similar to \method{loadTestsFromName()}, but takes a sequence of
940 names rather than a single name. The return value is a test suite
941 which supports all the tests defined for each name.
942\end{methoddesc}
943
944\begin{methoddesc}[TestLoader]{getTestCaseNames}{testCaseClass}
945 Return a sorted sequence of method names found within
Thomas Wouters89f507f2006-12-13 04:49:30 +0000946 \var{testCaseClass}; this should be a subclass of \class{TestCase}.
Fred Drake62a26692001-04-12 19:34:38 +0000947\end{methoddesc}
948
949
950The following attributes of a \class{TestLoader} can be configured
951either by subclassing or assignment on an instance:
952
953\begin{memberdesc}[TestLoader]{testMethodPrefix}
954 String giving the prefix of method names which will be interpreted
955 as test methods. The default value is \code{'test'}.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000956
957 This affects \method{getTestCaseNames()} and all the
958 \method{loadTestsFrom*()} methods.
Fred Drake62a26692001-04-12 19:34:38 +0000959\end{memberdesc}
960
961\begin{memberdesc}[TestLoader]{sortTestMethodsUsing}
962 Function to be used to compare method names when sorting them in
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000963 \method{getTestCaseNames()} and all the \method{loadTestsFrom*()} methods.
964 The default value is the built-in \function{cmp()} function; the attribute
965 can also be set to \constant{None} to disable the sort.
Fred Drake62a26692001-04-12 19:34:38 +0000966\end{memberdesc}
967
968\begin{memberdesc}[TestLoader]{suiteClass}
969 Callable object that constructs a test suite from a list of tests.
970 No methods on the resulting object are needed. The default value is
971 the \class{TestSuite} class.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000972
973 This affects all the \method{loadTestsFrom*()} methods.
Fred Drake62a26692001-04-12 19:34:38 +0000974\end{memberdesc}