当前位置:网站首页>Unittest框架之断言
Unittest框架之断言
2022-07-04 12:49:00 【大田测试录】
1、先认识Unittest断言
1)断言就是判断实际结果与预期结果是否一致;
2)虽然 Python 中也有自带的断言,但是最好还是使用Unittest自己的断言,比较好用;
3)unittest断言是在TestCase中实现的,需要在继承TestCase后才能使用断言;
2、常见断言方法
assertEqual(a,b):检查a和b是否相等
assertTrue(a):检查a是不是为true
assertIs(a,b):检查a和b是不是完全一样(assert is)
assertIsNone(a):检查a是不是None
assertIn(a,b):检查a是不是b的子集
assertIsInstance(a,b):检查a、b两个对象实例类型是否相同
3、例子
unittest_assert_demo.py
# 断言
import unittest
class AssertDemo(unittest.TestCase):
def setUp(self):
self.l1, self.l2 = [1, 2], [1, 2]
self.a, self.b = 1, 1
def test01_assertEqual(self):
self.assertEqual(self.a, self.b) # a和b相等
def test02_assertIs(self):
self.assertIs(self.l1, self.l2) # l1和l2不相同
self.assertIs(self.a, self.b) # a和b相同
def test03_assertTrue(self):
self.assertTrue(self.a) # 1 是true
self.assertTrue(0) # 0 是false
def test04_assertIsNone(self):
self.assertIsNone(self.b) # 1 不是 None
self.assertIsNone(None) # None 是 None
def test05_assertIn(self):
self.assertIn(self.l1, self.l2) # l1 不是l2的子集
def test06_assertIsInstance(self):
self.assertIsInstance(self.a, int) # a是int整型
self.assertIsInstance(self.l1, list) # l1是list列表型
if __name__ == '__main__':
unittest.main()
边栏推荐
- Fisher信息量检测对抗样本代码详解
- Node mongodb installation
- 【C 题集】of Ⅶ
- MongoDB常用28条查询语句(转)
- sharding key type not supported
- Dgraph: large scale dynamic graph dataset
- MySQL 45 lecture - learn the actual combat notes of MySQL in Geek time 45 lecture - 06 | global lock and table lock_ Why are there so many obstacles in adding a field to the table
- Web knowledge supplement
- 2022危险化学品经营单位主要负责人练习题及模拟考试
- MySQL8版本免安装步骤教程
猜你喜欢
面试官:Redis中哈希数据类型的内部实现方式是什么?
Distributed base theory
Node の MongoDB安装
使用默认路由作为指向Internet的路由
高质量软件架构的唯一核心指标
1200. 最小绝对差
Openharmony application development how to create dayu200 previewer
How to choose a technology stack for web applications in 2022
Getting started with the go language is simple: go implements the Caesar password
The only core indicator of high-quality software architecture
随机推荐
Variable promotion and function promotion in JS
Distributed base theory
C language programming topic reference
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
MongoDB常用28条查询语句(转)
C foundation in-depth learning II
美国土安全部部长警告移民“不要踏上危险的旅程”
数据库公共字段自动填充
Source code compilation and installation of MySQL
ASP. Net core introduction I
find命令报错: paths must precede expression(转)
C语言个人通讯录管理系统
Dgraph: large scale dynamic graph dataset
Programmer anxiety
Detailed explanation of Fisher information quantity detection countermeasure sample code
Haproxy high availability solution
常见 content-type对应表
读取 Excel 表数据
Three schemes to improve the efficiency of MySQL deep paging query
Animation and transition effects