Pytest 测试代码,并生成html
在软件开发中,一定会不断的测试代码,定位问题,那么pytest,是你首选的测试模块。
pytest 官网
1. pytest
# 安装
pip install pytest
测试代码
# test.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
pass
使用pytest运行
pytest .\test.py
输出:这是控制台输出格式
test.py F [100%]
============================================================= FAILURES ==============================================================
____________________________________________________________ test_answer ____________________________________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test.py:6: AssertionError
====================================================== short test summary info ======================================================
FAILED test.py::test_answer - assert 4 == 5
========================================================= 1 failed in 0.12s =========================================================
接下来,就是输出报告网页
2. pytest-html
# 安装
pip install pytest-html
还是测试test.py
pytest --html=reportname.html .\test.py

本文介绍了如何使用pytest进行代码测试,通过实例展示了测试脚本的编写及运行,详细解释了控制台输出的结果。此外,还演示了如何安装pytest-html插件,生成测试报告HTML文件,帮助开发者更好地理解和分析测试结果。
6631

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



