参考链接:https://blog.csdn.net/qq1124794084/article/details/51668672

1. 小数位模糊等于

自动化脚本最重要的是断言,正确设置断言以后才能帮助我们判断测试用例执行结果。

在小同事的帮助下,整理了一份比较详细的断言方法。

一、先说说unittest常用的断言吧

常用的就以下几个,网上一搜一大堆。python版本2.7以上都可以调用了。

断言语法 解释
assertEqual(a, b)  判断a==b
assertNotEqual(a, b) 判断a!=b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b)  a is not b
assertIsNone(x)  x is None
assertIsNotNone(x) x is not None
assertIn(a, b)  a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b) 
assertNotIsInstance(a, b)  not isinstance(a, b)

二、最近整理的一些

在官方文档里面看到的整理一下,有些还来不及翻译。

assertAlmostEqual(first, second[, places, ...])

适用于小数,place是应最少几位相等布尔值才为1(默认为7),如果在place位以内不同则断言失败。

assertDictContainsSubset(expected, actual[, msg])

检查实际是否超预期

assertDictEqual(d1, d2[, msg])

前后字典是否相同

assertEqual(first, second[, msg])

前后两个数不等的话,失败

assertFalse(expr[, msg])

检查表达式是否为假

assertGreater(a, b[, msg])

和self.assertTrue(a > b)用法一样,但是多了设置条件 .

assertGreaterEqual(a, b[, msg])

和self.assertTrue(a > =b)用法一样,但是多了设置条件 .

assertIn(member, container[, msg])

self.assertTrue(a in b)

assertIs(expr1, expr2[, msg])

assertTrue(a is b)

assertIsInstance(obj, cls[, msg])

Isinstance(a,b)

assertIsNone(obj[, msg])

Obj is none.

assertIsNot(expr1, expr2[, msg])

a is not b.

assertIsNotNone(obj[, msg])

Obj is not none.

assertItemsEqual(expected_seq, actual_seq[, msg])

一个无序的序列特异性的比较。

assertLess(a, b[, msg])

Just like self.assertTrue(a < b), but with a nicer default message.

assertLessEqual(a, b[, msg])

Just like self.assertTrue(a <= b), but with a nicer default message.

assertListEqual(list1, list2[, msg])

List1与list2是否相等.

assertMultiLineEqual(first, second[, msg])

断言,2个多行字符串是相等的

assertNotAlmostEqual(first, second[, ...])

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 between the two objects is less than the given delta.

assertNotAlmostEquals(first, second[, ...])

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 between the two objects is less than the given delta.

assertNotEqual(first, second[, msg])

Fail if the two objects are equal as determined by the ‘==’

assertNotEquals(first, second[, msg])

Fail if the two objects are equal as determined by the ‘==’

assertNotIn(member, container[, msg])

Just like self.assertTrue(a not in b), but with a nicer default message.

assertNotIsInstance(obj, cls[, msg])

Included for symmetry with assertIsInstance.

assertNotRegexpMatches(text, unexpected_regexp)

如果文本匹配正则表达式,将失败。

assertRaises(excClass[, callableObj])

除非excclass类抛出异常失败

assertRaisesRegexp(expected_exception, ...)

认为在引发异常的情况下消息匹配一个正则表达式。

assertRegexpMatches(text, expected_regexp[, msg])

测试失败,除非文本匹配正则表达式。

assertSequenceEqual(seq1, seq2[, msg, seq_type])

有序序列的相等断言 (like lists and tuples).

assertSetEqual(set1, set2[, msg])

A set-specific equality assertion.

assertTrue(expr[, msg])

Check that the expression is true.

assertTupleEqual(tuple1, tuple2[, msg])

A tuple-specific equality assertion.

assert_(expr[, msg])

Check that the expression is true.


三、在unittest包里面看到的比较全的断言

转载于:https://www.cnblogs.com/yoyoma0355/p/9565544.html

python 断言大全相关推荐

  1. Python 资源库大全

    Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 vinta 发起和维护.内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文 ...

  2. [资源]Python资源大全中文版

    Python资源大全中文版,内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文本处理.自然语言处理.机器学习.日志.代码分析等 Python 资源大全中文版 我想 ...

  3. Python 资源大全中文版

    我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python是 vinta 发起维护的 Python 资源列表,内容包括:Web 框架.网络 ...

  4. Python 资源大全中文版【2018-11-21】

    我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列表,内容包括:Web 框架.网 ...

  5. 0091-【Python系统】-Python 资源大全中文版

    我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列表,内容包括:Web 框架.网 ...

  6. 年薪20万Python工程师进阶(7):Python资源大全,让你相见恨晚的Python库

    我是 环境管理 管理 Python 版本和环境的工具 pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. virtualenv – 创建独立 Python 环 ...

  7. Python 资源大全

    来源:https://github.com/jobbole/awesome-python-cn Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX ...

  8. [转]Python 资源大全中文版

    摘自:https://github.com/jobbole/awesome-python-cn 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesom ...

  9. python你想知道的都在这里 python资源大全中文版

    Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列 ...

  10. Python 库大全

    作者:Lingfeng Ai 链接:http://www.zhihu.com/question/24590883/answer/92420471 来源:知乎 著作权归作者所有.商业转载请联系作者获得授 ...

最新文章

  1. 高斯混合模型(GaussianMixture Model, GMM)聚类、可视化最优协方差形式、通过TSNE进行结果可视化分析、抽取核心特征因子
  2. windows下手把手教你捕获数据包
  3. oracle 修改sgamaxsize_oracle初始化内存配置参数(sga,pga,sharepool)
  4. 面向对象三种模型之间的关系
  5. centos linux下vnc server远程桌面配置详解,CentOS 6.0 VNC远程桌面配置
  6. long 雪花算法_雪花算法(SnowFlake)Java版
  7. scala属性/成员变量
  8. js圆角矩形旋转(div拼凑)
  9. 超定方程组最小二乘解法——正规方程组求解(matlab代码)
  10. 认知无线电-频谱检测-匹配滤波
  11. WARNING: We noticed you're using the `useBuiltIns` option without declaring a core-js version.
  12. curl: (67) Access denied: 530的可能原因
  13. 第一次参加pub的地面聚会
  14. 篮球英文术语翻译与解释 (以A至E为限)
  15. 修改安卓默认的系统button样式,以及其它系统控件的默认样式
  16. [练习]QQ/微信 表情收藏-测试用例的编写 [简洁思路]
  17. 以太网交换芯片及PHY处理相关
  18. extern 用法小结
  19. 20135337朱荟潼 Linux第六周学习总结——进程的描述和进程的创建
  20. django博客项目-文章详情页功能

热门文章

  1. python 操作微信小程序数据库 实现批量修改
  2. 台式计算机怎么把声音输出,台式电脑音频管理器的设置教程 台式电脑音响没声音怎么弄...
  3. 计算机管理工具怎么设置,电脑音频管理器怎么设置,教你电脑音频管理器怎么设置...
  4. linux 队列缓冲,linux驱动的等待队列(阻塞操作)和轮询(poll),缓冲区笔记
  5. 在语雀中输入汉语拼音
  6. css鼠标移入变小手
  7. 改变世界的15个网站
  8. 工程图字体宋体仿宋_宋黑仿变各种字体的审美禀赋
  9. pytest框架之fixture测试夹具详解
  10. MFC--获取字符串的像素尺寸