做个简单的bug分析


前言

作为一名测试人员,bug分析也是必备的技能之一,在分析之前需要把数据收集一遍,有用过禅道的同学应该都用过它的数据可视化,没用禅道做bug管理,也可以用Excel表格来做数据可视化,这篇文章主要介绍用Python来做bug数据可视化。


提示:以下是本篇文章正文内容,下面案例可供参考

第三方库

pygal: Python有很多数据可视化库,像matplotlib、seaborn、plotly,我这里用的是一个可视化库-Pygal.
openpyxl: 用于读取和写入 Excel 2010 xlsx/xlsm/xltx/xltm 文件.
flask: Flask是一个Python编写的Web 微框架,可以使用Python快速实现一个网站或Web服务.

安装

pip install pygal
pip install openpyxl
pip install falsk

正文

一、pygal的使用例子

Pygal的用法很便捷,主要分三步:
1、生成图表对象
2、导入数据
3、导出图像

# 导入pygal库
import pygal  

# 创建柱状图对象
bar_chart = pygal.Bar() 

# 图表命名
bar_chart.title = '测试部二季度发现缺陷前五数据'

# 添加数据
bar_chart.add('A同学', 487) 
bar_chart.add('B同学', 469) 
bar_chart.add('C同学', 343) 
bar_chart.add('D同学', 336) 
bar_chart.add('E同学', 322) 

# 在浏览器中查看
#bar_chart.render_in_browser()

# 导出为矢量图形
bar_chart.render_to_file('BUG.svg') 

bug.svg

二、封装图表函数

我这里的思路是把图表的常用设置给固化,是整合几个图表后才设置的参数,👇有常用的参数说明。

def set_chart_cfg(title,x_title,y_title,x_labels=None,y_labels_max=None,bar_type=pygal.Bar,width=500,height=400):
    # 创建柱状图对象
    # chart = pygal.HorizontalStackedBar(
    chart = bar_type(
        explicit_size=True,
        width=width,
        height=height,
        # 图标题
        title=title,
        # x轴标题
        # x_title=x_title,  
        # y轴标题
        y_title=y_title,  
        # label设置
        # 显示x轴标签
        show_x_labels=True,  
        # x轴标签倾斜角度
        x_label_rotation=20,  
        # 自定义x轴标签
        # x_labels=list('ABCD'),  
        # y轴刻度值格式化输出
        # value_formatter=lambda x: "%.2f" % x,  
        print_values=True,
        # show_y_labels=False,
        y_labels = [i for i in range(0,y_labels_max+5,5)],
        x_labels = x_labels,
        inner_radius=.4,
        rounded_bars= 8,
    )
    return chart

常见的参数设置

chart = pygal.Bar(margin_bottom=10,#图与低端距离,类似的有上下左右
                  height=450,
                  #style=NeonStyle,#设置绘图风格,pygal拥有23种style,
                  #其它style可选:'BlueStyle', 'CleanStyle', 'DarkColorizedStyle', 'DarkGreenBlueStyle', 'DarkGreenStyle', 'DarkSolarizedStyle', 'DarkStyle', 'DarkenStyle', 'DefaultStyle', 'DesaturateStyle', 'LightColorizedStyle', 'LightGreenStyle', 'LightSolarizedStyle', 'LightStyle', 'LightenStyle', 'NeonStyle', 'ParametricStyleBase', 'RedBlueStyle', 'RotateStyle', 'SaturateStyle', 'SolidColorStyle', 'Style', 'TurquoiseStyle'
                  
                  ##title设置
                  title=u'Some points', #图标题
                  x_title='X Axis',#x轴标题
                  y_title='Y Axis',#y轴标题
                  
                  ##label设置
                  show_x_labels=True,#显示x轴标签
                  x_label_rotation=20,#x轴标签倾斜角度
                  x_labels = list('ABCD'),#自定义x轴标签
                  value_formatter = lambda x: "%.2f" % x,#y轴刻度值格式化输出
                  
                  ##图例legend设置
                  show_legend=True,#开启图例
                  legend_at_bottom=True,#图例放置于底部
                  legend_at_bottom_columns=2,#图例标签显示行数
                  legend_box_size=12,#图例前箱子大小
                  
                  ##坐标轴axis设置
                  include_x_axis=True,#坐标轴开启
                  range=(0, 30),#设置y轴刻度值范围
                  
                  secondary_range=(10, 25),#第二坐标轴刻度范围
                  xrange=(0,10),#x轴刻度范围
                  
                  ##柱子上text设置
                  print_values=True,#开启柱子上文本
                  print_values_position='top',#文本位置
                  style=LightSolarizedStyle(
                  value_font_family='googlefont:Raleway',#文本字体设置
                  value_font_size=15,#大小
                  value_colors=('red','blue'),#颜色设置
                  ),
                  
                 )

三、获取缺陷数据

我是仿照禅道导出来的数据格式,筛选我需要的字段。
excel

写个方法去读取Excel表格里我想要的数据

    def get_bug_data(self,field):
        bug_data = []
        bug_type_data = self.bug_dict[field]
        data_set = set(bug_type_data)
        for i in list(data_set):
            bug_data.append([i,bug_type_data.count(i)])
        return bug_data

根据自己的需求来筛选、展示数据,这里主要是看程序猿不小心写错的bug主要集中在哪些模块

    def get_loser_data(self):
        loser_index = 10
        # 收集每个解决者的模块
        loser_mode = {}
        for bug_list in self.loser_data:
            if bug_list[loser_index] in loser_mode.keys():
                loser_mode[bug_list[loser_index]].append(bug_list[1])
            else:
                loser_mode[bug_list[loser_index]] = [bug_list[1]]
        # 模块计数
        for k,v in loser_mode.items():
            response = []
            data_set = set(v)
            for i in list(data_set):
                response.append([i,v.count(i)])
            loser_mode[k] = response
        return loser_mode

把Excel表格里读取的数据制作图标

def render_demo(get_bug):
    data1 = get_bug.get_bug_data('严重程度')
    chart1 = set_chart_cfg('按Bug严重程度统计','严重等级','',y_labels_max=max([i[1] for i in data1]),bar_type=pygal.Pie)
    for i in data1:
        chart1.add(str(i[0]), [i[1]])
    return chart1.render_data_uri()

四、web展示结果

展示方式有多种,开头有提到可以直接保存为矢量图、保存为HTML、web展示。

# 访问地址自由定义
@app.route('/')
def line_route():
	'''
	:xxx_obj_data:读取Excel表格数据的对象
	:xxx_chart1:html要读取的变量名
	'''
    xxx_demo = render_demo(xxx_obj_data)
    return render_template('charts.html',
                           xxx_chart1 = xxx_demo[0],
                           )

if __name__ == '__main__':
    app.run()

demo图,比较粗糙,将就看吧。
demo


欢迎小伙伴关注微信公众号ID:gameTesterGz
或关注我的优快云:https://blog.youkuaiyun.com/qq_32557025
谢谢各位的关注、点赞!
微信二维码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

游戏测试-AJian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值