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

PyCharts 簡(jiǎn)介

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

 

PyCharts 的安裝

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

 

pip install pyecharts

 

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

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

 

缺陷統(tǒng)計(jì)

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

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

 

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

pie = Pie()
pie.add("", [("建議", 33), ("一般", 45), ("嚴(yán)重", 20), ("致命", 2)])
pie.set_global_opts(title_opts=opts.TitleOpts(title="缺陷嚴(yán)重級(jí)別統(tǒng)計(jì)"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c} (b4beckz%)"))
pie.render()

 

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

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

 

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

 

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

 

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

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

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

 

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

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

# 添加通過和失敗的數(shù)據(jù),并設(shè)置為堆疊模式
bar.add_yaxis("通過", [90, 80, 70, 60, 50], stack="總量")
bar.add_yaxis("失敗", [10, 20, 30, 40, 50], stack="總量")

# 設(shè)置全局選項(xiàng),包括標(biāo)題
bar.set_global_opts(title_opts=opts.TitleOpts(title="測(cè)試用例執(zhí)行情況"),)

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

bar.render()

 

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

 

使用JavaScript情況

因?yàn)閜ycharts 底層基于 ECharts,所以 JavaScript代碼也可以在腳本中使用。

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

在 Python 中調(diào)用 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ù),并設(shè)置為堆疊模式
bar.add_yaxis(
    "通過",
    passed_cases,
    stack="總量",
    label_opts=opts.LabelOpts(is_show=False)  # 隱藏通過用例的標(biāo)簽
)

# 使用回調(diào)函數(shù)計(jì)算失敗用例的百分比
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)
    )  # 顯示失敗用例的標(biāo)簽
)

# 設(shè)置全局選項(xiàng),包括標(biāo)題
bar.set_global_opts(
    title_opts=opts.TitleOpts(title="測(cè)試用例執(zhí)行情況"),
)

bar.render()

 

缺陷趨勢(shì)分析

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

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

 

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

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

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

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

# 計(jì)算剩余未修復(fù)的問題單
remaining_defects = []
cumulative_new_defects = 0
cumulative_fixed_defects = 0

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

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

# 向圖表中添加 y 軸數(shù)據(jù)系列
line.add_yaxis("新增缺陷", new_defects)
line.add_yaxis("已修復(fù)缺陷", fixed_defects)
line.add_yaxis("剩余未修復(fù)問題單", remaining_defects)

# 設(shè)置全局選項(xiàng),包括標(biāo)題
line.set_global_opts(title_opts=opts.TitleOpts(title="缺陷趨勢(shì)分析"))

# 渲染圖表
line.render()

 

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

 

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

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

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

 

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

# 創(chuàng)建 Line 對(duì)象

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("已修復(fù)缺陷", fixed_defects)
        .add_yaxis("剩余未修復(fù)問題單", remaining_defects)
        # 設(shè)置全局選項(xiàng),包括標(biāo)題
        .set_global_opts(title_opts=opts.TitleOpts(title="缺陷趨勢(shì)分析"),
                         legend_opts=opts.LegendOpts(pos_top="48%"),)

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

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

# 創(chuàng)建 Grid 對(duì)象,并添加 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在測(cè)試工作中的應(yīng)用 圖7

 

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

# 創(chuàng)建 Bar 對(duì)象
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 對(duì)象
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)

# 設(shè)置 is_smooth=True 參數(shù)使折線平滑顯示
line.add_yaxis("新增缺陷", new_defects)
line.add_yaxis("已修復(fù)缺陷", fixed_defects)
line.add_yaxis("剩余未修復(fù)問題單", remaining_defects)

# 使用 overlap() 方法將 Line 圖表疊加到 Bar 圖表中
bar.overlap(line)

# 設(shè)置全局選項(xiàng),包括標(biāo)題
bar.set_global_opts(title_opts=opts.TitleOpts(title="缺陷趨勢(shì)分析"))

# 渲染圖表
bar.render()

 

將多張圖組合在一個(gè)page 中(page)

【Python】數(shù)據(jù)可視化利器PyCharts在測(cè)試工作中的應(yīng)用 圖8

 

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

# 創(chuàng)建 Line 對(duì)象
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("已修復(fù)缺陷", fixed_defects)
        .add_yaxis("剩余未修復(fù)問題單", remaining_defects)
        # 設(shè)置全局選項(xiàng),包括標(biāo)題
        .set_global_opts(title_opts=opts.TitleOpts(title="趨勢(shì)分析(缺陷/用例)"),
                         legend_opts=opts.LegendOpts(pos_top="48%"),)

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

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

# 創(chuàng)建 Grid 對(duì)象,并添加 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), ("嚴(yán)重", 20), ("致命", 2)])
pie.set_global_opts(title_opts=opts.TitleOpts(title="缺陷嚴(yán)重級(jí)別統(tǒng)計(jì)"))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c} (zd2yluv%)"))
# 指定顏色
colors = ["#0000FF", "#4169E1", "#1E90FF", "#00BFFF"]
pie.set_colors(colors)

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

# 渲染圖表
page.render()

 

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

【Python】數(shù)據(jù)可視化利器PyCharts在測(cè)試工作中的應(yīng)用 圖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 = ['充值', '贈(zèng)送', '支付', '支付回退', '預(yù)授權(quán)']
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(當(dāng)前版本)"))
)
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="性能趨勢(shì)(支付)", pos_top="48%"),
    legend_opts=opts.LegendOpts(pos_top="48%"),
    yaxis_opts=opts.AxisOpts(
        name="TPS",
        axislabel_opts=opts.LabelOpts(is_show=False),  # 設(shè)置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), ("時(shí)間規(guī)劃中", 13)])
pie.set_global_opts(title_opts=opts.TitleOpts(title="摸底系統(tǒng)統(tǒng)計(jì)"), )
# - `{a}`:表示系列名稱。``:表示數(shù)據(jù)類別 `{c}`:表示數(shù)據(jù)值(如10、25、50和15)。`gfdwaj7`:表示數(shù)據(jù)所占的百分比。- `{@[index]}`:表示數(shù)據(jù)數(shù)組中索引為`index`的值。
pie.set_series_opts(label_opts=opts.LabelOpts(formatter="{a}: {c} (ryh9p3f%)"))

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

 

總結(jié)

PyCharts 是一個(gè)功能強(qiáng)大、易于使用的 Python 數(shù)據(jù)可視化庫。本文以測(cè)試工程師的日常工作中的一些數(shù)據(jù)舉例,演示了如何展示測(cè)試數(shù)據(jù),從而提高工作效率,更好地服務(wù)于項(xiàng)目進(jìn)展。本文僅介紹了 PyCharts 的基本使用和一些常見的應(yīng)用場(chǎng)景,實(shí)際上 PyCharts 還有更多的功能等待我們?nèi)ヌ剿?。希望本文能?duì)大家的工作帶來幫助。

轉(zhuǎn)載自:https://www.cnblogs.com/Detector/p/17548677.html