unittest -官网文档学习笔记-TestCase class

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()

MethodChecks thatNew in
assertEqual(a, b) == b  
assertNotEqual(a, b) != b  
assertTrue(x) bool(x) is True  
assertFalse(x) bool(x) is False  
assertIs(a, b) is b 2.7
assertIsNot(a, b) is not b 2.7
assertIsNone(x) is None 2.7
assertIsNotNone(x) is not None 2.7
assertIn(a, b) in b 2.7
assertNotIn(a, b) not in b 2.7
assertIsInstance(a, b) isinstance(a, b) 2.7
assertNotIsInstance(a, b) not isinstance(a, b) 2.7
MethodChecks thatNew in
assertAlmostEqual(a, b) round(a-b, 7) == 0  
assertNotAlmostEqual(a, b) round(a-b, 7) != 0  
assertGreater(a, b) b 2.7
assertGreaterEqual(a, b) >= b 2.7
assertLess(a, b) b 2.7
assertLessEqual(a, b) <= b 2.7
assertRegexpMatches(s, r) r.search(s) 2.7
assertNotRegexpMatches(s, r) not r.search(s) 2.7
assertItemsEqual(a, b) sorted(a) == sorted(b) and works with unhashable objs 2.7
assertDictContainsSubset(a, b) 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()

原文地址:https://www.cnblogs.com/isister/p/4638779.html