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

一、先说说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包里面看到的比较全的断言

最新文章

  1. Xcode7.x中安装Alcatraz
  2. 什么是Activity
  3. Airbase-ng帮助
  4. 解决Android AVD的方向键DPAD不能用的问题
  5. C# 动态绘制任务栏图标的实现
  6. [Java] SSH框架笔记_S2SH整合步骤
  7. centos6.4上安装phpmyfaq
  8. easyui源码翻译1.32--ComboGrid(数据表格下拉框)
  9. Name control
  10. android 布局如何支持多种不同屏幕尺寸
  11. openwrt下和云端通讯的例程,
  12. Linux下查看MySQL的安装路径
  13. Linux中cat、more、less、tail、head命令的区别
  14. ionic3 百度地图插件定位 问题
  15. HDU 2516 斐波那契博弈
  16. layer 关闭当前弹出层
  17. [Python_5] Python 线程
  18. spring-boot-资源处理
  19. log4j的配置和使用
  20. centos install rabbitmq

热门文章

  1. Music in Car CodeForces - 746F
  2. BFS 2015百度之星初赛2 HDOJ 5254 棋盘占领
  3. Zernike矩之边缘检测(附源码)
  4. 关于itchat用法的一篇博文
  5. python_函数嵌套(4)
  6. 洛谷P2764 最小路径覆盖问题(二分图)
  7. 微信小程序 逻辑层
  8. jquery实现上传图片及图片大小验证、图片预览效果代码
  9. 《Python基础教程》 读书笔记 第六章 抽象 函数 参数
  10. Java URL 中文乱码解决办法