接口框架第二篇—unittest/pytest 有什么区别

1.用例编写方法

unittest

1)测试文件必须导入unittest包

2)测试类必须继承unittest.TestCase

3)测试类必须有unittest.main()方法

4)测试方法必须要以test_打头

pytest

1)测试文件名要以test_打头,或者_test结尾

2)测试类名字要以Test打头

3)测试方法名字test_打头

2.用例分类执行

unittest

默认执行全部用例,也可通过加载testsuit,执行部分用例

pytest

通过@pytest.mark方法来标记类和方法,pytest.main()加入参数-m来标记执行标记的类和方法

3.用例的前置和后置

unittest

提供了setUp/tearDown,只能针对所有用例

pytest

pytest的fixture可以任意自定义方法函数

4.参数化

unittest

依赖ddt

pytest

使用@pytest.mark.parametrize装饰器

5.断言

unittest

断言有很多格式,(assertEqual,assertIn,assertTrue,assertFalse)

pytest

pytest只有assert一个表达式

6.生成测试报告

unittest

unittest使用HTMLTestRunner

pytest

pytest-HTML,allure插件

7.失败重跑

unittest

无机制

pytest

pytest-rerunfailures插件可支持失败重跑

相关推荐
Madison-No73 天前
pytest框架
pytest
凛铄linshuo9 天前
分析业务流程什么时候适合断言?
python·pytest
凛铄linshuo10 天前
自动化测试:pytest第一个实例学习
学习·pytest
Helix25012 天前
Python单元测试实战:pytest框架从入门到企业级测试体系搭建指南
python·单元测试·实战·mock·pytest·测试覆盖率·fixture
在世修行17 天前
干货:pytest
pytest·fixture·pytest assert
小小龙学IT18 天前
Python pytest 测试框架深度解析:从断言重写到插件生态的工程化测试体系
网络·python·pytest
落魄大学生之流水线上谋生计20 天前
pytest 入门与实践:从第一个测试到可维护的测试体系
pytest
念越22 天前
Allure 测试报告保姆级教程:从安装到 pytest 集成与报告生成
pytest·allure
念越22 天前
接口自动化测试从入门到实战:接口用例设计、Requests、Pytest、YAML、JSON Schema 与 Allure 报告
python·测试工具·自动化·json·pytest
霍格沃兹测试学院-小舟畅学1 个月前
AI+Swagger:一键生成500条pytest接口用例
人工智能·pytest