安装

pip install pytest-html

使用

测试代码:

import pytest
from random import randint


@pytest.mark.flaky(reruns=3)
def test01():
    assert randint(1, 10) == 6


def test02():
    assert randint(1, 10) > 6


def test03():
    assert randint(1, 10) < 6


if __name__ == '__main__':
    pytest.main(['pytestReport.py', '-s', "--html=report.html"])

命令行参数执行,加入--html=report.html
执行完毕后得到report.html文件和assets(包括html文件的css样式)文件夹。

在这里插入图片描述

打开report.html文件即可看到测试报告:

在这里插入图片描述

命令行参数执行,加入--html=report.html --self-contained-html 参数。则生成的css文件和html文件集为一体,最终只生成一个html文件

测试报告汉化

因为测试报告目前都是英文版的,目前可以通过使用其他开源项目替换成生成中文版报告
地址: https://github.com/13691579846/pytest-html
在本地的python安装目录下, /site-packages/site-packages/下替换pytest-html整个文件,然后再执行会发现已经替换为中文版的报告。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐