Python实现excel测试用例转xml导入到TestLink
使用python版本为:3.5
环境要求:
1,安装jdk1.8
2,安装python
3,安装python模块xlrd、xml
运行(注意:excel文件、xml文件和代码执行文件在同一目录):
1、命令行进入到py文件的目录
(windows系统,cmd进入dos命令窗口,执行:“cd C:\Users\...”,进入到文件所在路径目录,这里举例文件在C:\Users目录下面)
(OS系统,进入终端,输入命令"cd /Users/xiaojingjing/Documents/",进入到文件所在路径目录,这里举例文件在/Users/xiaojingjing/Documents/目录下面,注意⚠️,cd后面有一个空格)
2、执行py脚本文件,举例:脚本为“main.py”
(windows系统,输入命令:“python main.py”)
(OS系统,输入命令:“python main.py”)
excel格式要求:
1,excel文件名称为用例集名称
2,excel文件中的标签对应功能模块名称
3,目前一个testcase只能对应一个步骤和一个预期结果
参考附件截图
代码中是自定义测试用例集名称为“运维端app1.2版本”,可以用
testsuitename1=excelfilename[0:-4] 来截取文件前半部分名称来作为测试用例集名称
代码如下:
# -*- coding:utf-8 -*- __author__ = '三天', __time__ = '2018/3/18 下午9:58', version = '', from xml.dom.minidom import Document import xlrd # 指定生成xml文件名称以及路径,这里是在根目录下 # xmlfilename='testcase.xml' # 指定源excel文件名称以及路径,这里是在根目录下 excelfilename='your_testCases_suite.xlsx' # 创建dom文档 doc = Document() testsuiteid='1233' #testuitename为用例集名称,按照用例规范确定用例集名称,上线日期+项目版本,例如:20180329好医生APP1.2 testsuitename=excelfilename[0:-5] print("用例集名称:",testsuitename) #设置生成xml文件名称与Excel文件名称对应 xmlfilename=testsuitename+'.xml' testsuite = doc.createElement('testsuite') #设置根节点属性 testsuite.setAttribute('id',testsuiteid) testsuite.setAttribute('name',testsuitename) # 根节点插入dom树 doc.appendChild(testsuite) # ==================================================== #创建节点node_order node_order=doc.createElement('node_order') #创建node_order的文本节点 node_order_text=doc.createTextNode('<![CDATA[5]]>') #将文本节点插入到<node_order>下 node_order.appendChild(node_order_text) #将<node_order>插入到父节点<testsuite>下 testsuite.appendChild(node_order) # ===================================================== #创建节点details details=doc.createElement('details') #创建node_order的文本节点 details_text=doc.createTextNode("<![CDATA[]]>") #将文本节点插入到<node_order>下 details.appendChild(details_text) #将<node_order>插入到父节点<testsuite>下 testsuite.appendChild(details) datacases=xlrd.open_workbook(excelfilename) print("标签数量:",datacases.sheet_names()) sheets=datacases.sheet_names() case_num = 0 for sheet in sheets: sheet1=datacases.sheet_by_name(sheet) # ====================测试用例功能模块一1⃣️=============================== #创建节点testsuite testsuite1=doc.createElement('testsuite') #创建节点testsuite属性 id='999' testsuite1.setAttribute('id',id) testsuite1.setAttribute('name',sheet) testsuite.appendChild(testsuite1) # ==================================================== #创建节点node_order node_order=doc.createElement('node_order') #创建node_order的文本节点 node_order_text=doc.createTextNode('<![CDATA[5]]>') #将文本节点插入到<node_order>下 node_order.appendChild(node_order_text) #将<node_order>插入到父节点<testsuite>下 testsuite1.appendChild(node_order) # ===================================================== #创建节点details details=doc.createElement('details') #创建node_order的文本节点 details_text=doc.createTextNode("<![CDATA[]]>") #将文本节点插入到<node_order>下 details.appendChild(details_text) #将<node_order>插入到父节点<testsuite>下 testsuite1.appendChild(details) #定义行数为文档中有数据的行数 row_num=sheet1.nrows # print(sheet,"用例个数为:",case_num) row_nums = 0 for i in range(1,row_num): # casenum=casenum+1 # Actions_Number=1 #定义默认步骤编号第一步 Actions_Number=sheet1.cell_value(i,0) TestCase = sheet1.cell_value(i, 1) Summary = sheet1.cell_value(i, 2) Preconditions = sheet1.cell_value(i, 3) Actions = sheet1.cell_value(i, 4) Expectedresults = sheet1.cell_value(i, 5) if len(TestCase) <= 1: #增加步骤编号 # Actions_Number=Actions_Number+1 # # ============================================================= # 创建节点step step = doc.createElement('step') steps.appendChild(step) # ============================================================= # ============================================================= # 创建节点step_number step_number = doc.createElement('step_number') Actions_Number=str(Actions_Number) step_number_text = doc.createTextNode(Actions_Number) step_number.appendChild(step_number_text) step.appendChild(step_number) # ============================================================= # 创建节点actions actions = doc.createElement('actions') actions_text = doc.createTextNode(Actions) actions.appendChild(actions_text) step.appendChild(actions) # ============================================================= # 创建节点expectedresults expectedresults = doc.createElement('expectedresults') expectedresults_text = doc.createTextNode(Expectedresults) expectedresults.appendChild(expectedresults_text) step.appendChild(expectedresults) # ============================================================= # 创建节点execution_type execution_type = doc.createElement('execution_type') execution_type_text = doc.createTextNode("<![CDATA[1]]>") execution_type.appendChild(execution_type_text) step.appendChild(execution_type) else: case_num = case_num + 1 row_nums =row_nums+1 # 创建节点testcase testcase = doc.createElement('testcase') # 创建testcase节点属性 testcase.setAttribute('internalid', 'id') testcase.setAttribute('name', TestCase) testsuite1.appendChild(testcase) # ============================================================= # 创建节点node_order node_order = doc.createElement('node_order') # 创建node_order的文本节点 node_order_text = doc.createTextNode('<![CDATA[1000]]>') # 将文本节点插入到<node_order>下 node_order.appendChild(node_order_text) # 将<node_order>插入到父节点<testsuite>下 testcase.appendChild(node_order) # ============================================================= # 创建节点externalid externalid = doc.createElement('externalid') # 创建externalid的文本节点 externalid_text = doc.createTextNode('<![CDATA[216]]>') # 将文本节点插入到<externalid>下 externalid.appendChild(externalid_text) # 将<nexternalid>插入到父节点<testsuite>下 testcase.appendChild(externalid) # ============================================================= # 创建节点version version = doc.createElement('version') # 创建node_order的文本节点 version_text = doc.createTextNode('<![CDATA[1]]>') # 将文本节点插入到<node_order>下 version.appendChild(version_text) # 将<node_order>插入到父节点<testsuite>下 testcase.appendChild(version) # ============================================================= # 创建节点summary summary = doc.createElement('summary') # 创建node_order的文本节点 summary_conent = '<![CDATA[1]]>' + Summary summary_text = doc.createTextNode(summary_conent) # 将文本节点插入到<node_order>下 summary.appendChild(summary_text) # 将<node_order>插入到父节点<testsuite>下 testcase.appendChild(summary) # ============================================================= # preconditions # 创建节点preconditions preconditions = doc.createElement('preconditions') # 创建preconditions的文本节点 preconditions_conent = '<![CDATA[1]]>' + Preconditions preconditions_text = doc.createTextNode(preconditions_conent) # 将文本节点插入到<node_order>下 preconditions.appendChild(preconditions_text) # 将<node_order>插入到父节点<testsuite>下 testcase.appendChild(preconditions) # ============================================================= # ============================================================= # 创建节点execution_type execution_type = doc.createElement('execution_type') # 创建preconditions的文本节点 execution_type_text = doc.createTextNode('<![CDATA[1]]>') # 将文本节点插入到<node_order>下 execution_type.appendChild(execution_type_text) # 将<node_order>插入到父节点<testsuite>下 testcase.appendChild(execution_type) # ============================================================= # 创建节点importance importance = doc.createElement('importance') # 创建preconditions的文本节点 importance_text = doc.createTextNode('<![CDATA[2]]>') # 将文本节点插入到<node_order>下 importance.appendChild(importance_text) # 将<node_order>插入到父节点<testsuite>下 testcase.appendChild(importance) # ============================================================= # 创建节点estimated_exec_duration estimated_exec_duration = doc.createElement('estimated_exec_duration') testcase.appendChild(estimated_exec_duration) # ============================================================= # 创建节点status status = doc.createElement('status') status_text = doc.createTextNode('1') status.appendChild(status_text) testcase.appendChild(status) # ============================================================= # 创建节点is_open is_open = doc.createElement('is_open') is_open_text = doc.createTextNode('1') is_open.appendChild(is_open_text) testcase.appendChild(is_open) # ============================================================= # 创建节点active active = doc.createElement('active') active_text = doc.createTextNode('1') active.appendChild(active_text) testcase.appendChild(active) # ============================================================= # 创建节点steps steps = doc.createElement('steps') testcase.appendChild(steps) # ============================================================= # 创建节点step step = doc.createElement('step') steps.appendChild(step) # ============================================================= # 创建节点step_number step_number = doc.createElement('step_number') step_number_text_count =str(Actions_Number) step_number_text = doc.createTextNode(step_number_text_count) step_number.appendChild(step_number_text) step.appendChild(step_number) # ============================================================= # 创建节点actions actions = doc.createElement('actions') actions_text = doc.createTextNode(Actions) actions.appendChild(actions_text) step.appendChild(actions) # ============================================================= # 创建节点expectedresults expectedresults = doc.createElement('expectedresults') expectedresults_text = doc.createTextNode(Expectedresults) expectedresults.appendChild(expectedresults_text) step.appendChild(expectedresults) # ============================================================= # 创建节点execution_type execution_type = doc.createElement('execution_type') execution_type_text = doc.createTextNode("<![CDATA[1]]>") execution_type.appendChild(execution_type_text) step.appendChild(execution_type) print(sheet, "用例个数为:", row_nums) # 将dom对象写入本地xml文件 with open(xmlfilename, 'w') as f: f.write(doc.toprettyxml(indent='\t')) f.close() print("共计用例条数为:",case_num,"条!")
小技巧:
如果要实现换行,需要在excel单元格换行部分增加<p></p>
例如:
<p>1.当前设备已绑定</p>
<p>2.BD工号:123456</p>
<p>3.设备类型:小桩机</p>
<p>4.mac地址:456789</p>
<p>5.门店:哈哈</p>
效果如下:
上一篇: 夏季多见电冰箱肠炎 应充分热透才食用
下一篇: 中西医都赞同 夏季养生首先应养心