1 | \section{\module{unittest} ---
|
---|
2 | Unit testing framework}
|
---|
3 |
|
---|
4 | \declaremodule{standard}{unittest}
|
---|
5 | \modulesynopsis{Unit testing framework for Python.}
|
---|
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}
|
---|
9 | \sectionauthor{Raymond Hettinger}{python@rcn.com}
|
---|
10 |
|
---|
11 | \versionadded{2.1}
|
---|
12 |
|
---|
13 | The Python unit testing framework, sometimes referred to as ``PyUnit,'' is
|
---|
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 |
|
---|
19 | \module{unittest} supports test automation, sharing of setup and shutdown
|
---|
20 | code for tests, aggregation of tests into collections, and independence of
|
---|
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 |
|
---|
25 | To achieve this, \module{unittest} supports some important concepts:
|
---|
26 |
|
---|
27 | \begin{definitions}
|
---|
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 |
|
---|
34 | \term{test case}
|
---|
35 | A \dfn{test case} is the smallest unit of testing. It checks for a
|
---|
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.
|
---|
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 |
|
---|
53 | The test case and test fixture concepts are supported through the
|
---|
54 | \class{TestCase} and \class{FunctionTestCase} classes; the former
|
---|
55 | should be used when creating new tests, and the latter can be used when
|
---|
56 | integrating existing test code with a \module{unittest}-driven framework.
|
---|
57 | When building test fixtures using \class{TestCase}, the \method{setUp()}
|
---|
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
|
---|
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.
|
---|
82 |
|
---|
83 |
|
---|
84 | \begin{seealso}
|
---|
85 | \seemodule{doctest}{Another test-support module with a very
|
---|
86 | different flavor.}
|
---|
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 |
|
---|
94 | \subsection{Basic example \label{minimal-example}}
|
---|
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 |
|
---|
131 | A testcase is created by subclassing \class{unittest.TestCase}.
|
---|
132 | The three individual tests are defined with methods whose names start with
|
---|
133 | the letters \samp{test}. This naming convention informs the test runner
|
---|
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 |
|
---|
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:
|
---|
151 |
|
---|
152 | \begin{verbatim}
|
---|
153 | ...
|
---|
154 | ----------------------------------------------------------------------
|
---|
155 | Ran 3 tests in 0.000s
|
---|
156 |
|
---|
157 | OK
|
---|
158 | \end{verbatim}
|
---|
159 |
|
---|
160 | Instead of \function{unittest.main()}, there are other ways to run the tests
|
---|
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}
|
---|
166 | suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
|
---|
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 |
|
---|
189 | \subsection{Organizing test code
|
---|
190 | \label{organizing-tests}}
|
---|
191 |
|
---|
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
|
---|
194 | \module{unittest}, test cases are represented by instances of
|
---|
195 | \module{unittest}'s \class{TestCase} class. To make
|
---|
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 |
|
---|
207 | The simplest \class{TestCase} subclass will simply override the
|
---|
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):
|
---|
215 | widget = Widget('The widget')
|
---|
216 | self.assertEqual(widget.size(), (50, 50), 'incorrect default size')
|
---|
217 | \end{verbatim}
|
---|
218 |
|
---|
219 | Note that in order to test something, we use the one of the
|
---|
220 | \method{assert*()} or \method{fail*()} methods provided by the
|
---|
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.
|
---|
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
|
---|
238 | repetitive. In the above case, constructing a \class{Widget} in each of
|
---|
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):
|
---|
250 | self.widget = Widget('The widget')
|
---|
251 |
|
---|
252 | class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
|
---|
253 | def runTest(self):
|
---|
254 | self.failUnless(self.widget.size() == (50,50),
|
---|
255 | 'incorrect default size')
|
---|
256 |
|
---|
257 | class WidgetResizeTestCase(SimpleWidgetTestCase):
|
---|
258 | def runTest(self):
|
---|
259 | self.widget.resize(100,150)
|
---|
260 | self.failUnless(self.widget.size() == (100,150),
|
---|
261 | 'wrong size after resize')
|
---|
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):
|
---|
276 | self.widget = Widget('The widget')
|
---|
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
|
---|
284 | run whether \method{runTest()} succeeded or not.
|
---|
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
|
---|
293 | discouraging, so in the same vein as JUnit, \module{unittest} provides
|
---|
294 | a simpler mechanism:
|
---|
295 |
|
---|
296 | \begin{verbatim}
|
---|
297 | import unittest
|
---|
298 |
|
---|
299 | class WidgetTestCase(unittest.TestCase):
|
---|
300 | def setUp(self):
|
---|
301 | self.widget = Widget('The widget')
|
---|
302 |
|
---|
303 | def tearDown(self):
|
---|
304 | self.widget.dispose()
|
---|
305 | self.widget = None
|
---|
306 |
|
---|
307 | def testDefaultSize(self):
|
---|
308 | self.failUnless(self.widget.size() == (50,50),
|
---|
309 | 'incorrect default size')
|
---|
310 |
|
---|
311 | def testResize(self):
|
---|
312 | self.widget.resize(100,150)
|
---|
313 | self.failUnless(self.widget.size() == (100,150),
|
---|
314 | 'wrong size after resize')
|
---|
315 | \end{verbatim}
|
---|
316 |
|
---|
317 | Here we have not provided a \method{runTest()} method, but have
|
---|
318 | instead provided two different test methods. Class instances will now
|
---|
319 | each run one of the \method{test*()} methods, with \code{self.widget}
|
---|
320 | created and destroyed separately for each instance. When creating an
|
---|
321 | instance we must specify the test method it is to run. We do this by
|
---|
322 | passing the method name in the constructor:
|
---|
323 |
|
---|
324 | \begin{verbatim}
|
---|
325 | defaultSizeTestCase = WidgetTestCase('testDefaultSize')
|
---|
326 | resizeTestCase = WidgetTestCase('testResize')
|
---|
327 | \end{verbatim}
|
---|
328 |
|
---|
329 | Test case instances are grouped together according to the features
|
---|
330 | they test. \module{unittest} provides a mechanism for this: the
|
---|
331 | \dfn{test suite}, represented by \module{unittest}'s \class{TestSuite}
|
---|
332 | class:
|
---|
333 |
|
---|
334 | \begin{verbatim}
|
---|
335 | widgetTestSuite = unittest.TestSuite()
|
---|
336 | widgetTestSuite.addTest(WidgetTestCase('testDefaultSize'))
|
---|
337 | widgetTestSuite.addTest(WidgetTestCase('testResize'))
|
---|
338 | \end{verbatim}
|
---|
339 |
|
---|
340 | For the ease of running tests, as we will see later, it is a good
|
---|
341 | idea to provide in each test module a callable object that returns a
|
---|
342 | pre-built test suite:
|
---|
343 |
|
---|
344 | \begin{verbatim}
|
---|
345 | def suite():
|
---|
346 | suite = unittest.TestSuite()
|
---|
347 | suite.addTest(WidgetTestCase('testDefaultSize'))
|
---|
348 | suite.addTest(WidgetTestCase('testResize'))
|
---|
349 | return suite
|
---|
350 | \end{verbatim}
|
---|
351 |
|
---|
352 | or even:
|
---|
353 |
|
---|
354 | \begin{verbatim}
|
---|
355 | def suite():
|
---|
356 | tests = ['testDefaultSize', 'testResize']
|
---|
357 |
|
---|
358 | return unittest.TestSuite(map(WidgetTestCase, tests))
|
---|
359 | \end{verbatim}
|
---|
360 |
|
---|
361 | Since it is a common pattern to create a \class{TestCase} subclass
|
---|
362 | with many similarly named test functions, \module{unittest} provides a
|
---|
363 | \class{TestLoader} class that can be used to automate the process of
|
---|
364 | creating a test suite and populating it with individual tests.
|
---|
365 | For example,
|
---|
366 |
|
---|
367 | \begin{verbatim}
|
---|
368 | suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
|
---|
369 | \end{verbatim}
|
---|
370 |
|
---|
371 | will 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
|
---|
374 | test methods automatically.
|
---|
375 |
|
---|
376 | Note that the order in which the various test cases will be run is
|
---|
377 | determined by sorting the test function names with the built-in
|
---|
378 | \function{cmp()} function.
|
---|
379 |
|
---|
380 | Often it is desirable to group suites of test cases together, so as to
|
---|
381 | run tests for the whole system at once. This is easy, since
|
---|
382 | \class{TestSuite} instances can be added to a \class{TestSuite} just
|
---|
383 | as \class{TestCase} instances can be added to a \class{TestSuite}:
|
---|
384 |
|
---|
385 | \begin{verbatim}
|
---|
386 | suite1 = module1.TheTestSuite()
|
---|
387 | suite2 = module2.TheTestSuite()
|
---|
388 | alltests = unittest.TestSuite([suite1, suite2])
|
---|
389 | \end{verbatim}
|
---|
390 |
|
---|
391 | You can place the definitions of test cases and test suites in the
|
---|
392 | same modules as the code they are to test (such as \file{widget.py}),
|
---|
393 | but there are several advantages to placing the test code in a
|
---|
394 | separate module, such as \file{test_widget.py}:
|
---|
395 |
|
---|
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.
|
---|
399 | \item There is less temptation to change test code to fit the code
|
---|
400 | 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 |
|
---|
410 |
|
---|
411 | \subsection{Re-using old test code
|
---|
412 | \label{legacy-unit-tests}}
|
---|
413 |
|
---|
414 | Some users will find that they have existing test code that they would
|
---|
415 | like to run from \module{unittest}, without converting every old test
|
---|
416 | function to a \class{TestCase} subclass.
|
---|
417 |
|
---|
418 | For this reason, \module{unittest} provides a \class{FunctionTestCase}
|
---|
419 | class. This subclass of \class{TestCase} can be used to wrap an existing
|
---|
420 | test function. Set-up and tear-down functions can also be provided.
|
---|
421 |
|
---|
422 | Given the following test function:
|
---|
423 |
|
---|
424 | \begin{verbatim}
|
---|
425 | def testSomething():
|
---|
426 | something = makeSomething()
|
---|
427 | assert something.name is not None
|
---|
428 | # ...
|
---|
429 | \end{verbatim}
|
---|
430 |
|
---|
431 | one can create an equivalent test case instance as follows:
|
---|
432 |
|
---|
433 | \begin{verbatim}
|
---|
434 | testcase = unittest.FunctionTestCase(testSomething)
|
---|
435 | \end{verbatim}
|
---|
436 |
|
---|
437 | If there are additional set-up and tear-down methods that should be
|
---|
438 | called as part of the test case's operation, they can also be provided
|
---|
439 | like so:
|
---|
440 |
|
---|
441 | \begin{verbatim}
|
---|
442 | testcase = unittest.FunctionTestCase(testSomething,
|
---|
443 | setUp=makeSomethingDB,
|
---|
444 | tearDown=deleteSomethingDB)
|
---|
445 | \end{verbatim}
|
---|
446 |
|
---|
447 | To make migrating existing test suites easier, \module{unittest}
|
---|
448 | supports tests raising \exception{AssertionError} to indicate test failure.
|
---|
449 | However, it is recommended that you use the explicit
|
---|
450 | \method{TestCase.fail*()} and \method{TestCase.assert*()} methods instead,
|
---|
451 | as future versions of \module{unittest} may treat \exception{AssertionError}
|
---|
452 | differently.
|
---|
453 |
|
---|
454 | \note{Even though \class{FunctionTestCase} can be used to quickly convert
|
---|
455 | an existing test base over to a \module{unittest}-based system, this
|
---|
456 | approach is not recommended. Taking the time to set up proper
|
---|
457 | \class{TestCase} subclasses will make future test refactorings infinitely
|
---|
458 | easier.}
|
---|
459 |
|
---|
460 |
|
---|
461 |
|
---|
462 | \subsection{Classes and functions
|
---|
463 | \label{unittest-contents}}
|
---|
464 |
|
---|
465 | \begin{classdesc}{TestCase}{\optional{methodName}}
|
---|
466 | Instances of the \class{TestCase} class represent the smallest
|
---|
467 | 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.
|
---|
473 |
|
---|
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'}.
|
---|
490 | \end{classdesc}
|
---|
491 |
|
---|
492 | \begin{classdesc}{FunctionTestCase}{testFunc\optional{,
|
---|
493 | setUp\optional{, tearDown\optional{, description}}}}
|
---|
494 | 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 |
|
---|
502 | \begin{classdesc}{TestSuite}{\optional{tests}}
|
---|
503 | This class represents an aggregation of individual tests cases and
|
---|
504 | test suites. The class presents the interface needed by the test
|
---|
505 | 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.
|
---|
513 | \end{classdesc}
|
---|
514 |
|
---|
515 | \begin{classdesc}{TestLoader}{}
|
---|
516 | 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}
|
---|
519 | subclass.
|
---|
520 | \end{classdesc}
|
---|
521 |
|
---|
522 | \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 |
|
---|
527 | \begin{datadesc}{defaultTestLoader}
|
---|
528 | Instance of the \class{TestLoader} class intended to be shared. If no
|
---|
529 | customization of the \class{TestLoader} is needed, this instance can
|
---|
530 | be used instead of repeatedly creating new instances.
|
---|
531 | \end{datadesc}
|
---|
532 |
|
---|
533 | \begin{classdesc}{TextTestRunner}{\optional{stream\optional{,
|
---|
534 | descriptions\optional{, verbosity}}}}
|
---|
535 | A basic test runner implementation which prints results on standard
|
---|
536 | error. It has a few configurable parameters, but is essentially
|
---|
537 | very simple. Graphical applications which run test suites should
|
---|
538 | provide alternate implementations.
|
---|
539 | \end{classdesc}
|
---|
540 |
|
---|
541 | \begin{funcdesc}{main}{\optional{module\optional{,
|
---|
542 | defaultTest\optional{, argv\optional{,
|
---|
543 | testRunner\optional{, testRunner}}}}}}
|
---|
544 | A command-line program that runs a set of tests; this is primarily
|
---|
545 | for making test modules conveniently executable. The simplest use
|
---|
546 | for this function is to include the following line at the end of a
|
---|
547 | test script:
|
---|
548 |
|
---|
549 | \begin{verbatim}
|
---|
550 | if __name__ == '__main__':
|
---|
551 | unittest.main()
|
---|
552 | \end{verbatim}
|
---|
553 | \end{funcdesc}
|
---|
554 |
|
---|
555 | In some cases, the existing tests may have been written using the
|
---|
556 | \refmodule{doctest} module. If so, that module provides a
|
---|
557 | \class{DocTestSuite} class that can automatically build
|
---|
558 | \class{unittest.TestSuite} instances from the existing
|
---|
559 | \module{doctest}-based tests.
|
---|
560 | \versionadded{2.3}
|
---|
561 |
|
---|
562 |
|
---|
563 | \subsection{TestCase Objects
|
---|
564 | \label{testcase-objects}}
|
---|
565 |
|
---|
566 | Each \class{TestCase} instance represents a single test, but each
|
---|
567 | concrete subclass may be used to define multiple tests --- the
|
---|
568 | concrete class represents a single test fixture. The fixture is
|
---|
569 | created and cleaned up for each test case.
|
---|
570 |
|
---|
571 | \class{TestCase} instances provide three groups of methods: one group
|
---|
572 | used to run the test, another used by the test implementation to
|
---|
573 | check conditions and report failures, and some inquiry methods
|
---|
574 | allowing information about the test itself to be gathered.
|
---|
575 |
|
---|
576 | Methods in the first group (running the test) are:
|
---|
577 |
|
---|
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 |
|
---|
585 | \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
|
---|
591 | 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
|
---|
598 | passed as \var{result}. If \var{result} is omitted or \constant{None},
|
---|
599 | 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.
|
---|
602 |
|
---|
603 | The same effect may be had by simply calling the \class{TestCase}
|
---|
604 | instance.
|
---|
605 | \end{methoddesc}
|
---|
606 |
|
---|
607 | \begin{methoddesc}[TestCase]{debug}{}
|
---|
608 | Run the test without collecting the result. This allows exceptions
|
---|
609 | raised by the test to be propagated to the caller, and can be used
|
---|
610 | to support running tests under a debugger.
|
---|
611 | \end{methoddesc}
|
---|
612 |
|
---|
613 |
|
---|
614 | The test code can use any of the following methods to check for and
|
---|
615 | report failures.
|
---|
616 |
|
---|
617 | \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
|
---|
621 | \constant{None}.
|
---|
622 | \end{methoddesc}
|
---|
623 |
|
---|
624 | \begin{methoddesc}[TestCase]{assertEqual}{first, second\optional{, msg}}
|
---|
625 | \methodline{failUnlessEqual}{first, second\optional{, msg}}
|
---|
626 | 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
|
---|
628 | \var{msg}, or \constant{None}. Note that using \method{failUnlessEqual()}
|
---|
629 | improves upon doing the comparison as the first parameter to
|
---|
630 | \method{failUnless()}: the default value for \var{msg} can be
|
---|
631 | computed to include representations of both \var{first} and
|
---|
632 | \var{second}.
|
---|
633 | \end{methoddesc}
|
---|
634 |
|
---|
635 | \begin{methoddesc}[TestCase]{assertNotEqual}{first, second\optional{, msg}}
|
---|
636 | \methodline{failIfEqual}{first, second\optional{, msg}}
|
---|
637 | 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
|
---|
639 | \var{msg}, or \constant{None}. Note that using \method{failIfEqual()}
|
---|
640 | 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 |
|
---|
646 | \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
|
---|
655 | given by \var{msg}, or \constant{None}.
|
---|
656 | \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
|
---|
667 | given by \var{msg}, or \constant{None}.
|
---|
668 | \end{methoddesc}
|
---|
669 |
|
---|
670 | \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 |
|
---|
680 | \begin{methoddesc}[TestCase]{failIf}{expr\optional{, msg}}
|
---|
681 | The inverse of the \method{failUnless()} method is the
|
---|
682 | \method{failIf()} method. This signals a test failure if \var{expr}
|
---|
683 | is true, with \var{msg} or \constant{None} for the error message.
|
---|
684 | \end{methoddesc}
|
---|
685 |
|
---|
686 | \begin{methoddesc}[TestCase]{fail}{\optional{msg}}
|
---|
687 | Signals a test failure unconditionally, with \var{msg} or
|
---|
688 | \constant{None} for the error message.
|
---|
689 | \end{methoddesc}
|
---|
690 |
|
---|
691 | \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 |
|
---|
700 |
|
---|
701 | Testing frameworks can use the following methods to collect
|
---|
702 | information on the test:
|
---|
703 |
|
---|
704 | \begin{methoddesc}[TestCase]{countTestCases}{}
|
---|
705 | Return the number of tests represented by this test object. For
|
---|
706 | \class{TestCase} instances, this will always be \code{1}.
|
---|
707 | \end{methoddesc}
|
---|
708 |
|
---|
709 | \begin{methoddesc}[TestCase]{defaultTestResult}{}
|
---|
710 | 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.
|
---|
717 | \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
|
---|
722 | name.
|
---|
723 | \end{methoddesc}
|
---|
724 |
|
---|
725 | \begin{methoddesc}[TestCase]{shortDescription}{}
|
---|
726 | Returns a one-line description of the test, or \constant{None} if no
|
---|
727 | description has been provided. The default implementation of this
|
---|
728 | method returns the first line of the test method's docstring, if
|
---|
729 | available, or \constant{None}.
|
---|
730 | \end{methoddesc}
|
---|
731 |
|
---|
732 |
|
---|
733 | \subsection{TestSuite Objects
|
---|
734 | \label{testsuite-objects}}
|
---|
735 |
|
---|
736 | \class{TestSuite} objects behave much like \class{TestCase} objects,
|
---|
737 | except they do not actually implement a test. Instead, they are used
|
---|
738 | to aggregate tests into groups of tests that should be run together.
|
---|
739 | Some additional methods are available to add tests to \class{TestSuite}
|
---|
740 | instances:
|
---|
741 |
|
---|
742 | \begin{methoddesc}[TestSuite]{addTest}{test}
|
---|
743 | Add a \class{TestCase} or \class{TestSuite} to the suite.
|
---|
744 | \end{methoddesc}
|
---|
745 |
|
---|
746 | \begin{methoddesc}[TestSuite]{addTests}{tests}
|
---|
747 | Add all the tests from an iterable of \class{TestCase} and
|
---|
748 | \class{TestSuite} instances to this test suite.
|
---|
749 |
|
---|
750 | This is equivalent to iterating over \var{tests}, calling
|
---|
751 | \method{addTest()} for each element.
|
---|
752 | \end{methoddesc}
|
---|
753 |
|
---|
754 | \class{TestSuite} shares the following methods with \class{TestCase}:
|
---|
755 |
|
---|
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 |
|
---|
763 | \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 |
|
---|
774 | In the typical usage of a \class{TestSuite} object, the \method{run()}
|
---|
775 | method is invoked by a \class{TestRunner} rather than by the end-user
|
---|
776 | test harness.
|
---|
777 |
|
---|
778 |
|
---|
779 | \subsection{TestResult Objects
|
---|
780 | \label{testresult-objects}}
|
---|
781 |
|
---|
782 | A \class{TestResult} object stores the results of a set of tests. The
|
---|
783 | \class{TestCase} and \class{TestSuite} classes ensure that results are
|
---|
784 | properly recorded; test authors do not need to worry about recording the
|
---|
785 | outcome of tests.
|
---|
786 |
|
---|
787 | Testing frameworks built on top of \refmodule{unittest} may want
|
---|
788 | access to the \class{TestResult} object generated by running a set of
|
---|
789 | tests for reporting purposes; a \class{TestResult} instance is
|
---|
790 | returned by the \method{TestRunner.run()} method for this purpose.
|
---|
791 |
|
---|
792 | \class{TestResult} instances have the following attributes that will
|
---|
793 | be of interest when inspecting the results of running a set of tests:
|
---|
794 |
|
---|
795 | \begin{memberdesc}[TestResult]{errors}
|
---|
796 | A list containing 2-tuples of \class{TestCase} instances and
|
---|
797 | strings holding formatted tracebacks. Each tuple represents a test which
|
---|
798 | raised an unexpected exception.
|
---|
799 | \versionchanged[Contains formatted tracebacks instead of
|
---|
800 | \function{sys.exc_info()} results]{2.2}
|
---|
801 | \end{memberdesc}
|
---|
802 |
|
---|
803 | \begin{memberdesc}[TestResult]{failures}
|
---|
804 | A list containing 2-tuples of \class{TestCase} instances and strings
|
---|
805 | holding formatted tracebacks. Each tuple represents a test where a failure
|
---|
806 | was explicitly signalled using the \method{TestCase.fail*()} or
|
---|
807 | \method{TestCase.assert*()} methods.
|
---|
808 | \versionchanged[Contains formatted tracebacks instead of
|
---|
809 | \function{sys.exc_info()} results]{2.2}
|
---|
810 | \end{memberdesc}
|
---|
811 |
|
---|
812 | \begin{memberdesc}[TestResult]{testsRun}
|
---|
813 | The total number of tests run so far.
|
---|
814 | \end{memberdesc}
|
---|
815 |
|
---|
816 | \begin{methoddesc}[TestResult]{wasSuccessful}{}
|
---|
817 | 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.
|
---|
831 | \end{methoddesc}
|
---|
832 |
|
---|
833 |
|
---|
834 | The following methods of the \class{TestResult} class are used to
|
---|
835 | maintain the internal data structures, and may be extended in
|
---|
836 | subclasses to support additional reporting requirements. This is
|
---|
837 | particularly useful in building tools which support interactive
|
---|
838 | reporting 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.
|
---|
842 |
|
---|
843 | The default implementation simply increments the instance's
|
---|
844 | \code{testsRun} counter.
|
---|
845 | \end{methoddesc}
|
---|
846 |
|
---|
847 | \begin{methoddesc}[TestResult]{stopTest}{test}
|
---|
848 | Called after the test case \var{test} has been executed, regardless
|
---|
849 | of the outcome.
|
---|
850 |
|
---|
851 | The default implementation does nothing.
|
---|
852 | \end{methoddesc}
|
---|
853 |
|
---|
854 | \begin{methoddesc}[TestResult]{addError}{test, err}
|
---|
855 | 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})}.
|
---|
858 |
|
---|
859 | The default implementation appends \code{(\var{test}, \var{err})} to
|
---|
860 | the instance's \code{errors} attribute.
|
---|
861 | \end{methoddesc}
|
---|
862 |
|
---|
863 | \begin{methoddesc}[TestResult]{addFailure}{test, err}
|
---|
864 | Called when the test case \var{test} signals a failure.
|
---|
865 | \var{err} is a tuple of the form returned by
|
---|
866 | \function{sys.exc_info()}: \code{(\var{type}, \var{value},
|
---|
867 | \var{traceback})}.
|
---|
868 |
|
---|
869 | The default implementation appends \code{(\var{test}, \var{err})} to
|
---|
870 | the instance's \code{failures} attribute.
|
---|
871 | \end{methoddesc}
|
---|
872 |
|
---|
873 | \begin{methoddesc}[TestResult]{addSuccess}{test}
|
---|
874 | Called when the test case \var{test} succeeds.
|
---|
875 |
|
---|
876 | The default implementation does nothing.
|
---|
877 | \end{methoddesc}
|
---|
878 |
|
---|
879 |
|
---|
880 |
|
---|
881 | \subsection{TestLoader Objects
|
---|
882 | \label{testloader-objects}}
|
---|
883 |
|
---|
884 | The \class{TestLoader} class is used to create test suites from
|
---|
885 | classes and modules. Normally, there is no need to create an instance
|
---|
886 | of this class; the \refmodule{unittest} module provides an instance
|
---|
887 | that can be shared as \code{unittest.defaultTestLoader}.
|
---|
888 | Using a subclass or instance, however, allows customization of some
|
---|
889 | configurable 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
|
---|
895 | \class{TestCase}-derived \class{testCaseClass}.
|
---|
896 | \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 |
|
---|
904 | \warning{While using a hierarchy of
|
---|
905 | \class{TestCase}-derived classes can be convenient in sharing
|
---|
906 | 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
|
---|
909 | fixtures are different and defined in subclasses.}
|
---|
910 | \end{methoddesc}
|
---|
911 |
|
---|
912 | \begin{methoddesc}[TestLoader]{loadTestsFromName}{name\optional{, module}}
|
---|
913 | Return a suite of all tests cases given a string specifier.
|
---|
914 |
|
---|
915 | The specifier \var{name} is a ``dotted name'' that may resolve
|
---|
916 | 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''.
|
---|
922 |
|
---|
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
|
---|
930 | \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.
|
---|
933 |
|
---|
934 | The method optionally resolves \var{name} relative to the given
|
---|
935 | \var{module}.
|
---|
936 | \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
|
---|
946 | \var{testCaseClass}; this should be a subclass of \class{TestCase}.
|
---|
947 | \end{methoddesc}
|
---|
948 |
|
---|
949 |
|
---|
950 | The following attributes of a \class{TestLoader} can be configured
|
---|
951 | either 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'}.
|
---|
956 |
|
---|
957 | This affects \method{getTestCaseNames()} and all the
|
---|
958 | \method{loadTestsFrom*()} methods.
|
---|
959 | \end{memberdesc}
|
---|
960 |
|
---|
961 | \begin{memberdesc}[TestLoader]{sortTestMethodsUsing}
|
---|
962 | Function to be used to compare method names when sorting them in
|
---|
963 | \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.
|
---|
966 | \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.
|
---|
972 |
|
---|
973 | This affects all the \method{loadTestsFrom*()} methods.
|
---|
974 | \end{memberdesc}
|
---|