~bzr/ubuntu/natty/python-testtools/bzr-ppa

« back to all changes in this revision

Viewing changes to MANUAL

  • Committer: Robert Collins
  • Date: 2010-11-14 15:49:58 UTC
  • mfrom: (16.11.4 upstream)
  • Revision ID: robertc@robertcollins.net-20101114154958-lwb16rdhehq6q020
New snapshot for testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
Extensions to TestCase
12
12
----------------------
13
13
 
14
 
Controlling test execution
15
 
~~~~~~~~~~~~~~~~~~~~~~~~~~
 
14
Custom exception handling
 
15
~~~~~~~~~~~~~~~~~~~~~~~~~
16
16
 
17
 
Testtools supports two ways to control how tests are executed. The simplest
18
 
is to add a new exception to self.exception_handlers::
 
17
testtools provides a way to control how test exceptions are handled.  To do
 
18
this, add a new exception to self.exception_handlers on a TestCase.  For
 
19
example::
19
20
 
20
21
    >>> self.exception_handlers.insert(-1, (ExceptionClass, handler)).
21
22
 
23
24
ExceptionClass, handler will be called with the test case, test result and the
24
25
raised exception.
25
26
 
26
 
Secondly, by overriding __init__ to pass in runTest=RunTestFactory the whole
27
 
execution of the test can be altered. The default is testtools.runtest.RunTest
28
 
and calls  case._run_setup, case._run_test_method and finally
29
 
case._run_teardown. Other methods to control what RunTest is used may be
30
 
added in future.
31
 
 
 
27
Controlling test execution
 
28
~~~~~~~~~~~~~~~~~~~~~~~~~~
 
29
 
 
30
If you want to control more than just how exceptions are raised, you can
 
31
provide a custom `RunTest` to a TestCase.  The `RunTest` object can change
 
32
everything about how the test executes.
 
33
 
 
34
To work with `testtools.TestCase`, a `RunTest` must have a factory that takes
 
35
a test and an optional list of exception handlers.  Instances returned by the
 
36
factory must have a `run()` method that takes an optional `TestResult` object.
 
37
 
 
38
The default is `testtools.runtest.RunTest` and calls 'setUp', the test method
 
39
and 'tearDown' in the normal, vanilla way that Python's standard unittest
 
40
does.
 
41
 
 
42
To specify a `RunTest` for all the tests in a `TestCase` class, do something
 
43
like this::
 
44
 
 
45
  class SomeTests(TestCase):
 
46
      run_tests_with = CustomRunTestFactory
 
47
 
 
48
To specify a `RunTest` for a specific test in a `TestCase` class, do::
 
49
 
 
50
  class SomeTests(TestCase):
 
51
      @run_test_with(CustomRunTestFactory, extra_arg=42, foo='whatever')
 
52
      def test_something(self):
 
53
          pass
 
54
 
 
55
In addition, either of these can be overridden by passing a factory in to the
 
56
`TestCase` constructor with the optional 'runTest' argument.
32
57
 
33
58
TestCase.addCleanup
34
59
~~~~~~~~~~~~~~~~~~~
42
67
        self.addCleanup(foo.unlock)
43
68
        ...
44
69
 
 
70
Cleanups can also report multiple errors, if appropriate by wrapping them in
 
71
a testtools.MultipleExceptions object::
 
72
 
 
73
    raise MultipleExceptions(exc_info1, exc_info2)
 
74
 
45
75
 
46
76
TestCase.addOnException
47
77
~~~~~~~~~~~~~~~~~~~~~~~
52
82
more data (via the addDetails API) and potentially other uses.
53
83
 
54
84
 
 
85
TestCase.patch
 
86
~~~~~~~~~~~~~~
 
87
 
 
88
``patch`` is a convenient way to monkey-patch a Python object for the duration
 
89
of your test.  It's especially useful for testing legacy code.  e.g.::
 
90
 
 
91
    def test_foo(self):
 
92
        my_stream = StringIO()
 
93
        self.patch(sys, 'stderr', my_stream)
 
94
        run_some_code_that_prints_to_stderr()
 
95
        self.assertEqual('', my_stream.getvalue())
 
96
 
 
97
The call to ``patch`` above masks sys.stderr with 'my_stream' so that anything
 
98
printed to stderr will be captured in a StringIO variable that can be actually
 
99
tested. Once the test is done, the real sys.stderr is restored to its rightful
 
100
place.
 
101
 
 
102
 
55
103
TestCase.skipTest
56
104
~~~~~~~~~~~~~~~~~
57
105
 
68
116
``skipTest`` support, the ``skip`` name is now deprecated (but no warning
69
117
is emitted yet - some time in the future we may do so).
70
118
 
 
119
TestCase.useFixture
 
120
~~~~~~~~~~~~~~~~~~~
 
121
 
 
122
``useFixture(fixture)`` calls setUp on the fixture, schedules a cleanup to 
 
123
clean it up, and schedules a cleanup to attach all details held by the 
 
124
fixture to the details dict of the test case. The fixture object should meet
 
125
the ``fixtures.Fixture`` protocol (version 0.3.4 or newer). This is useful
 
126
for moving code out of setUp and tearDown methods and into composable side
 
127
classes.
 
128
 
71
129
 
72
130
New assertion methods
73
131
~~~~~~~~~~~~~~~~~~~~~
92
150
        self.assertEqual('bob', error.username)
93
151
        self.assertEqual('User bob cannot frobnicate', str(error))
94
152
 
 
153
Note that this is incompatible with the assertRaises in unittest2/Python2.7.
 
154
While we have no immediate plans to change to be compatible consider using the
 
155
new assertThat facility instead::
 
156
 
 
157
        self.assertThat(
 
158
            lambda: thing.frobnicate('foo', 'bar'),
 
159
            Raises(MatchesException(UnauthorisedError('bob')))
 
160
 
 
161
There is also a convenience function to handle this common case::
 
162
 
 
163
        self.assertThat(
 
164
            lambda: thing.frobnicate('foo', 'bar'),
 
165
            raises(UnauthorisedError('bob')))
 
166
 
95
167
 
96
168
TestCase.assertThat
97
169
~~~~~~~~~~~~~~~~~~~
211
283
Running tests
212
284
-------------
213
285
 
214
 
Testtools provides a convenient way to run a test suite using the testtools
 
286
testtools provides a convenient way to run a test suite using the testtools
215
287
result object: python -m testtools.run testspec [testspec...].
216
288
 
 
289
To run tests with Python 2.4, you'll have to do something like:
 
290
  python2.4 /path/to/testtools/run.py testspec [testspec ...].
 
291
 
 
292
 
217
293
Test discovery
218
294
--------------
219
295
 
220
 
Testtools includes a backported version of the Python 2.7 glue for using the
 
296
testtools includes a backported version of the Python 2.7 glue for using the
221
297
discover test discovery module. If you either have Python 2.7/3.1 or newer, or
222
298
install the 'discover' module, then you can invoke discovery::
223
299
 
226
302
For more information see the Python 2.7 unittest documentation, or::
227
303
 
228
304
    python -m testtools.run --help
 
305
 
 
306
 
 
307
Twisted support
 
308
---------------
 
309
 
 
310
Support for running Twisted tests is very experimental right now.  You
 
311
shouldn't really do it.  However, if you are going to, here are some tips for
 
312
converting your Trial tests into testtools tests.
 
313
 
 
314
 * Use the AsynchronousDeferredRunTest runner
 
315
 * Make sure to upcall to setUp and tearDown
 
316
 * Don't use setUpClass or tearDownClass
 
317
 * Don't expect setting .todo, .timeout or .skip attributes to do anything
 
318
 * flushLoggedErrors is not there for you.  Sorry.
 
319
 * assertFailure is not there for you.  Even more sorry.