pytest 测试报告
安装
pip install pytest-html
使用
配置文件中添加参数addopts = -s --html=report/report.html
结果
在项目目录下会对一个 report 文件夹,里面有个 report.html 即为测试报告

控制用例执行顺序
安装
pip install pytest-ordering
使用
- 标记于被测试函数,@pytest.mark.run(order=x)
- 根据order传入的参数来解决运行顺序
- order值全为正数或全为负数时,运行顺序:值越小,优先级越高
- 正负数同时存在:正数优先级高
测试代码
import pytest
def add(x, y):
return x + y
class TestAdd:
@pytest.mark.last # 设置用例最后执行
def test_add_01(self):
result = add(1, 2)
assert 3 == result
@pytest.mark.run(order=0)
def test_add_02(self):
result = add(2, 2)
assert 4 == result
@pytest.mark.run(order=-2)
def test_add_03(self):
result = add(3, 2)
assert 5 == result
结果

失败重试
安装
pip install pytest-rerunfailures
使用
在配置文件中的命令行参数中增加 --reruns n
结果

文章介绍了如何使用pytest进行测试报告生成,包括安装pytest-html插件以创建HTML测试报告,以及通过pytest-ordering控制测试用例的执行顺序。此外,还讲解了如何利用pytest-rerunfailures处理测试失败并进行重试。

271

被折叠的 条评论
为什么被折叠?



