博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python的unittest单元测试框架断言整理汇总
阅读量:4364 次
发布时间:2019-06-07

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

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

一、先说说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)

二、最近整理的一些

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

 

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

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

(expected, actual[, msg])

检查实际是否超预期

(d1, d2[, msg])

前后字典是否相同

(first, second[, msg])

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

(expr[, msg])

检查表达式是否为假

(a, b[, msg])

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

(a, b[, msg])

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

(member, container[, msg])

self.assertTrue(a in b)

(expr1, expr2[, msg])

assertTrue(a is b)

(obj, cls[, msg])

Isinstance(a,b)

(obj[, msg])

Obj is none.

(expr1, expr2[, msg])

a is not b.

(obj[, msg])

Obj is not none.

(expected_seq, actual_seq[, msg])

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

(a, b[, msg])

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

(a, b[, msg])

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

(list1, list2[, msg])

List1与list2是否相等.

(first, second[, msg])

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

(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.

(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.

(first, second[, msg])

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

(first, second[, msg])

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

(member, container[, msg])

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

(obj, cls[, msg])

Included for symmetry with assertIsInstance.

(text, unexpected_regexp)

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

(excClass[, callableObj])

除非excclass类抛出异常失败

(expected_exception, ...)

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

(text, expected_regexp[, msg])

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

(seq1, seq2[, msg, seq_type])

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

(set1, set2[, msg])

A set-specific equality assertion.

(expr[, msg])

Check that the expression is true.

(tuple1, tuple2[, msg])

A tuple-specific equality assertion.

(expr[, msg])

Check that the expression is true.

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

 

转载于:https://www.cnblogs.com/xiaohuhu/p/8747699.html

你可能感兴趣的文章
Topcoder SRM 648 (div.2)
查看>>
Constructor总结
查看>>
HttpClient使用之下载远程服务器中的文件(注意目录遍历漏洞)
查看>>
JAVA UDP网络编程学习笔记
查看>>
反素数 -- 数学
查看>>
CODEVS 1205 单词反转
查看>>
洛谷 P3367 【模板】并查集
查看>>
求质数算法的N种境界 (N > 10) zz
查看>>
XmlPullParserException
查看>>
机器学习降维算法一:PCA(主成分分析算法)
查看>>
第五周总结
查看>>
Beam概念学习系列之Pipeline Runners
查看>>
Elasticsearch之需要注意的问题(es和jdk版本)
查看>>
HBASE启动失败,Failed construction of Master: class org.apache.hadoop.hbase.master.HMaster
查看>>
【Python 19】BMR计算器3.0(字符串分割与格式化输出)
查看>>
函数和模块的使用
查看>>
sqlx使用说明
查看>>
[转载]SQL Plus 一些使用技巧
查看>>
Dashboard集群
查看>>
TMS320F28335——IO控制/定时计操作
查看>>