Pytest 执行过测试任务后我们需要查看整体测试情况,本文记录使用 pytest 生成测试报告的几种方法。
mtutils 库中的测试代码为例,分别尝试几种测试报告的使用--resultlog = path-to-log.txtpytest --resultlog=./log.txt test-dirpip3 install pytest-html--html=report.htmlpytest .\tests --html=report.html
--junitxml=./log.xml pytest .\tests --junitxml=./log.xml <?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="4" time="1.132" timestamp="2022-06-16T10:48:47.656215" hostname="VVD"><testcase classname="tests.test_utils.test_utils" name="test_get_list_from_list" time="0.001" /><testcase classname="tests.test_utils.test_utils" name="test_segment_intersection" time="0.001" /><testcase classname="tests.test_utils.test_utils" name="test_concat_generator" time="0.002" /><testcase classname="tests.test_utils.test_utils" name="test_get_mac_address" time="0.002" /></testsuite></testsuites>
java --version返回java 版本表示 java 环境配置成功
C:\Users\Administrator>java --version
java 18.0.1.1 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)allure --version正常返回 allure 版本表明配置成功
C:\Users\Administrator>allure --version
2.18.1pip install allure-pytest --alluredir=reports-dirallure serve reports-dirreports/allure 文件夹pytest .\tests\ --alluredir=reports/allureallure serve reports\allure
pip install pytest-cover--cov=.\code\ 参数,用于统计制定文件夹中的代码运行覆盖率,该参数指向的是被测试的代码,不是测试代码--cov-report=html 参数输出 html 报告mtutils 库中的测试代码为例pytest --cov=.\lib\ .\tests\ --cov-report=htmlhtmlcov 文件夹,其中包含了覆盖率的 html 网页报告
