Python知識分享網(wǎng) - 專業(yè)的Python學習網(wǎng)站 學Python,上Python222
【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用
匿名網(wǎng)友發(fā)布于:2023-07-14 13:30:19
(侵權舉報)

PyCharts 簡介

PyCharts 是一個基于 Python 的數(shù)據(jù)可視化庫,它支持多種圖表類型,如折線圖、柱狀圖、餅圖等。PyCharts 提供了簡潔的 API,使得用戶能夠輕松地創(chuàng)建各種圖表,同時支持個性化的配置,以滿足不同需求。PyCharts 的底層依賴于 ECharts,這使得它在功能和性能上都具有很高的優(yōu)勢。

 

PyCharts 的安裝

PyCharts 的安裝非常簡單,只需在命令行中輸入以下命令:

 

pip install pyecharts

 

安裝完成后,即可在 Python 代碼中導入 PyCharts 庫并開始使用。

作為軟件測試工程師,我們經(jīng)常需要處理測試過程中的數(shù)據(jù)。文不如表、表不如圖,通過 PyCharts,我們可以輕松的將這些數(shù)據(jù)以直觀的形式展示出來,從而更好地分析問題、匯報進度。

 

缺陷統(tǒng)計

在軟件測試過程中,我們需要對發(fā)現(xiàn)的缺陷進行統(tǒng)計和分析。以下代碼演示了如何使用 PyCharts 創(chuàng)建一個餅圖,展示各個嚴重級別的缺陷數(shù)量:

【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖1

 

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="缺陷嚴重級別統(tǒng)計"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c} (udwo7mq%)"))
pie.render()

 

執(zhí)行上面的代碼會自動的在同級目錄生成一個 render.xml 文件,使用瀏覽器打開就可以看到展示了不同嚴重級別的缺陷數(shù)量及其占比的餅圖。我們可以依據(jù)這個圖更好地制定測試策略和優(yōu)先級。

當然,我們也可以在 render 之前加上下面這段代碼來指定顏色,得到下面這張圖:

 

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

 

【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖2

 

測試用例執(zhí)行情況

在執(zhí)行測試用例時,我們需要關注測試用例的執(zhí)行情況,如通過率、失敗率等。以下代碼演示了如何使用 PyCharts 創(chuàng)建一個堆疊柱狀圖,展示各個模塊的測試用例執(zhí)行情況:

【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖3

 

from pyecharts.charts import Bar
from pyecharts import options as opts

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

# 添加通過和失敗的數(shù)據(jù),并設置為堆疊模式
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="測試用例執(zhí)行情況"),)

# 設置系列選項,包括標簽格式
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position="outside", formatter="{c}%")) # 顯示百分比的標簽格式

bar.render()

 

這段代碼創(chuàng)建了一個堆疊柱狀圖,展示了各個模塊的測試用例通過和失敗數(shù)量。通過這樣的圖表,我們可以直觀地了解到各個模塊的測試情況,從而更好地調整測試計劃和資源分配。

 

使用JavaScript情況

因為pycharts 底層基于 ECharts,所以 JavaScript代碼也可以在腳本中使用。

【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用  圖4

在 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]

# 添加通過和失敗的數(shù)據(jù),并設置為堆疊模式
bar.add_yaxis(
    "通過",
    passed_cases,
    stack="總量",
    label_opts=opts.LabelOpts(is_show=False)  # 隱藏通過用例的標簽
)

# 使用回調函數(shù)計算失敗用例的百分比
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="測試用例執(zhí)行情況"),
)

bar.render()

 

缺陷趨勢分析

在項目進展過程中,我們需要關注缺陷的趨勢,以評估項目質量和進度。以下代碼演示了如何使用 PyCharts 創(chuàng)建一個折線圖,展示項目中缺陷的趨勢變化:

【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖5

 

from pyecharts.charts import Line
from pyecharts import options as opts

# 創(chuàng)建 Line 對象
line = Line()

# 添加 x 軸數(shù)據(jù)
line.add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])

# 新增問題單和已修復問題單的數(shù)據(jù)
new_defects = [50, 35, 28, 20, 5]
fixed_defects = [5, 20, 30, 33, 50]

# 計算剩余未修復的問題單
remaining_defects = []
cumulative_new_defects = 0
cumulative_fixed_defects = 0

# 遍歷新增問題單和已修復問題單的數(shù)據(jù)
for new, fixed in zip(new_defects, fixed_defects):
    # 累積計算新增問題單和已修復問題單的數(shù)量
    cumulative_new_defects += new
    cumulative_fixed_defects += fixed

    # 計算剩余未修復的問題單,并將結果添加到 remaining_defects 列表中
    remaining_defects.append(cumulative_new_defects - cumulative_fixed_defects)

# 向圖表中添加 y 軸數(shù)據(jù)系列
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()

 

這段代碼創(chuàng)建了一個折線圖,展示了項目中新增缺陷和已修復缺陷的數(shù)量變化。通過這樣的圖表,我們可以直觀地了解到項目的質量趨勢,從而更好地制定測試策略和計劃。

 

將兩張圖放在一個組合里(grid)

【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖6

體現(xiàn)隨著時間的變化,執(zhí)行用例和 bug 情況的變化:

 

from pyecharts.charts import Line, Bar, Grid
from pyecharts import options as opts

# 創(chuàng)建 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%"),)

)
# 添加執(zhí)行用例數(shù)的數(shù)據(jù)
executed_cases = [150, 170, 195, 110, 86]

# 創(chuàng)建 Bar 對象
bar = (Bar().add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])
.add_yaxis("執(zhí)行用例數(shù)", executed_cases)
       )

# 創(chuàng)建 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】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖7

 

from pyecharts.charts import Line, Bar
from pyecharts import options as opts

# 創(chuàng)建 Bar 對象
bar = Bar()
bar.add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])

# 添加執(zhí)行用例數(shù)的數(shù)據(jù)
executed_cases = [15, 17, 19.5, 11, 3]
bar.add_yaxis("執(zhí)行用例數(shù)(/百條)", executed_cases)

# 創(chuàng)建 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 參數(shù)使折線平滑顯示
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】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖8

 

from pyecharts.charts import Line, Bar, Page, Pie, Grid
from pyecharts import options as opts

# 創(chuàng)建 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%"),)

)
# 添加執(zhí)行用例數(shù)的數(shù)據(jù)
executed_cases = [150, 170, 195, 110, 86]

# 創(chuàng)建 Bar 對象
bar = (Bar().add_xaxis(["第一周", "第二周", "第三周", "第四周", "第五周"])
.add_yaxis("執(zhí)行用例數(shù)", executed_cases)
       )

# 創(chuàng)建 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="缺陷嚴重級別統(tǒng)計"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c} (pcteamn%)"))
# 指定顏色
colors = ["#0000FF", "#4169E1", "#1E90FF", "#00BFFF"]
pie.set_colors(colors)

# 創(chuàng)建 Page 對象,并添加圖表
page = Page()
page.add(grid)
page.add(pie)

# 渲染圖表
page.render()

 

實際應用:常態(tài)化性能壓測數(shù)據(jù)統(tǒng)計

【Python】數(shù)據(jù)可視化利器PyCharts在測試工作中的應用 圖9

 

import random
from pyecharts.charts import Line, Bar, Grid, Pie, Page
from pyecharts import options as opts
# 查詢過去 8 次數(shù)據(jù)
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參數(shù)
    )
)

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), ("優(yōu)化中", 13), ("時間規(guī)劃中", 13)])
pie.set_global_opts(title_opts=opts.TitleOpts(title="摸底系統(tǒng)統(tǒng)計"), )
# - `{a}`:表示系列名稱。``:表示數(shù)據(jù)類別 `{c}`:表示數(shù)據(jù)值(如10、25、50和15)。`unu78vw`:表示數(shù)據(jù)所占的百分比。- `{@[index]}`:表示數(shù)據(jù)數(shù)組中索引為`index`的值。
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{a}: {c} (nqwsygw%)"))

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

 

總結

PyCharts 是一個功能強大、易于使用的 Python 數(shù)據(jù)可視化庫。本文以測試工程師的日常工作中的一些數(shù)據(jù)舉例,演示了如何展示測試數(shù)據(jù),從而提高工作效率,更好地服務于項目進展。本文僅介紹了 PyCharts 的基本使用和一些常見的應用場景,實際上 PyCharts 還有更多的功能等待我們去探索。希望本文能對大家的工作帶來幫助。

轉載自:https://www.cnblogs.com/Detector/p/17548677.html