【Python】数据可视化利器PyCharts在测试工作中的应用

目录

[PyCharts 简介](#PyCharts 简介)

[PyCharts 的安装](#PyCharts 的安装)

缺陷统计

测试用例执行情况

使用JavaScript情况

缺陷趋势分析

将两张图放在一个组合里(grid)

将两张图重叠成一张图(overlap)

[将多张图组合在一个page 中(page)](#将多张图组合在一个page 中(page))

实际应用:常态化性能压测数据统计

总结

资料获取方法


PyCharts 简介

PyCharts 是一个基于 Python 的数据可视化库,它支持多种图表类型,如折线图、柱状图、饼图等。PyCharts 提供了简洁的 API,使得用户能够轻松地创建各种图表,同时支持个性化的配置,以满足不同需求。PyCharts 的底层依赖于 ECharts,这使得它在功能和性能上都具有很高的优势。

PyCharts 的安装

PyCharts 的安装非常简单,只需在命令行中输入以下命令:

python 复制代码
pip install pyecharts

安装完成后,即可在 Python 代码中导入 PyCharts 库并开始使用。

作为软件测试工程师,我们经常需要处理测试过程中的数据。文不如表、表不如图,通过 PyCharts,我们可以轻松的将这些数据以直观的形式展示出来,从而更好地分析问题、汇报进度。

缺陷统计

在软件测试过程中,我们需要对发现的缺陷进行统计和分析。以下代码演示了如何使用 PyCharts 创建一个饼图,展示各个严重级别的缺陷数量:

python 复制代码
from pyecharts.charts import Pie
from pyecharts import options as opts

pie = Pie()
pie.add("", [("建议", 33), ("一般", 45), ("严重", 20), ("致命", 2)])
pie.set_global_opts(title_opts=opts.TitleOpts(title="缺陷严重级别统计"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)"))
pie.render()

执行上面的代码会自动的在同级目录生成一个 render.xml 文件,使用浏览器打开就可以看到展示了不同严重级别的缺陷数量及其占比的饼图。我们可以依据这个图更好地制定测试策略和优先级。

当然,我们也可以在 render 之前加上下面这段代码来指定颜色,得到下面这张图:

python 复制代码
# 指定颜色
colors = ["#0000FF", "#4169E1", "#1E90FF", "#00BFFF"]
pie.set_colors(colors)

测试用例执行情况

在执行测试用例时,我们需要关注测试用例的执行情况,如通过率、失败率等。以下代码演示了如何使用 PyCharts 创建一个堆叠柱状图,展示各个模块的测试用例执行情况:

python 复制代码
from pyecharts.charts import Bar
from pyecharts import options as opts

bar = Bar()
bar.add_xaxis(["模块A", "模块B", "模块C", "模块D", "模块E"])

# 添加通过和失败的数据,并设置为堆叠模式
bar.add_yaxis("通过", [90, 80, 70, 60, 50], stack="总量")
bar.add_yaxis("失败", [10, 20, 30, 40, 50], stack="总量")

# 设置全局选项,包括标题
bar.set_global_opts(title_opts=opts.TitleOpts(title="测试用例执行情况"),)

# 设置系列选项,包括标签格式
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="outside", formatter="{c}%")) # 显示百分比的标签格式

bar.render()

这段代码创建了一个堆叠柱状图,展示了各个模块的测试用例通过和失败数量。通过这样的图表,我们可以直观地了解到各个模块的测试情况,从而更好地调整测试计划和资源分配。

使用JavaScript情况

因为pycharts 底层基于 ECharts,所以 JavaScript代码也可以在脚本中使用。

在 Python 中调用 JavaScript 代码:

python 复制代码
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode

bar = Bar()
bar.add_xaxis(["模块A", "模块B", "模块C", "模块D", "模块E"])

passed_cases = [90, 75, 76, 69, 58]

# 添加通过和失败的数据,并设置为堆叠模式
bar.add_yaxis(
    "通过",
    passed_cases,
    stack="总量",
    label_opts=opts.LabelOpts(is_show=False)  # 隐藏通过用例的标签
)

# 使用回调函数计算失败用例的百分比
failure_percentage = f"""
function(params) {{
    var passed_cases = {passed_cases};
    var total = params.value + passed_cases[params.dataIndex];
    var percentage = (params.value / total) * 100;
    return percentage.toFixed(2) + '%';
}}
"""

bar.add_yaxis(
    "失败",
    [10, 20, 30, 40, 50],
    stack="总量",
    label_opts=opts.LabelOpts(
        is_show=True, position="inside", formatter=JsCode(failure_percentage)
    )  # 显示失败用例的标签
)

# 设置全局选项,包括标题
bar.set_global_opts(
    title_opts=opts.TitleOpts(title="测试用例执行情况"),
)

bar.render()

缺陷趋势分析

在项目进展过程中,我们需要关注缺陷的趋势,以评估项目质量和进度。以下代码演示了如何使用 PyCharts 创建一个折线图,展示项目中缺陷的趋势变化:

python 复制代码
from pyecharts.charts import Line
from pyecharts import options as opts

# 创建 Line 对象
line = Line()

# 添加 x 轴数据
line.add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])

# 新增问题单和已修复问题单的数据
new_defects = [50, 35, 28, 20, 5]
fixed_defects = [5, 20, 30, 33, 50]

# 计算剩余未修复的问题单
remaining_defects = []
cumulative_new_defects = 0
cumulative_fixed_defects = 0

# 遍历新增问题单和已修复问题单的数据
for new, fixed in zip(new_defects, fixed_defects):
    # 累积计算新增问题单和已修复问题单的数量
    cumulative_new_defects += new
    cumulative_fixed_defects += fixed

    # 计算剩余未修复的问题单,并将结果添加到 remaining_defects 列表中
    remaining_defects.append(cumulative_new_defects - cumulative_fixed_defects)

# 向图表中添加 y 轴数据系列
line.add_yaxis("新增缺陷", new_defects)
line.add_yaxis("已修复缺陷", fixed_defects)
line.add_yaxis("剩余未修复问题单", remaining_defects)

# 设置全局选项,包括标题
line.set_global_opts(title_opts=opts.TitleOpts(title="缺陷趋势分析"))

# 渲染图表
line.render()

这段代码创建了一个折线图,展示了项目中新增缺陷和已修复缺陷的数量变化。通过这样的图表,我们可以直观地了解到项目的质量趋势,从而更好地制定测试策略和计划。

将两张图放在一个组合里(grid)

体现随着时间的变化,执行用例和 bug 情况的变化:

python 复制代码
from pyecharts.charts import Line, Bar, Grid
from pyecharts import options as opts

# 创建 Line 对象

new_defects = [50, 35, 28, 20, 5]
fixed_defects = [5, 20, 30, 33, 50]

remaining_defects = []
cumulative_new_defects = 0
cumulative_fixed_defects = 0

for new, fixed in zip(new_defects, fixed_defects):
    cumulative_new_defects += new
    cumulative_fixed_defects += fixed
    remaining_defects.append(cumulative_new_defects - cumulative_fixed_defects)

line = (Line().add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"]).add_yaxis("新增缺陷", new_defects)
        .add_yaxis("已修复缺陷", fixed_defects)
        .add_yaxis("剩余未修复问题单", remaining_defects)
        # 设置全局选项,包括标题
        .set_global_opts(title_opts=opts.TitleOpts(title="缺陷趋势分析"),
                         legend_opts=opts.LegendOpts(pos_top="48%"),)

)
# 添加执行用例数的数据
executed_cases = [150, 170, 195, 110, 86]

# 创建 Bar 对象
bar = (Bar().add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])
.add_yaxis("执行用例数", executed_cases)
       )

# 创建 Grid 对象,并添加 Line 和 Bar 图表
grid = (Grid().
        add(bar, grid_opts=opts.GridOpts(pos_bottom="60%")).
        add(line, grid_opts=opts.GridOpts(pos_top="60%")))

# 渲染图表
grid.render()

将两张图重叠成一张图(overlap)

python 复制代码
from pyecharts.charts import Line, Bar
from pyecharts import options as opts

# 创建 Bar 对象
bar = Bar()
bar.add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])

# 添加执行用例数的数据
executed_cases = [15, 17, 19.5, 11, 3]
bar.add_yaxis("执行用例数(/百条)", executed_cases)

# 创建 Line 对象
line = Line()
line.add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])

new_defects = [50, 35, 28, 20, 5]
fixed_defects = [5, 20, 30, 33, 40]

remaining_defects = []
cumulative_new_defects = 0
cumulative_fixed_defects = 0

for new, fixed in zip(new_defects, fixed_defects):
    cumulative_new_defects += new
    cumulative_fixed_defects += fixed
    remaining_defects.append(cumulative_new_defects - cumulative_fixed_defects)

# 设置 is_smooth=True 参数使折线平滑显示
line.add_yaxis("新增缺陷", new_defects)
line.add_yaxis("已修复缺陷", fixed_defects)
line.add_yaxis("剩余未修复问题单", remaining_defects)

# 使用 overlap() 方法将 Line 图表叠加到 Bar 图表中
bar.overlap(line)

# 设置全局选项,包括标题
bar.set_global_opts(title_opts=opts.TitleOpts(title="缺陷趋势分析"))

# 渲染图表
bar.render()

将多张图组合在一个page 中(page)

python 复制代码
from pyecharts.charts import Line, Bar, Page, Pie, Grid
from pyecharts import options as opts

# 创建 Line 对象
new_defects = [50, 35, 28, 20, 5]
fixed_defects = [5, 20, 30, 33, 50]

remaining_defects = []
cumulative_new_defects = 0
cumulative_fixed_defects = 0

for new, fixed in zip(new_defects, fixed_defects):
    cumulative_new_defects += new
    cumulative_fixed_defects += fixed
    remaining_defects.append(cumulative_new_defects - cumulative_fixed_defects)

line = (Line().add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"]).add_yaxis("新增缺陷", new_defects)
        .add_yaxis("已修复缺陷", fixed_defects)
        .add_yaxis("剩余未修复问题单", remaining_defects)
        # 设置全局选项,包括标题
        .set_global_opts(title_opts=opts.TitleOpts(title="趋势分析(缺陷/用例)"),
                         legend_opts=opts.LegendOpts(pos_top="48%"),)

)
# 添加执行用例数的数据
executed_cases = [150, 170, 195, 110, 86]

# 创建 Bar 对象
bar = (Bar().add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])
.add_yaxis("执行用例数", executed_cases)
       )

# 创建 Grid 对象,并添加 Line 和 Bar 图表
grid = (Grid().
        add(bar, grid_opts=opts.GridOpts(pos_bottom="60%")).
        add(line, grid_opts=opts.GridOpts(pos_top="60%"))
        )

# 渲染图表
grid.render()

pie = Pie()
pie.add("", [("建议", 33), ("一般", 45), ("严重", 20), ("致命", 2)])
pie.set_global_opts(title_opts=opts.TitleOpts(title="缺陷严重级别统计"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)"))
# 指定颜色
colors = ["#0000FF", "#4169E1", "#1E90FF", "#00BFFF"]
pie.set_colors(colors)

# 创建 Page 对象,并添加图表
page = Page()
page.add(grid)
page.add(pie)

# 渲染图表
page.render()

实际应用:常态化性能压测数据统计

python 复制代码
import random
from pyecharts.charts import Line, Bar, Grid, Pie, Page
from pyecharts import options as opts
# 查询过去 8 次数据
time_range = 8

interface = ['充值', '赠送', '支付', '支付回退', '预授权']
bar = (
    Bar()
        .add_xaxis(interface)
        .add_yaxis("支付", [113, 106, 122, 128, 128, 55, 45])
        .add_yaxis("券", [75, 46, 75, 65, 118, 15, 70])
        .add_yaxis("限额限频", [173, 146, 175, 165, 218, 115, 170])
        .add_yaxis("全流程", [65, 46, 70, 65, 108, 45, 40])
        .set_global_opts(title_opts=opts.TitleOpts(title="TPS(当前版本)"))
)
line = Line().add_xaxis([f"2023-07-0{i} 05:04:2{i}" for i in range(1, time_range)]). \
    add_yaxis(interface[0], [random.randint(100, 150) for _ in range(time_range)])

for i, inter in enumerate(interface):
    line.add_yaxis(inter, [random.randint(10 * (i + 1), 100) for _ in range(time_range)],
                   label_opts=opts.LabelOpts(is_show=False))
line.set_global_opts(
    title_opts=opts.TitleOpts(title="性能趋势(支付)", pos_top="48%"),
    legend_opts=opts.LegendOpts(pos_top="48%"),
    yaxis_opts=opts.AxisOpts(
        name="TPS",
        axislabel_opts=opts.LabelOpts(is_show=False),  # 设置label_opts参数
    )
)

grid = Grid().add(bar, grid_opts=opts.GridOpts(pos_bottom="60%")).add(line, grid_opts=opts.GridOpts(pos_top="60%"))

pie = Pie()
pie.add("-", [("已剔除", 2), ("梳理中", 2),  ("已完成",  15), ("优化中", 13), ("时间规划中", 13)])
pie.set_global_opts(title_opts=opts.TitleOpts(title="摸底系统统计"), )
# - `{a}`:表示系列名称。`{b}`:表示数据类别 `{c}`:表示数据值(如10、25、50和15)。`{d}`:表示数据所占的百分比。- `{@[index]}`:表示数据数组中索引为`index`的值。
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{a}{b}: {c} ({d}%)"))

page = Page()
page.add(grid)
page.add(pie)
page.render()

总结

PyCharts 是一个功能强大、易于使用的 Python 数据可视化库。本文以测试工程师的日常工作中的一些数据举例,演示了如何展示测试数据,从而提高工作效率,更好地服务于项目进展。本文仅介绍了 PyCharts 的基本使用和一些常见的应用场景,实际上 PyCharts 还有更多的功能等待我们去探索。希望本文能对大家的工作带来帮助。


资料获取方法

【留言777】

各位想获取源码等教程资料的朋友请 点赞 + 评论 + 收藏 ,三连!

三连之后我会在评论区挨个私信发给你们~

相关推荐
数据智能老司机4 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机5 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机5 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机5 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i5 小时前
drf初步梳理
python·django
每日AI新事件5 小时前
python的异步函数
python
这里有鱼汤6 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook15 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室16 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三17 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试