unittest断言方法的使用
unittest框架的TestCase类提供以下方法用于测试结果的判断
| 方法 | 检查 | 版本 |
| 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 | 3.1 |
| assertIsNot(a, b) | a is not b | 3.1 |
| assertIsNone(x) | x is None | 3.1 |
| assertIsNotNone(x) | x is not None | 3.1 |
| assertIn(a, b) | a in b | 3.1 |
| assertNotIn(a, b) | a not in b | 3.1 |
| assertIsInstance(a, b) | isinstance(a,b) | 3.1 |
| assertNotIsInstance(a, b) | not isinstance(a,b) | 3.1 |
-assertEqual(first,second,msg=None)
断言第一个参数和第二个参数是否相等,如果不相等则测试失败
-assertNotEqual(first,second,msg=None)
assertNotEqueal()和assertEqual()相反,它用于第一个参数与第二个参数是否不相等,如果相等则测试失败
-assertTrue(expr,msg=None)
-assertFalse(expr,msg=None)
测试表达式是true(或false)
-assertIn(first,second,msg=None)
-assertNotIn(first,second,msg=None)
判断第一个参数是否在第二个参数中,反过来讲,第二个参数是否包含第一个参数
-assertIs(first,second,msg=None)
-assertIsNot(first,second,msg=None)
断言第一个参数和第二个参数是否为同一个对象
-assertIsNone(first,second,msg=None)
-assertIsNotNone(first,second,msg=None)
断言表达式是否为None对象
-assertIsInstance(first,second,msg=None)
-assertIsNotInstance(first,second,msg=None)
断言obj是否为cls的一个实例
unittest案例:
calculate.py:
class Math:
def __init__(self,a,b):
self.a = int(a)
self.b = int(b) def add(self):
return self.a + self.b
test_Math.py:
from calculator import Math
import unittest class TestMath(unittest.TestCase): def setUp(self):
print("test start") def test_add(self):
j = Math(5,10)
self.assertEquals(j.add(),15)
# self.assertEquals(j.add(),12) def test_add1(self):
j = Math(55,100)
self.assertNotEqual(j.add(),145) def test_add2(self):
j = Math(5,10)
self.assertTrue(j.add() > 10) def assertIs_test(self):
self.assertIs("abc","abc")
# self.assertIs("ab","abc") def assertIn_test(self):
self.assertIn("python","hello python")
# self.assertIn("abc","hello python") def tearDown(self):
print("test end") if __name__ == '__main__':
# unittest.main()
# 构造测试集
suit = unittest.TestSuite()
suit.addTest(TestMath("test_case"))
# 执行测试
runner = unittest.TextTestRunner()
runner.run(suit)
unittest断言方法的使用的更多相关文章
- (三)unittest断言方法的介绍
断言如同在测试用例上,类似于预期结果与实际结果是否一致,如果一致则表示测试通过,Assert断言很好的用于测试结果判断上,更灵活的对预期结果和实际结果进行对比,下面简单的介绍一下unittest的As ...
- unittest常用的断言方法
unittest常用的断言方法 #msg:判断不成立时需要反馈的字符串 assertEqual(self, first, second, msg=None) --判断两个参数相等:first == s ...
- 测试教程网.unittest教程.7. 各种断言方法
From: http://www.testclass.net/pyunit/assert/ 背景 unittest支持各种断言方法. 断言列表 官方文档 方法 检查点 assertEqual(a, b ...
- unittest 单元测试框架断言方法
unittest单元测试框架的TestCase类下,测试结果断言方法:Assertion methods 方法 检查 版本 assertEqual(a, b) a == b assertNotEqu ...
- Selenium实战(四)——unittest单元测试2(断言方法+discover()多测试用例的执行)
一.断言方法 方法 检查 版本 assertEqual(a,b) a==b assertNotEqual(a,b) a!=b assertTrue(x) bool(x) is True a ...
- unittest框架里的常用断言方法:用于检查数据
1.unittest框架里的常用断言方法:用于检查数据. (1)assertEqual(x,y) 检查两个参数类型相同并且值相等.(2)assertTrue(x) 检查唯一的参数值等于True(3)a ...
- python接口自动化(二十三)--unittest断言——上(详解)
简介 在测试用例中,执行完测试用例后,最后一步是判断测试结果是 pass 还是 fail,自动化测试脚本里面一般把这种生成测试结果的方法称为断言(assert).用 unittest 组件测试用例的时 ...
- 自动化测试神器 之 python unittest 断言
自动化测试的最后一步需要判断结果是否正确,而正确设置断言可以帮助判断测试用例的执行结果,从而提高自动化测试的效率,python unittest 提供了一个比较完整的断言方法.unittest框架测 ...
- Selenium 2自动化测试实战28(断言方法)
一.断言方法 执行用例的过程中,最终用例是否执行通过,是通过判断测试得到的实例结果与预期结果是否相等决定的.unittest框架的TestCase类提供下面这些方法用于测试结果的判断. -assert ...
随机推荐
- 一图秒懂http与https的区别
HTTPS与HTTP的一些区别 HTTPS协议需要到CA申请证书,一般免费证书很少,需要交费. HTTP协议运行在TCP之上,所有传输的内容都是明文,HTTPS运行在SSL/TLS之上,SSL/TLS ...
- 【转】Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
Java并发编程:CountDownLatch.CyclicBarrier和Semaphore Java并发编程:CountDownLatch.CyclicBarrier和Semaphore 在j ...
- 浅析 var that = this;
在阅读别人的代码时,发现别人写的代码中有这么一句:var that = this;,这代表什么意思呢?经过一番查阅,才明白是这么回事. 在JavaScript中,this代表的是当前对象. var t ...
- svn merge当主干修改后合并分支
例如版本r1的主干创建分支r2,在r2上修改后得到r3,r1之后也修改得到r4,现在合并分支到主干上: 如果r3的修改和r4有冲突会提示出现冲突,因此不用担心主干合并后会被分支操作覆盖,因为这并不是简 ...
- vertx从入门到精通
1.Vert.x安装指南 http://blog.csdn.net/sdyy321/article/details/38926005 http://blog.csdn.net/chszs/articl ...
- select *from where 和select *from jion on 语句的差别
https://zhidao.baidu.com/question/541791438.html select 学号 a,成绩 a,姓名 b from 成绩表 a,学生表 b where a.学号=b ...
- 2018_oakland_linuxmalware
2018年oakland论文:理解linux恶意软件 论文地址:http://www.s3.eurecom.fr/~yanick/publications/2018_oakland_linuxmalw ...
- TCP的三次握手与四次挥手详解
TCP的三次握手与四次挥手是TCP创建连接和关闭连接的核心流程,我们就从一个TCP结构图开始探究中的奥秘 序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数据字节都编上一个序 ...
- 【转载】WPF DataGrid 性能加载大数据
作者:过客非归 来源:CSDN 原文:https://blog.csdn.net/u010265681/article/details/76651725 WPF(Windows Presentatio ...
- 打印两个有序链表的公共部分 【题目】 给定两个有序链表的头指针head1和head2,打印两个 链表的公共部分
简单题 package my_basic.class_3; public class Code_10_PrintCommonPart { public static class Node{ int v ...