PyDocs

Nav

Development Tools

TestCase

  • TestCase(methodName='runTest')
A class whose instances are single test cases.

By default, the test code itself should be placed in a method named
'runTest'.

If the fixture may be used for many test cases, create as
many test methods as are needed. When instantiating such a TestCase
subclass, specify in the constructor arguments the name of the test method
that the instance is to execute.

Test authors should subclass TestCase for their own tests. Construction
and deconstruction of the test's environment ('fixture') can be
implemented by overriding the 'setUp' and 'tearDown' methods respectively.

If it is necessary to override the __init__ method, the base class
__init__ method must always be called. It is important that subclasses
should not change the signature of their __init__ method, since instances
of the classes are instantiated automatically by parts of the framework
in order to be run.

When subclassing TestCase, you can set these attributes:
* failureException: determines which exception will be raised when
    the instance's assertion methods fail; test methods raising this
    exception will be deemed to have 'failed' rather than 'errored'.
* longMessage: determines whether long messages (including repr of
    objects used in assert methods) will be printed on failure in *addition*
    to any explicit message passed.
* maxDiff: sets the maximum length of a diff in failure messages
    by assert methods using difflib. It is looked up as an instance
    attribute so can be configured by individual tests if required.
Changed in 3.2
  • 3.2: TestCase can be instantiated successfully without providing a methodName. This makes it easier to experiment with TestCase from the interactive interpreter.
Members of TestCase

a

addClassCleanup
Same as addCleanup, except the cleanup items are called even if setUpClass fails (unlike tearDownClass).
addCleanup
Add a function, with arguments, to be called when the test is completed.
addTypeEqualityFunc
Add a type specific assertEqual style function to compare a type.
assertAlmostEqual
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, or by comparing that the difference between the …
assertCountEqual
Asserts that two iterables have the same elements, the same number of times, without regard to order.
assertDictEqual
Test that two dictionaries are equal.
assertEndsWith
Test that the Unicode or byte string s ends (or does not end) with a suffix.
assertEqual
Fail if the two objects are unequal as determined by the '==' operator.
assertFalse
Check that the expression is false.
assertGreater
Just like self.assertTrue(a > b), but with a nicer default message.
assertGreaterEqual
Just like self.assertTrue(a >= b), but with a nicer default message.
assertHasAttr
Test that the object obj has (or has not) an attribute name.
assertIn
Just like self.assertTrue(a in b), but with a nicer default message.
assertIs
Just like self.assertTrue(a is b), but with a nicer default message.
assertIsInstance
Same as self.assertTrue(isinstance(obj, cls)), with a nicer default message.
assertIsNone
Same as self.assertTrue(obj is None), with a nicer default message.
assertIsNot
Just like self.assertTrue(a is not b), but with a nicer default message.
assertIsNotNone
Included for symmetry with assertIsNone.
assertIsSubclass
Test that cls is (or is not) a subclass of superclass (which can be a class or a tuple of classes, as supported by issubclass()).
assertLess
Just like self.assertTrue(a < b), but with a nicer default message.
assertLessEqual
Just like self.assertTrue(a <= b), but with a nicer default message.
assertListEqual
A list-specific equality assertion.
assertLogs
Fail unless a log message of level *level* or higher is emitted on *logger_name* or its children.
assertMultiLineEqual
Assert that two multi-line strings are equal.
assertNoLogs
Fail unless no log messages of level *level* or higher are emitted on *logger_name* or its children.
assertNotAlmostEqual
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, or by comparing that the difference between the tw…
assertNotEndsWith
Test that the Unicode or byte string s ends (or does not end) with a suffix.
assertNotEqual
Fail if the two objects are equal as determined by the '!=' operator.
assertNotHasAttr
Test that the object obj has (or has not) an attribute name.
assertNotIn
Just like self.assertTrue(a not in b), but with a nicer default message.
assertNotIsInstance
Included for symmetry with assertIsInstance.
assertNotIsSubclass
Test that cls is (or is not) a subclass of superclass (which can be a class or a tuple of classes, as supported by issubclass()).
assertNotRegex
Fail the test if the text matches the regular expression.
assertNotStartsWith
Test that the Unicode or byte string s starts (or does not start) with a prefix.
assertRaises
Fail unless an exception of class expected_exception is raised by the callable when invoked with specified positional and keyword arguments.
assertRaisesRegex
Asserts that the message in a raised exception matches a regex.
assertRegex
Fail the test unless the text matches the regular expression.
assertSequenceEqual
An equality assertion for ordered sequences (like lists and tuples).
assertSetEqual
A set-specific equality assertion.
assertStartsWith
Test that the Unicode or byte string s starts (or does not start) with a prefix.
assertTrue
Check that the expression is true.
assertTupleEqual
A tuple-specific equality assertion.
assertWarns
Fail unless a warning of class warnClass is triggered by the callable when invoked with specified positional and keyword arguments.
assertWarnsRegex
Asserts that the message in a triggered warning matches a regexp.

c

countTestCases
Return the number of tests represented by this test object.

d

debug
Run the test without collecting errors in a TestResult
defaultTestResult
Return an instance of the test result class that should be used for this test case class (if no other result instance is provided to the run() method).
doClassCleanups
Execute all class cleanup functions.
doCleanups
Execute all cleanup functions.

e

enterClassContext
Same as enterContext, but class-wide.
enterContext
Enters the supplied context manager.

f

fail
Fail immediately, with the given message.
failureException
Assertion failed.

i

id
Return a string identifying the specific test case.

l

longMessage
This class attribute determines what happens when a custom failure message is passed as the msg argument to an assertXYY call that fails.

m

maxDiff
This attribute controls the maximum length of diffs output by assert methods that report diffs on failure.

o

output
A list of str objects with the formatted output of matching messages.

r

records
A list of logging.LogRecord objects of the matching log messages.
run
Run the test, collecting the result into the TestResult object passed as result.

s

setUp
Hook method for setting up the test fixture before exercising it.
setUpClass
Hook method for setting up class fixture before running tests in the class.
shortDescription
Returns a one-line description of the test, or None if no description has been provided.
skipTest
Skip this test.
subTest
Return a context manager that will return the enclosed block of code in a subtest identified by the optional message and keyword parameters.

t

tearDown
Hook method for deconstructing the test fixture after testing it.
tearDownClass
Hook method for deconstructing the class fixture after running all tests in the class.
0 Examples
No examples for unittest/TestCase.
See Also
No see-alsos for unittest/TestCase
0 Notes
No notes for TestCase