一、封装一个绘图类
1、导入模块
# coding=utf-8
import matplotlib.pyplot as plt
2、解决中文和负号显示乱码
plt.rcParams['font.sans-serif'] = ['simhei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
3、封装绘图类
绘制统计图后,需要有一个路径保存
class Draw:
"""
绘图
"""
def __init__(self, reportpath):
self.reportpath = reportpath
4、绘制饼状图(只显示大于1的数据,避免重叠)
# 只显示>1标签
def my_autopct(self, pct, limit=1):
return ('%.2f%%' % pct) if pct > limit else ''
def drawing_cake(self, amounts, lebals, project_name, title):
"""
绘制饼状图
:param amounts:数量列表
:param lebals:图例
:param project_name:项目名称(用于保存路径、文件名称)
:param title:标题(用于保存路径、文件名称)
"""
plt.figure(figsize=(10, 10))
indic = []
# 将数据最大的突出显示
for value in amounts:
if value == max(amounts):
indic.append(0.1)
else:
indic.append(0)
plt.pie(amounts,
# labels=lebals,
startangle=90, # 起始角度
shadow=True, # 阴影
# 是否分割出饼图的哪一块 默认是None,不分割的时候为0,分割的时候传入0.1
explode=tuple(indic), # tuple方法用于将列表转化为元组
autopct=self.my_autopct # 比例数值格式
# autopct='%1.2f%%'
)
# 给图像加上图例,并显示在左上方
plt.legend(lebals, loc='upper left')
plt.title(project_name + ' ' + title)
filename = self.reportpath + "/" + project_name + ' ' + title + ".png"
plt.savefig(filename)
plt.show()
plt.cla()
plt.close("all")
def drawing_histogram_tied(self):
"""
绘制柱状图,见后文
"""
pass
def drawing_linechart_more(self):
"""
绘制折线图,见后文
"""
pass
二、封装饼状图数据统计类
1、导入依赖的模块
# coding=utf-8
from ana_jira import Ana_jira
from draw import Draw
2、封装数据统计类
class Cake:
def __init__(self, project_name, test_jira, first_day, last_day, types, weeks, report_path):
self.project_name = project_name
self.test_jira = test_jira
self.first_day = first_day
self.last_day = last_day
self.types = types
self.weeks = weeks
self.report_path = report_path
3、统计缺陷严重性分布情况
方法一:
直接通过查询JQL统计
def cake(self, ana_type):
"""
绘制饼状图(严重性分布、优先级分布)
:param project_name:
:return:
"""
# ana_type = '严重性'
# 获取不同优先级\严重等级JQL
amount = []
severity = []
for i in self.types[ana_type]:
severity.append(i)
serch_jql = """
project = "{}" AND {} = {} AND created >= {} AND created <= {}
""".format(self.project_name, ana_type, i, self.first_day, self.last_day)
onetypy_numbers = Ana_jira(self.test_jira, serch_jql).req_jira()
amount.append(onetypy_numbers) # 缺陷数量列表
# 绘制饼状图: 优先级\严重性
if sum(amount) > 0:
# severity = ['Fatal', 'Severe', 'Average', 'Minor']
Draw(self.report_path).drawing_cake(amount, severity, self.project_name,
'Issues Severity Distribution (Iteration Week)')
print('项目{}最近{}周{}分布情况统计完成'.format(self.project_name, self.weeks, ana_type))
else:
print('项目{}最近{}周未提交缺陷,无法统计{}分布情况'.format(self.project_name, self.weeks, ana_type))
def cake_repairs_cycle(self):
"""
绘制饼图:修复周期(见后文)
:param project_name:
:return:
"""
pass
方法二:
通过查询数据库缺陷记录,即 jira_issues 表获取缺陷严重性分布
def cake_db(self):
sql = """
SELECT severity_level,COUNT(id) AS amount
FROM jira_issues
WHERE project='{}' AND created BETWEEN '{}' AND '{}'
GROUP BY project,severity_level
""".format(self.project_name, self.first_day, self.last_day)
from common.mysqluntil import MysqlUntil
results = MysqlUntil().mysql_select(sql)
amount, severity = [], []
for result in results:
severity.append(result['severity_level'])
amount.append(result['amount'])
if sum(amount) > 0:
# severity = ['Fatal', 'Severe', 'Average', 'Minor']
Draw(self.report_path).drawing_cake(amount, severity, self.project_name,
'Issues Severity Distribution (Iteration Week)')
print('项目{}最近{}周统计完成'.format(self.project_name, self.weeks))
else:
print('项目{}最近{}周未提交缺陷,无法统计分布情况'.format(self.project_name, self.weeks))
方法三、
也可以通过测试人员统计表统计缺陷的严重性分布,跟上一方法是一样的
三、执行统计,绘制缺陷严重性分布
1、根据要统计的时间周期获取日期()
from datetime import datetime
import time
import datetime
获取前N周的所有日期列表
# 获取前N周的所有日期(weeks=N)
def getBeforeWeekDays(self, weeks):
# 0,1,2,3,4,5,6,分别对应周一到周日
week = datetime.datetime.now().weekday()
start = 7 * weeks + week
end = week
days_list = []
for index in range(start, end, -1):
day = self.getdate(index)
days_list.append(day)
return days_list
在这里加个判断,限制下统计周期最大为4周:周期一般为迭代周期,迭代不宜过长,并且周期过长统计图显示容易出问题
weeks = 2 # 缺陷统计N周前的数据
if isinstance(weeks, int):
if 1 <= weeks <= 4:
weeks = int(weeks)
else:
weeks = 2
else:
weeks = 2
# 缺陷统计分析的开始日期、结束日期
days_list = getBeforeWeekDays(weeks)
first_day, last_day = days_list[0], days_list[-1]
2、获取报表保存路径
import os
根据当前路径设置保存路径
file_path_now = os.path.abspath('.') # 获取当前目录
report_path = file_path_now + "/report/" + date_now # 拼接图片存储地址(拼接时间,以防重复被覆盖)
# 如果不存在,新建一个文件夹
if not os.path.exists(report_path):
os.mkdir(report_path)
3、定义统计类型
types = {
'优先级': ['Highest', 'High', 'Medium', 'Low', 'Lowest'],
'严重性': [u'致命', u'严重', u'一般', u'优化']
}
4、执行统计,查看报表统计结果
c = Cake(project_name, test_jira, first_day, last_day, types, weeks, project_report_path)
c.cake('严重性') # (项目)严重性分布、优先级分布
c.cake_db()