1. Test Cases
class unittest.TestCase(methodName='runTest')
Methods:
setUp()
Methods called to prepare the test fixture. This is called immediately before calling the test method; other than AssertError or SkipTest, any exception raised by this method will be considered an error rather than a test failure.tearDown()
Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly carefully about checking internal state. Any exception, other than AssertError or SkipTest, raised by this method will be considered an error rather than a test failure. This method will only be called in the setUp() succeeds, regardless of the outcome of the test method.setUpClass()
tearDownClass()run(result=None) Run the test, collecting the result into the test result object passed as result. If result is omitted or None, a temporary result object is created (by calling the defaultTestResult() method) and used. The result object is not returned to run()'s caller.The same effect may be had by simply calling the TestCase instance.
skipTest(reason)
debug()Method | Checks that | New in |
---|---|---|
a == b | ||
a != b | ||
bool(x) is True | ||
bool(x) is False | ||
a is b | 2.7 | |
a is not b | 2.7 | |
x is None | 2.7 | |
x is not None | 2.7 | |
a in b | 2.7 | |
a not in b | 2.7 | |
isinstance(a, b) | 2.7 | |
not isinstance(a, b) | 2.7 |
Method | Checks that | New in |
---|---|---|
round(a-b, 7) == 0 | ||
round(a-b, 7) != 0 | ||
a > b | 2.7 | |
a >= b | 2.7 | |
a < b | 2.7 | |
a <= b | 2.7 | |
r.search(s) | 2.7 | |
not r.search(s) | 2.7 | |
sorted(a) == sorted(b) and works with unhashable objs | 2.7 | |
all the key/value pairs in aexist in b | 2.7 |
Finally the TestCase provides the following methods and attributes:
fail(msg=None)
Singals a test failure unconditiaonally, with msg or None for the error message.failureException
This class attribute gives the exception raised by the test method. If a test framework needs to use a specialized exception, possibly to carry additional information, it must subclass this exception in order to "play fair" with the framework. The initial value of this attribute is AssertionError.longMessage
If set to True then any explicit failure message you pass in to the assert methods will be appended to the end of the normal failure message. The normal message contain useful information about the objects involved, for example the message from assertEqual shows you the repr of the two unequal objects. Setting this attribute to True allows you to have a custom error message in addition to the normal one.This attribute defaults to False. meaning that a custom message passed to an assert method will silence the normal message.maxDiff
Testing frameworks can use the following methods to collect information on the test:
countTestCases()
Return the number of tests represented by this test objecte. For TestCase instances, this will always be 1.defaultTestResult()
id()
Return a string identifying the specific test case. This is usually the full name of the test method, including the module and class name.shortDescription()
Return a description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the test method's docstring, if available, or None.addCleanup(function,*args,**kwargs)
doCleanups()