From: http://www.testclass.net/pyunit/assert/

背景

unittest支持各种断言方法。

断言列表

官方文档

方法 检查点
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)
assertRaises(exc, fun, *args, **kwds) fun(*args, **kwds) raises exc
assertRaisesRegexp(exc, r, fun, *args, **kwds) fun(*args, **kwds) raises exc and the message matches regex r
assertAlmostEqual(a, b) round(a-b, 7) == 0
assertNotAlmostEqual(a, b) round(a-b, 7) != 0
assertGreater(a, b) a > b 2.7
assertGreaterEqual(a, b) a >= b
assertLess(a, b) a < b
assertLessEqual(a, b) a <= b
assertRegexpMatches(s, r) r.search(s)
assertNotRegexpMatches(s, r) not r.search(s)
assertItemsEqual(a, b) sorted(a) == sorted(b) 也支持unhashable对象
assertDictContainsSubset(a, b) a里面所有的键值对都在b中存在

自己动手

大家可以自己动手亲自尝试使用这些断言方法。

测试教程网.unittest教程.7. 各种断言方法的更多相关文章

  1. 测试教程网.unittest教程.2. 基本用法

    From: http://www.testclass.net/pyunit/basic_example/ 我们通过最简单的例子来看一下unittest的基本用法,下面的代码测试了3个python字符串 ...

  2. 测试教程网.unittest教程.8. 断言异常

    From: http://www.testclass.net/pyunit/assert_raise/ 背景 我们有时候需要断言一些方法会抛出异常,这些异常需要符合我们的预期. 代码 新建test_e ...

  3. 测试教程网.unittest教程.3. 实例: 测试弱密码

    From: http://www.testclass.net/pyunit/test_example_1/ 背景 考虑这样一个测试弱密码的实例,这个我们在pytest相关教程中也有过描述. 我们需要判 ...

  4. 测试教程网.unittest教程.5. 实例: 找出所有是弱密码的用户

    From: http://www.testclass.net/pyunit/test_example_3/ 背景 当我们的测试数据是下面这些的时候,我们的用例是有问题的. [ {"name& ...

  5. 测试教程网.unittest教程.4. 实例: 读取测试数据并测试弱密码

    From: http://www.testclass.net/pyunit/test_example_2/ 背景 接上一节的弱密码例子,我们的用例尽管运行的不错,但还是有点问题. 假如我们需要增加一些 ...

  6. 测试教程网.unittest教程.1. 基本概念

    From:http://www.testclass.net/pyunit/basic_concept/ unittest是python自带的单元测试框架,有时候又被称为”PyUnit”,是python ...

  7. 测试教程网.unittest教程.6. 命令行接口

    From: http://www.testclass.net/pyunit/commind_line_interface/ 背景 unittest支持命令行接口,我们可以在命令行里指定运行具体的测试用 ...

  8. Selenium实战(四)——unittest单元测试2(断言方法+discover()多测试用例的执行)

    一.断言方法 方法 检查 版本 assertEqual(a,b) a==b   assertNotEqual(a,b) a!=b   assertTrue(x) bool(x) is True   a ...

  9. (三)unittest断言方法的介绍

    断言如同在测试用例上,类似于预期结果与实际结果是否一致,如果一致则表示测试通过,Assert断言很好的用于测试结果判断上,更灵活的对预期结果和实际结果进行对比,下面简单的介绍一下unittest的As ...

随机推荐

  1. vs2015连接mysql进行数据库操作

    要求:电脑提前安装好vs,mysql. 1.在需要连接mysql的项目上右键选择“属性” -> “C/C++” -> “常规” ->选择“附加包含目录” 在弹出窗口中添加mysql的 ...

  2. Sonar理论篇

    一.Sonar是什么    Sonar是一个代码质量管理的开源平台,用于管理源代码的质量,通过插件形式,可以支持包括java.C#.JavaScript等二十余种编程语言的代码质量管理与检测. Son ...

  3. 嵌套for

  4. UE4开发安卓遇到的坑

    做个记录,方便以后自己查询. 1.  打包后,应用出现缺乏google key的问题. 理论上,这个网站可以解决:https://wiki.unrealengine.com/How_To_Sign_U ...

  5. C++学习(十一)(C语言部分)之 练习

    /* 1.if 输入一个成绩 进行分级 输入一个数字 判断是否是水仙花数 比如 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 2.switch 提示 先对数字做处理 ...

  6. linux----磁盘介绍

    1.磁盘简图: 2.常见分区 mbr:主引导区,共512byte 扩展分区 逻辑分区

  7. sailsjs learning note

    menu list: custom controller custom 模块使用 custom model custom middleware custom service ? 路由与对应的contr ...

  8. Go Example--切片

    package main import ( "fmt" ) func main() { //make来初始化一个切片,必须指名切片的长度 s:= make([]string, 3) ...

  9. 你真的了解word-wrap和word-break的区别吗? (转载)

    这两个东西是什么,我相信至今还有很多人搞不清,只会死记硬背的写一个word-wrap:break-word;word-break:break-all;这样的东西来强制断句,又或者是因为这两个东西实在是 ...

  10. 05typedef struct用法详解与小结

    1.基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字,这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等). 在编程中使用typedef ...