Python统计FreeMind测试用例数量
1.编写背景
由于最近编写迭代测试报告时发现缺少测试用例数量的统计,为了更好地分析迭代测试质量,需统计测试用例与bug的数量占比,故编写此脚本用于统计测试用例的数量(现测试用例编写工具为飞书思维导图,其导出格式为freemind)
2.code
代码如下:
# coding=utf-8
import xml.etree.ElementTree as ET
import xlwt
import argparse
# 设置从第n+1行开始插入excel
row_num = 1
# 设置用例名称所在列
name_col_num = 1
# 设置期望结果所在列
expect_col_num = 4
# 设置用例类型所在列、用例类型
type_col_num = 6
testcase_type = '功能测试'
# 用于设置插入excel的标题
def set_excel_header():
n = 0
header = ['所属模块', '用例标题', '前置条件', '步骤', '预期', '优先级', '用例类型', '用例编号']
for i in header:
ws.write(0, n, i)
n += 1
# 该方法用于插入excel
def insert_excel(text, row, col):
ws.write(row, col, text)
# 该方法用于生成用例名称,格式为模块1_模块2_模块3
def per_round(element, charset):
global row_num
charset += '_' + element.get('TEXT')
for child in element:
per_fun(child, charset)
value = child.get('TEXT')
if child.find('node') is None and value is not None:
insert_excel(charset, row_num, name_col_num)
insert_excel(testcase_type, row_num, type_col_num)
row_num += 1
def per_fun(element, charset):
global row_num
if element is not None:
if len(element) == 0:
value = element.get('TEXT')
# 将思维导图中最后一列设置为期望结果,并写入excel
insert_excel(value, row_num, expect_col_num)
else:
# 将思维导图中除最后一列外,以"_"间隔组成用例名称
per_round(element, charset)
# 重新设置生成文件名
def set_file_name(input_filename, output_filename):
if output_filename == '.xls':
str1 = input_filename.replace('.mm', '') + output_filename
return str1
else:
return output_filename
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, dest='inputfile', default='test.mm', help='Default inputfile is test.mm')
parser.add_argument('-o', '--output-file', type=str, dest='outputfile', default='.xls',
help='Default outputfile is test.xls')
args = parser.parse_args()
if args.inputfile is None:
parser.print_help()
exit()
print('开始将', args.inputfile, '文件转换为excel')
first_node = ET.parse(args.inputfile).getroot().find('node')
str2 = '#'
sheet = xlwt.Workbook()
ws = sheet.add_sheet('test', cell_overwrite_ok=True)
set_excel_header()
per_round(first_node, str2)
file_name = set_file_name(args.inputfile, args.outputfile)
sheet.save(file_name)
print('导出完成:', file_name, ',总计', row_num - 1, '条用例')