Improve manual for RunTest handling.

This commit is contained in:
Jonathan Lange
2010-10-17 17:09:19 +01:00
parent 863cade82b
commit a3fd2583d4
2 changed files with 37 additions and 11 deletions

43
MANUAL
View File

@@ -11,11 +11,12 @@ to the API docs (i.e. docstrings) for full details on a particular feature.
Extensions to TestCase
----------------------
Controlling test execution
~~~~~~~~~~~~~~~~~~~~~~~~~~
Custom exception handling
~~~~~~~~~~~~~~~~~~~~~~~~~
Testtools supports two ways to control how tests are executed. The simplest
is to add a new exception to self.exception_handlers::
testtools provides a way to control how test exceptions are handled. To do
this, add a new exception to self.exception_handlers on a TestCase. For
example::
>>> self.exception_handlers.insert(-1, (ExceptionClass, handler)).
@@ -23,12 +24,36 @@ Having done this, if any of setUp, tearDown, or the test method raise
ExceptionClass, handler will be called with the test case, test result and the
raised exception.
Secondly, by overriding __init__ to pass in runTest=RunTestFactory the whole
execution of the test can be altered. The default is testtools.runtest.RunTest
and calls case._run_setup, case._run_test_method and finally
case._run_teardown. Other methods to control what RunTest is used may be
added in future.
Controlling test execution
~~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to control more than just how exceptions are raised, you can
provide a custom `RunTest` to a TestCase. The `RunTest` object can change
everything about how the test executes.
To work with `testtools.TestCase`, a `RunTest` must have a factory that takes
a test and an optional list of exception handlers. Instances returned by the
factory must have a `run()` method that takes an optional `TestResult` object.
The default is `testtools.runtest.RunTest` and calls 'setUp', the test method
and 'tearDown' in the normal, vanilla way that Python's standard unittest
does.
To specify a `RunTest` for all the tests in a `TestCase` class, do something
like this::
class SomeTests(TestCase):
run_tests_with = CustomRunTestFactory
To specify a `RunTest` for a specific test in a `TestCase` class, do::
class SomeTests(TestCase):
@run_test_with(CustomRunTestFactory, extra_arg=42, foo='whatever')
def test_something(self):
pass
In addition, either of these can be overridden by passing a factory in to the
`TestCase` constructor with the optional 'runTest' argument.
TestCase.addCleanup
~~~~~~~~~~~~~~~~~~~

View File

@@ -98,8 +98,9 @@ class TestCase(unittest.TestCase):
:ivar exception_handlers: Exceptions to catch from setUp, runTest and
tearDown. This list is able to be modified at any time and consists of
(exception_class, handler(case, result, exception_value)) pairs.
:cvar run_tests_with: A `RunTest` class to run tests with. Defaults to
`RunTest`.
:cvar run_tests_with: A factory to make the `RunTest` to run tests with.
Defaults to `RunTest`. The factory is expected to take a test case
and an optional list of exception handlers.
"""
skipException = TestSkipped