Package twisted :: Package trial :: Module unittest :: Class TestCase
[show private | hide private]
[frames | no frames]

Type TestCase

    object --+    
             |    
object --+   |    
         |   |    
  TestCase --+    
             |    
   _Assertions --+
                 |
                TestCase

Known Subclasses:
AttributeType_KnownValues, BERBaseEquality, BERBooleanKnownValues, BEREnumeratedKnownValues, BEREnumeratedSanityCheck, BERIntegerKnownValues, BERIntegerSanityCheck, BerLengths, BERNullKnownValues, BEROctetStringKnownValues, BEROctetStringSanityCheck, BERSequenceKnownValues, DistinguishedName_Comparison, DistinguishedName_Init, KnownValues, LDAPDistinguishedName_contains, LDAPDistinguishedName_DomainName, LDAPDistinguishedName_Malformed, LDAPDistinguishedName_Prettify, NetmaskToNumbits, ObjectClass_KnownValues, PtrSoaName, RelativeDistinguishedName_Init, RFC2254Examples, Substrings, TestCallableOverride, TestCaseWithKnownValues, TestComparison, TestComparison, TestEquality, TestInvalid, TestMaybeSubstring, TestSetOperations, TestValid, TestWhitespace

A unit test. The atom of the unit testing universe.

This class extends unittest.TestCase from the standard library. The main feature is the ability to return Deferreds from tests and fixture methods and to have the suite wait for those Deferreds to fire.

To write a unit test, subclass TestCase and define a method (say, 'test_foo') on the subclass. To run the test, instantiate your subclass with the name of the method, and call run on the instance, passing a TestResult object.

The trial script will automatically find any TestCase subclasses defined in modules beginning with 'test_' and construct test cases for all methods beginning with 'test'.

If an error is logged during the test run, the test will fail with an error. See log.err.
Method Summary
  __init__(self, methodName)
Construct an asynchronous test case for methodName.
  __call__(self, *args, **kwargs)
  deferSetUp(self, ignored, result)
  deferSetUpClass(self, result)
  deferTearDown(self, ignored, result)
  deferTearDownClass(self, ignored, result)
  deferTestMethod(self, ignored, result)
  flushLoggedErrors(self, *errorTypes)
Remove stored errors received from the log.
  getSkip(self)
Return the skip reason set on this test, if any is set.
  getSuppress(self)
Returns any warning suppressions set for this test.
  getTimeout(self)
Returns the timeout value set on this test.
  getTodo(self)
Return a Todo object if the test is marked todo.
  mktemp(self)
Returns a unique name that may be used as either a temporary directory or filename.
  run(self, result)
Run the test case, storing the results in result.
  runTest(self)
If no methodName argument is passed to the constructor, run will treat this method as the thing with the actual test inside.
  shortDescription(self)
Returns a one-line description of the test, or None if no description has been provided.
  visit(self, visitor)
Visit this test case.
  _cbDeferTestMethod(self, ignored, result)
  _classCleanUp(self, result)
  _cleanUp(self, result)
  _deprecateReactor(self, reactor)
Deprecate iterate, crash and stop on reactor.
  _ebDeferSetUp(self, failure, result)
  _ebDeferSetUpClass(self, error, result)
  _ebDeferTearDown(self, failure, result)
  _ebDeferTestMethod(self, f, result)
  _ebTearDownClass(self, error, result)
  _getReason(self, f)
  _initInstances(cls)
(Class method)
  _installObserver(self)
  _isFirst(self)
  _isLast(self)
  _makeReactorMethod(self, name)
Create a method which wraps the reactor method name.
  _prepareClassFixture(self)
Lots of tests assume that test methods all run in the same instance of TestCase.
  _removeObserver(self)
  _run(self, methodName, result)
  _undeprecateReactor(self, reactor)
Restore the deprecated reactor methods.
  _wait(self, d, running)
Take a Deferred that only ever callbacks.
    Inherited from _Assertions
  assert_(self, condition, msg)
fails the test if condition evaluates to True
  assertAlmostEqual(self, first, second, places, msg)
Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  assertAlmostEquals(self, first, second, places, msg)
Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  assertApproximates(self, first, second, tolerance, msg)
asserts that first - second > tolerance
  assertEqual(self, first, second, msg)
fail the test if first and second are not equal
  assertEquals(self, first, second, msg)
fail the test if first and second are not equal
  assertFailure(self, deferred, *expectedFailures)
Assert that deferred will errback with one of expectedFailures.
  assertFalse(self, condition, msg)
fails the test if condition evaluates to False
  assertIdentical(self, first, second, msg)
fail the test if first is not second.
  assertIn(self, containee, container, msg)
fail the test if containee is not found in container
  assertNot(self, condition, msg)
fails the test if condition evaluates to False
  assertNotAlmostEqual(self, first, second, places, msg)
Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  assertNotAlmostEquals(self, first, second, places, msg)
Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  assertNotEqual(self, first, second, msg)
fail the test if first == second
  assertNotEquals(self, first, second, msg)
fail the test if first == second
  assertNotIdentical(self, first, second, msg)
fail the test if first is second.
  assertNotIn(self, containee, container, msg)
fail the test if containee is found in container
  assertNotSubstring(self, substring, astring, msg)
  assertRaises(self, exception, f, *args, **kwargs)
fails the test unless calling the function f with the given args and kwargs does not raise exception.
  assertSubstring(self, substring, astring, msg)
  assertTrue(self, condition, msg)
fails the test if condition evaluates to True
  fail(self, msg)
absolutely fails the test, do not pass go, do not collect $200
  failIf(self, condition, msg)
fails the test if condition evaluates to False
  failIfAlmostEqual(self, first, second, places, msg)
Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  failIfAlmostEquals(self, first, second, places, msg)
Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  failIfEqual(self, first, second, msg)
fail the test if first == second
  failIfEquals(self, first, second, msg)
fail the test if first == second
  failIfIdentical(self, first, second, msg)
fail the test if first is second.
  failIfIn(self, containee, container, msg)
fail the test if containee is found in container
  failIfSubstring(self, substring, astring, msg)
  failUnless(self, condition, msg)
fails the test if condition evaluates to True
  failUnlessAlmostEqual(self, first, second, places, msg)
Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  failUnlessAlmostEquals(self, first, second, places, msg)
Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero.
  failUnlessApproximates(self, first, second, tolerance, msg)
asserts that first - second > tolerance
  failUnlessEqual(self, first, second, msg)
fail the test if first and second are not equal
  failUnlessEquals(self, first, second, msg)
fail the test if first and second are not equal
  failUnlessFailure(self, deferred, *expectedFailures)
Assert that deferred will errback with one of expectedFailures.
  failUnlessFalse(self, condition, msg)
fails the test if condition evaluates to False
  failUnlessIdentical(self, first, second, msg)
fail the test if first is not second.
  failUnlessIn(self, containee, container, msg)
fail the test if containee is not found in container
  failUnlessRaises(self, exception, f, *args, **kwargs)
fails the test unless calling the function f with the given args and kwargs does not raise exception.
  failUnlessSubstring(self, substring, astring, msg)
  failUnlessTrue(self, condition, msg)
fails the test if condition evaluates to True
    Inherited from TestCase
  __repr__(self)
  __str__(self)
  countTestCases(self)
  debug(self)
Run the test without collecting errors in a TestResult
  defaultTestResult(self)
  id(self)
  setUp(self)
Hook method for setting up the test fixture before exercising it.
  tearDown(self)
Hook method for deconstructing the test fixture after testing it.
  _exc_info(self)
Return a version of sys.exc_info() with the traceback frame minimised; usually the top level of the traceback frame is not needed.
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value

Instance Variable Summary
type failureException = twisted.trial.unittest.FailTest
  forceGarbageCollection: If set to True, gc.collect() will be called before and after the test.
  skip: None or a string explaining why this test is to be skipped.
  suppress: None or a list of tuples of (args, kwargs) to be passed to warnings.filterwarnings.
  timeout: None or a real number of seconds.
  todo: None, a string or a tuple of (errors, reason) where errors is either an exception class or an iterable of exception classes, and reason is a string.

Class Variable Summary
Implements __implemented__ = <implementedBy twisted.trial.unittest....
ClassProvides __provides__ = <zope.interface.declarations.ClassProvide...
    Inherited from TestCase
ClassProvides __providedBy__ = <zope.interface.declarations.ClassProvi...

Instance Method Details

__init__(self, methodName='runTest')
(Constructor)

Construct an asynchronous test case for methodName.
Parameters:
methodName - The name of a method on self. This method should be a unit test. That is, it should be a short method that calls some of the assert* methods. If methodName is unspecified, runTest will be used as the test method. This is mostly useful for testing Trial.
Overrides:
unittest.TestCase.__init__

flushLoggedErrors(self, *errorTypes)

Remove stored errors received from the log.

TestCase stores each error logged during the run of the test and reports them as errors during the cleanup phase (after tearDown).
Returns:
A list of failures that have been removed.

getSkip(self)

Return the skip reason set on this test, if any is set. Checks on the instance first, then the class, then the module, then packages. As soon as it finds something with a skip attribute, returns that. Returns None if it cannot find anything. See TestCase docstring for more details.

getSuppress(self)

Returns any warning suppressions set for this test. Checks on the instance first, then the class, then the module, then packages. As soon as it finds something with a suppress attribute, returns that. Returns any empty list (i.e. suppress no warnings) if it cannot find anything. See TestCase docstring for more details.

getTimeout(self)

Returns the timeout value set on this test. Checks on the instance first, then the class, then the module, then packages. As soon as it finds something with a timeout attribute, returns that. Returns util.DEFAULT_TIMEOUT_DURATION if it cannot find anything. See TestCase docstring for more details.

getTodo(self)

Return a Todo object if the test is marked todo. Checks on the instance first, then the class, then the module, then packages. As soon as it finds something with a todo attribute, returns that. Returns None if it cannot find anything. See TestCase docstring for more details.

mktemp(self)

Returns a unique name that may be used as either a temporary directory or filename.

Note: you must call os.mkdir on the value returned from this method if you wish to use it as a directory!

run(self, result)

Run the test case, storing the results in result.

First runs setUp on self, then runs the test method (defined in the constructor), then runs tearDown. Any of these may return Deferreds. After they complete, does some reactor cleanup.
Parameters:
result - A TestResult object.
Overrides:
unittest.TestCase.run

runTest(self)

If no methodName argument is passed to the constructor, run will treat this method as the thing with the actual test inside.

shortDescription(self)

Returns a one-line description of the test, or None if no description has been provided.

The default implementation of this method returns the first line of the specified test method's docstring.
Overrides:
unittest.TestCase.shortDescription (inherited documentation)

visit(self, visitor)

Visit this test case. Call visitor with self as a parameter.
Parameters:
visitor - A callable which expects a single parameter: a test case.
Returns:
None

_deprecateReactor(self, reactor)

Deprecate iterate, crash and stop on reactor. That is, each method is wrapped in a function that issues a deprecation warning, then calls the original.
Parameters:
reactor - The Twisted reactor.

_makeReactorMethod(self, name)

Create a method which wraps the reactor method name. The new method issues a deprecation warning and calls the original.

_prepareClassFixture(self)

Lots of tests assume that test methods all run in the same instance of TestCase. This isn't true. Calling this method ensures that self.__class__._testCaseInstance contains an instance of this class that will remain the same for all tests from this class.

_undeprecateReactor(self, reactor)

Restore the deprecated reactor methods. Undoes what _deprecateReactor did.
Parameters:
reactor - The Twisted reactor.

_wait(self, d, running=[])

Take a Deferred that only ever callbacks. Block until it happens.

Instance Variable Details

forceGarbageCollection

If set to True, gc.collect() will be called before and after the test. Otherwise, garbage collection will happen in whatever way Python sees fit.

skip

None or a string explaining why this test is to be skipped. If defined, the test will not be run. Instead, it will be reported to the result object as 'skipped' (if the TestResult supports skipping).

suppress

None or a list of tuples of (args, kwargs) to be passed to warnings.filterwarnings. Use these to suppress warnings raised in a test. Useful for testing deprecated code. See also util.suppress.

timeout

None or a real number of seconds. If set, the test will raise an error if it takes longer than timeout seconds.

todo

None, a string or a tuple of (errors, reason) where errors is either an exception class or an iterable of exception classes, and reason is a string. See Todo or makeTodo for more information.

Class Variable Details

__implemented__

Type:
Implements
Value:
<implementedBy twisted.trial.unittest.TestCase>                        

__provides__

Type:
ClassProvides
Value:
<zope.interface.declarations.ClassProvides object at 0x83c67ec>        

Generated by Epydoc 2.1 on Wed Jan 24 20:46:21 2007 http://epydoc.sf.net