博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unittest -官网文档学习笔记-TestCase class
阅读量:4963 次
发布时间:2019-06-12

本文共 3387 字,大约阅读时间需要 11 分钟。

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
== b  
!= b  
bool(x) is True  
bool(x) is False  
is b 2.7
is not b 2.7
is None 2.7
is not None 2.7
in b 2.7
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  
b 2.7
>= b 2.7
b 2.7
<= 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()

 

转载于:https://www.cnblogs.com/isister/p/4638779.html

你可能感兴趣的文章
centos7 mysql 5.7.24 源码编译
查看>>
mysql 重新初始化
查看>>
从安装VMware15、Ubuntu18.04、ROS Melodic Morenia 详细教程
查看>>
VI基础
查看>>
机器人ROS系统学习随笔->1《ROS基础》
查看>>
机器人ROS系统学习随笔->2 RPLIDAR激光雷达使用
查看>>
python函数式编程之偏函数
查看>>
python 中if __name__ =="__main__":的个人理解
查看>>
flask 设置https请求 访问flask服务器
查看>>
django 从零开始 4 404页面和500页面设置
查看>>
django 验证码图片生成视图函数
查看>>
django 从零开始 5 数据库模型创建
查看>>
django 从零开始 3认识url解析
查看>>
django 从零开始 6 数据库模型增删改查
查看>>
django 从零开始 11 根据时间戳加密数据
查看>>
django 从零开始 7 数据库模型关联属性
查看>>
docker 编译开发代码做镜像
查看>>
django 从零开始 8 用户登录验证 待测
查看>>
django 从零开始 10 缓存控制
查看>>
django 从零开始 12 快速集合queryset对象
查看>>