首页 > 编程语言 >【Python】数据可视化利器PyCharts在测试工作中的应用

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

时间:2023-07-13 10:33:51浏览次数:38  
标签:yaxis Python PyCharts add 可视化 defects new fixed opts

PyCharts 简介

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

PyCharts 的安装

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

pip install pyecharts

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

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

缺陷统计

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

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 之前加上下面这段代码来指定颜色,得到下面这张图:

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

image

测试用例执行情况

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

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代码也可以在脚本中使用。
image

在 Python 中调用 JavaScript 代码:

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 创建一个折线图,展示项目中缺陷的趋势变化:
image

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)

image

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

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)

image

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)

image

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()

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

image

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 还有更多的功能等待我们去探索。希望本文能对大家的工作带来帮助。

标签:yaxis,Python,PyCharts,add,可视化,defects,new,fixed,opts
From: https://www.cnblogs.com/Detector/p/17548677.html

相关文章

  • 可视化大屏,提升水灾应急管理效能
    随着气候变化和城市化进程的加剧,暴雨天气引发的水灾风险日益凸显。在面对这种自然灾害时,如何高效、及时地应对、减轻损失成为了当务之急。水灾应急管理平台的可视化大屏为相关部门和决策者提供了实时、全面的信息展示和决策支持,大大提升了应急管理的效能。  可视化大屏能够实......
  • PYTHON随笔-logging
    PYTHON随笔-loggingimportloggingfromlogging.handlersimportRotatingFileHandlergLogFile='/home/mcs/log/dbm_py.log'LOG_FORMAT="%(asctime)s[%(levelname)-5s][%(filename)s:%(lineno)s]-%(message)s"#日志输出的格式#-8表示占位符,让输出左对齐,输......
  • python-pymysql-类对象映射为sql语句
    查询语句importpymysqlclassUserQuery:def__init__(self,name=None,age=None,email=None):self.name=nameself.age=ageself.email=emaildefselect_data(table,condition):#连接到数据库connection=pymysql.connec......
  • Python用Keras神经网络序列模型回归拟合预测、准确度检查和结果可视化|附代码数据
    原文链接:http://tecdat.cn/?p=23573最近我们被客户要求撰写关于Keras神经网络序列模型的研究报告,包括一些图形和统计输出。我们可以很容易地用Keras序列模型拟合回归数据并预测测试数据。  在这篇文章中,我们将简要地学习如何用Python中的Keras神经网络API拟合回归数据。我们将......
  • R语言线性混合效应模型(固定效应&随机效应)和交互可视化3案例|附代码数据
    全文下载链接:http://tecdat.cn/?p=23050最近我们被客户要求撰写关于线性混合效应模型的研究报告,包括一些图形和统计输出。在本文中,我们将用R语言对数据进行线性混合效应模型的拟合,然后可视化你的结果线性混合效应模型是在有随机效应时使用的,随机效应发生在对随机抽样的单位进行......
  • 双服务台串联排队系统——Python仿真
    串联排队系统是一种常见的排队系统结构,由多个单一排队系统按照特定的顺序连接而成。在串联排队系统中,一个排队系统的输出将成为下一个排队系统的输入,从而形成连续的流程。这种系统结构可以用于模拟和优化许多现实世界中的流程,如生产线、物流运输等。一、双服务台串联排队系统模......
  • 如何使用Python制作交互式股票K线图?
    如何使用Python制作交互式股票K线图?如何使用Python制作交互式股票K线图?-知乎(zhihu.com)州的先生  在之前的文章中,我们介绍了使用PyQtGraph在PyQt5中绘制股票K线图:PythonGUI教程(十三):在GUI中使用pyqtgraph绘图库​zmister.com/archives/187.html以及使......
  • Python3.6下scrapy框架的安装
    命令安装,提示  FailedbuildingwheelforTwistedMicrosoftVisualC++14.0isrequired...  总结pipinstallwheel 下载Twisted包安装下载Scrapy包安装下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/详细解决方案1首先考虑使用最简单的方法安装pipinstallsc......
  • Python-[]列表.py
     19printlist;            #输出完整列表 20printlist[0]  #输出列表第一个元素 21printlist[1:3]#输出列表下标1~3之间的元素(和字符串一样,含头不含尾) 22printlist[2:] #输出下标2以后所有的元素(包含下标2的元素) 23printtinylist*2     ......
  • Python-()元组.py
     1#!/usr/bin/python 2#coding=UTF-8 3 4 5''' 6Python元组 7 8元组是另一个数据类型,类似于List(列表)。 9 10元组用()标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。 11''' 12 13 14tuple=('runoob',786,2.23,'......