突然有一天老大把我叫到旁边,语重心长对我说,咱们写的用例文档都是word格式,上面就要一个excel格式测试用例文档,OK搞起来
先看下word文档格式
基本上都是word里面嵌套表格写入用例还好比较规范,有的数据提取需要正则表达式,引用docx模块,上代码!
# -*- coding: utf-8 -*-
# @Auth : hanxu
# @Time : 2019-05-28
"""
从doc文件提取用例工具类
"""
import docx
import xlwt
import re
from xlwt import Workbook
def docx_try(path=''):
# doc = docx.Document(r'C:\Users\hanxu\Desktop\FZ.docx')
doc = docx.Document(path)
# 创建excel 写入
book = Workbook(encoding='utf-8')
sheet1 = book.add_sheet('Sheet 1')
# 用例数据文档表格从第1个开始
items_no = 0
for t_index in range(0, len(doc.tables)):
t = doc.tables[t_index]
# 数据从第二行开始
sql = ''
data_str = ''
shell_str = ''
test_items = ''
expected_txt = ''
expected_result = ''
xml_property = ''
items_no += 1
for r_index in range(0, len(t.rows)):
r = t.rows[r_index]
for c in range(0, len(r.cells)):
print(r.cells[c].text)
if '测试目的' == r.cells[c].text:
test_items = r.cells[c + 1].text
if '测试输入' == r.cells[c].text:
# 提取sql
sql = str_sql(r.cells[c + 1].text)
print(sql)
xml_property = str_properties(r.cells[c + 1].text)
print(xml_property)
if '预期结果' == r.cells[c].text:
expected_txt = r.cells[c + 1].text
expected_result = expect_split(expected_txt)
if '测试数据' == r.cells[c].text:
data_str = str_data(r.cells[c + 1].text)
print(data_str)
if '测试脚本' == r.cells[c].text:
shell_str = str_shell((r.cells[c + 1].text).strip())
print(shell_str)
# if '配置文件' == r.cells[c].text:
# property = r.cells[c + 1].text
sheet1.write(t_index, 0, items_no)
sheet1.write(t_index, 1, test_items)
sheet1.write(t_index, 2, expected_txt)
if data_str:
sheet1.write(t_index, 3, data_str.split(';')[0])
sheet1.write(t_index, 4, data_str.split(';')[1])
else:
sheet1.write(t_index, 3, '')
sheet1.write(t_index, 4, '')
if shell_str:
sheet1.write(t_index, 5, shell_str.split(';')[0])
sheet1.write(t_index, 6, shell_str.split(';')[1])
if sql:
sheet1.write(t_index, 7, sql[:-1])
sheet1.write(t_index, 8, xml_property)
sheet1.write(t_index, 9, expected_result)
book.save('C:/Users/hanxu/Desktop/FZ2.xls')
def expect_split(txt):
res = ''
pattern = re.compile(r'(?:为:)\d+')
list_expect = pattern.findall(txt)
if len(list_expect) == 3:
res = "calltype|%s listtype|%s barringtype|%s" % (
list_expect[0][list_expect[0].find(':') + 1:], list_expect[1][list_expect[1].find(':') + 1:],
list_expect[2][list_expect[2].find(':') + 1:])
if len(list_expect) == 4:
res = "calltype|%s listtype|%s barringtype|%s keynumber|%s" % (
list_expect[0][list_expect[0].find(':') + 1:], list_expect[1][list_expect[1].find(':') + 1:],
list_expect[2][list_expect[2].find(':') + 1:], list_expect[3][list_expect[2].find(':') + 1:])
return res
# barringtype = ''
# listType = ''
# category = ''
# calltype = ''
# if txt.find('barringtype') > 0:
# b_txt = txt[txt.find('barringtype'):]
# barringtype = 'BarringType=%s' % b_t xt[b_txt.find(':') + 1:b_txt.find(':') + 2]
# if txt.find('listType') > 0:
# l_txt = txt[txt.find('listType'):]
# listType = 'listType=%s' % l_txt[l_txt.find(':') + 1:l_txt.find(':') + 2]
#
# if txt.find('category') > 0:
# c_txt = txt[txt.find('category'):]
# category = 'category=%s' % c_txt[c_txt.find(':') + 1:c_txt.find(':') + 2]
#
# if txt.find('calltype') > 0:
# ca_txt = txt[txt.find('calltype'):]
# calltype = 'CallType=%s' % ca_txt[ca_txt.find(':') + 1:ca_txt.find(':') + 2]
# return '%s %s %s %s' % (barringtype, listType, category, calltype)
def str_sql(context):
"""
根据字符串截取其中sql
:param context:
:return:
"""
index_0 = context.find('update') # 第一个update
sql = ''
if index_0 > 0:
context_0 = context[index_0:] # update-->最后
sql += context_0[:context_0.find(';')] + ";"
text_1 = context_0[context_0.find(';'):] # 第一个update分号到最后
index_1 = text_1.find('update') # 查找第二个update
if index_1 > 0:
context_1 = text_1[index_1:] # 从第二个update-->最后
sql += context_1[:context_1.find(';')] + ';'
text_2 = context_1[context_1.find(';'):]
index_2 = text_2.find('update') # 查找第三个update
if index_2 > 0:
context_2 = text_2[index_2:] # 第三个update-->最后
sql += context_2[:context_2.find(';')] + ';'
text_3 = context_2[context_2.find(';'):]
index_3 = text_3.find('update') # 查找第四个update
if index_3 > 0:
context_3 = text_3[index_3:] # 第四个update-->最后
sql += context_3[:context_3.find(';')] + ';'
text_4 = context_3[context_3.find(';'):]
index_4 = text_4.find('update') # 查找第五个update
if index_4 > 0:
context_4 = text_4[index_4:] # 第五个update-->最后
sql += context_4[:context_4.find(';')] + ';'
text_5 = context_4[context_4.find(';'):]
index_5 = text_5.find('update') # 查找第六个update
if index_5 > 0:
context_5 = text_5[index_5:] # 第六个update-->最后
sql += context_5[:context_5.find(';')] + ';'
return sql
def str_data(text):
"""
处理测试数据例如:
数据库:sip_hjljdb8
主叫:024709361298
被叫:033587341292
:param text:
:return:
"""
# database_index = text.find('数据库')
uac = ''
uas = ''
uac_index = text.find('主叫:')
if uac_index > 0:
uac = text[uac_index + 3:text.find('被叫:')].strip()
uas_index = text.find('被叫:')
uas = text[uas_index + 3:].strip()
if uas.find('原被叫') > 0:
uas = uas[:uas.find('原被叫')]
if uas.find('境内') > 0:
uas = uas[:uas.find('境内')]
if uas.find('超长') > 0:
uas = uas[:uas.find('超长')]
return uac + ";" + uas
def str_shell(shell_text):
"""
从word文本中提取出shell脚本
:param shell_text:
:return:
"""
# index_0 = shell_text.find('./')
# uas_shell = ''
# uac_shell = ''
# if index_0 > 0:
#
# text_1 = shell_text[index_0:] # 从./位置到最后
# uas_shell = text_1[:text_1.find(')')]
# temp_1 = text_1[text_1.find(')') + 1:] # 截取到跳过半括号
# if temp_1.find('./') > 0:
# uac_shell = temp_1[temp_1.find('./'):temp_1.find(')')]
# return uas_shell + ";" + uac_shell
uas = ''
uac_xml = ''
index_0 = shell_text.find('Uas:')
if not index_0 == -1:
index_1 = shell_text.find('\n')
if index_1 > 0:
uas = shell_text[index_0 + 4:index_1]
else:
index_2 = shell_text.find('(./')
if index_2 > 0:
uas = shell_text[index_0 + 4:index_2].strip()
uac_text = shell_text[shell_text.find('Uac:') + 4:]
index_3 = uac_text.find('\n')
if index_3 > 0:
uac_xml = uac_text[:index_3].strip()
else:
index_4 = uac_text.find('(./')
uac_xml = uac_text[:index_4].strip()
uac_txt = '%s.txt' % uac_text[uac_text.find('-inf') + 4:uac_text.find('.txt')].strip()
return '%s;%s|%s' % (uas, uac_xml, uac_txt)
def str_properties(str_xml):
re_xml = re.compile('<(?P<label>\S+)>(.*?)</(?P=label)>')
list = (re_xml.findall(str_xml))
res = ''
for tuples in list:
key = '='.join(tuples)
key += ';'
res += key
return res[:-1]
if __name__ == '__main__':
str = """
1、 在后端web管理中的策略“207 主叫号码位长超长超短”已下发到前端系统;
2、 在后端web全局策略参数页面设置:“最短国外主叫位长”设置为3,“最长国外主叫位长”设置为18,并下发前端成功;
3、 查看前端境外呼叫拦截策略信息表(Inter_BarrFuncList)中的:
1) CliLenIllegalFunc(主叫号码长度非法拦截功能)为1:开启;执行sql:update inter_barrfunclist set clilenillegalfunc=1;
2) CliLenLegalMin(主叫号码长度非法拦截功能开启时生效,合法长度的最小值)为4;执行sql:update inter_barrfunclist set clilenlegalmin=4;
3) CliLenLegalMax(主叫号码长度非法拦截功能开启时生效,合法长度的最大值)为:18;执行sql:update inter_barrfunclist set clilenlegalmax=18;
4、 前端系统Cin/xmlwoods/clibarring中配置:
<InternationalCall><!--境外设定-->
<GrayListFirst>0</GrayListFirst><!--灰名单优先功能。0:关闭;1:开启;默认值0-->
</InternationalCall>
5、 境外A主叫号码2位拨打被叫C;
6、 境外B主叫号码20位拨打被叫C;
"""
str3 = """
数据库:数据库:hjljdb8(192.168.2.22)
主叫:0018134579
被叫:051257683586 、15505135739
"""
docx_try(r'C:\Users\hanxu\Desktop\FZ.docx')
555