【pytest】生成测试报告

0. 脚本:

fixture/test_fixtures_02.py

复制代码
# 功能函数
import pytest


def multiply(a, b):
    return a * b


class TestMultiply:
    # =====fixtures========
    @classmethod
    def setup_class(cls):
        print("setup_class=========>")

    @classmethod
    def teardown_class(cls):
        print("teardown_class=========>")

    def setup_method(self, method):
        print("setup_method----->>")

    def teardown_method(self, method):
        print("teardown_method-->>")

    def setup(self):
        print("setup----->")

    def teardown(self):
        print("teardown-->")

    # =====测试用例========
    def test_numbers_5_6(self):
        print('test_numbers_5_6')
        assert multiply(5, 6) == 30

    def test_strings_b_2(self):
        print('test_strings_b_2')
        assert multiply('b', 2) == 'bb'

1.生成 xml 报告

main.py

复制代码
import pytest

if __name__=='__main__':
    #pytest.main(['-s','./fixture'])
    pytest.main(['-v', './fixture','--junit-xml=./report/log.xml'])

报告:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<testsuite name="pytest" errors="0" failures="0" skipped="0" tests="6" time="0.231" timestamp="2023-09-18T06:57:21.230110" hostname="WIN-9AMNN7ECA5T">
<testcase classname="fixture.test_fixtures_01" name="test_mult_3_4" time="0.003" /><testcase classname="fixture.test_fixtures_01" name="test_mult_a_4" time="0.004" /><testcase classname="fixture.test_fixtures_011" name="test_multiply_3_4" time="0.003" /><testcase classname="fixture.test_fixtures_011" name="test_multiply_a_3" time="0.003" /><testcase classname="fixture.test_fixtures_02.TestMultiply" name="test_numbers_5_6" time="0.003" />
<testcase classname="fixture.test_fixtures_02.TestMultiply" name="test_strings_b_2" time="0.003" />
</testsuite>
</testsuites>

方便处理成自己需要报告

2.生成在线报告

--pastebin=all 生成一个 session-log 链接浏览器打开

XML 复制代码
import pytest

if __name__=='__main__':
    pytest.main(['-s','./fixture'])
    #pytest.main(['-v', './fixture','--junit-xml=./report/log.xml'])
    pytest.main(['-v', './fixture', '--pastebin=all'])
XML 复制代码
======================== 6 passed, 1 warning in 0.14s =========================
==================== Sending information to Paste Service =====================
pastebin session-log: https://bpa.st/show/GFZQ

打开View paste L2HQ

相关推荐
zstar-_6 分钟前
一套个人知识储备库构建方案
python
独立开阀者_FwtCoder6 分钟前
stagewise:让AI与代码编辑器无缝连接
前端·javascript·github
清沫8 分钟前
Cursor Rules 开发实践指南
前端·ai编程·cursor
20242817李臻11 分钟前
20242817李臻-安全文件传输系统-项目验收
数据库·安全
江城开朗的豌豆14 分钟前
JavaScript篇:对象派 vs 过程派:编程江湖的两种武功心法
前端·javascript·面试
不吃糖葫芦315 分钟前
App使用webview套壳引入h5(二)—— app内访问h5,顶部被手机顶部菜单遮挡问题,保留顶部安全距离
前端·webview
行思理24 分钟前
MongoDB慢查询临时开启方法讲解
数据库·mongodb
Amo Xiang29 分钟前
《100天精通Python——基础篇 2025 第5天:巩固核心知识,选择题实战演练基础语法》
python·选择题·基础语法
bbsh209935 分钟前
WebFuture 升级提示“不能同时包含聚集KEY和大字段””的处理办法
数据库·sql·mysql·webfuture
江城开朗的豌豆36 分钟前
JavaScript篇:字母侦探:如何快速统计字符串里谁才是'主角'?
前端·javascript·面试