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 b2.7
assertIsNot(a, b)is not b2.7
assertIsNone(x)is None2.7
assertIsNotNone(x)is not None2.7
assertIn(a, b)in b2.7
assertNotIn(a, b)not in b2.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)b2.7
assertGreaterEqual(a, b)>= b2.7
assertLess(a, b)b2.7
assertLessEqual(a, b)<= b2.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 objs2.7
assertDictContainsSubset(a, b)all the key/value pairs in aexist in b2.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值