当前位置:网站首页>pytest接口自动化测试框架 | unittest与pytest的区别
pytest接口自动化测试框架 | unittest与pytest的区别
2022-07-22 09:48:00 【COCOgsta】
视频来源:B站《冒死上传!pytest接口自动化测试框架(基础理论到项目实战及二次开发)教学视频【软件测试】》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
unittest
Unittest是Python标准库中自带的单元测试框架,Unittest有时候也被称为PyUnit,就像JUnit是Java单元的标准单元测试框架一样,Unittest则是Python语言的标准单元测试框架。
Unittest支持自动化测试,测试用例的初始化、关闭和测试用例的聚合等功能,它由一个很重要的特性:它是通过类(class)的方式,将测试用例组织在一起。
Pytest
Pytest是Python的另一个第三方单元测试库。它的目的是让单元测试变得更容易,并且也能扩展到支持应用层面复杂的功能测试。
pytest的特性有:
- 支持用简单的assert语句实现丰富的断言,无需复杂的self.assert*函数
- 自动识别测试模块和测试函数
- 模块化夹具以管理各类测试资源
- 对unittest完全兼容,对nose基本兼容
- 支持Python3和PyPy3
- 丰富的插件生态,已有300多个各式各样的插件,社区繁荣
pytest VS unittest
unitest | pytest | |
用例编写规则 | 1)测试文件必须先import unittest 2)测试类必须继承unittest.TestCase 3)测试方法必须以“test_”开头 4)测试类必须要有unittest.main()方法 | 1)测试文件名必须以“test_”开头或者“_test”结尾(如:test_ab.py) 2)测试方法必须以“test_”开头 3)测试类命令以“Test”开头 |
用例分类执行 | 默认执行全部用例,也可以通过加载testsuit,执行部分用例 | 可以通过@pytest.mark来标记类和方法,pytest.main加入参数(“-m”)可以只运行标记的类和方法 |
用例前置和后置 | 提供了setup/teardown,只能针对所有用例 | pytest中的fixture显然更加灵活。可以任意自定义方法函数,只要加上@pytest.fixture()这个装饰器,那么被装饰的方法就可以被使用 |
参数化 | 需依赖ddt库 | 使用@pytest.mark.parametrize装饰器 |
断言 | 很多断言格式(assertEqual、assertIn、assertTrue、assertFalse) | 只有assert一个表达式,用起来比较方便 |
报告 | 使用HTMLTestRunnerNew库 | 有pytset-HTML、allure插件 |
失败重跑 | 无此功能 | pytest支持用例执行失败重跑,pytest-rerunfailures插件 |
边栏推荐
- Introduction to functions
- “35岁,我退休了”:关于中年危机,这是最靠谱的回答
- LeetCode 每日一题 2022/2/21-2022/2/27
- Help brand insight -- Analysis of consumers' emotional behavior
- 蓝队资源大合集
- Leetcode daily question 2022/2/14-2022/2/20
- Leetcode daily question 2021/11/29-2021/12/5
- LeetCode 每日一题 2022/1/3-2022/1/9
- "35 years old, I retired": This is the most reliable answer to the midlife crisis
- 河北、浙江的气候变化了?
猜你喜欢
MySQL optimization enforces the use of indexes
Day3:分支结构
“35岁,我退休了”:关于中年危机,这是最靠谱的回答
MFC dialog program only runs a simple example of a single instance
由浅入深详解NLP中的Adapter技术
Jackson parsing JSON detailed tutorial
PHP implementation deletes a value in a one-dimensional array
4G工业路由器大气环境监测方案
Several trends in the future development of software industry
About human resource outsourcing companies
随机推荐
Leetcode daily question 2022/1/24-2022/1/30
Introduction to arrays
不再执着于用产品说话,苹果库克加大美国反垄断游说投入:今年上半年支出 460 万美元
garbage collection
Force deduction solution summary 729- my schedule I
LeetCode 每日一题 2022/3/28-2022/4/3
Prototype object
软件产业未来发展的几个趋势
Ps: how to call up auxiliary lines
Delphi提高开发效率之GExperts专家的使用说明
MySQL implements querying data from other tables and inserting another table
LeetCode 每日一题 2021/11/22-2021/11/28
LeetCode 每日一题 2022/3/21-2022/3/27
Future prospects and trends of IT outsourcing service industry in various fields
Huawei mobile phone locking application
Day5:构造程序逻辑
LeetCode 每日一题 2022/1/31-2022/2/6
Leetcode daily question 2022/3/21-2022/3/27
Leetcode daily question 2022/2/14-2022/2/20
Force deduction solution summary 522- longest special sequence II