Use python version: 3.5
Environmental requirements :
1. Install jdk1.8
2. Install python
3. Install python modules xlrd, xml
Run ( note: excel file, xml file and code execution file are in the same directory ):
1. Enter the directory of the py file from the command line
(windows system, cmd enters the dos command window, execute: "cd C:\Users\...", enter the path directory where the file is located, the example file here is under the C:\Users directory)
(OS system, enter the terminal, enter the command "cd /Users/xiaojingjing/Documents/", enter the path directory where the file is located, the example file here is under the /Users/xiaojingjing/Documents/ directory, pay attention to ⚠️, there is a space after the cd)
2. Execute the py script file, for example: the script is "main.py"
(windows system, enter the command: "python main.py")
(OS system, enter the command: "python main.py")
Excel format requirements:
1. The name of the excel file is the name of the use case set
2. The label in the excel file corresponds to the function module name
3. Currently, a testcase can only correspond to one step and one expected result
Refer to the attached screenshot
In the code, the name of the custom test case set is "operation and maintenance side app1.2 version", which can be used
testsuitename1=excelfilename[ 0 :- 4 ] to intercept the first half of the file name as the test case set name
code show as below:
# -*- coding:utf-8 -*- __author__ = 'three days', __time__ = '2018/3/18 9:58 PM', version = '', from xml.dom.minidom import Document import xlrd # Specify the name and path of the generated xml file, here is in the root directory # xmlfilename='testcase.xml' # Specify the source excel file name and path, here is in the root directory excelfilename='your_testCases_suite.xlsx' # create dom document doc = Document() testsuiteid='1233' #testuitename is the name of the use case set, the name of the use case set is determined according to the use case specification, the launch date + project version, for example: 20180329 Good Doctor APP1.2 testsuitename=excelfilename[0:-5] print("Name of test suite: ",testsuitename) #Set the generated xml file name to correspond to the Excel file name xmlfilename=testsuitename+'.xml' testsuite = doc.createElement('testsuite') #Set root node properties testsuite.setAttribute('id',testsuiteid) testsuite.setAttribute('name',testsuitename) # Insert the root node into the dom tree doc.appendChild(testsuite) # ==================================================== #Create node node_order node_order=doc.createElement('node_order') #Create a text node for node_order node_order_text=doc.createTextNode('<![CDATA[5]]>') #Insert text nodes under <node_order> node_order.appendChild(node_order_text) #Insert <node_order> under the parent node <testsuite> testsuite.appendChild(node_order) # ===================================================== #Create node details details=doc.createElement('details') #Create a text node for node_order details_text=doc.createTextNode("<![CDATA[]]>") #Insert text nodes under <node_order> details.appendChild(details_text) #Insert <node_order> under the parent node <testsuite> testsuite.appendChild(details) datacases=xlrd.open_workbook(excelfilename) print("Number of labels: ",datacases.sheet_names()) sheets=datacases.sheet_names() case_num = 0 for sheet in sheets: sheet1=datacases.sheet_by_name(sheet) # =====================Test case function module one 1⃣️======================== ======== #Create node testsuite testsuite1=doc.createElement('testsuite') #Create node testsuite properties id='999' testsuite1.setAttribute('id',id) testsuite1.setAttribute('name',sheet) testsuite.appendChild(testsuite1) # ==================================================== #Create node node_order node_order=doc.createElement('node_order') #Create a text node for node_order node_order_text=doc.createTextNode('<![CDATA[5]]>') #Insert text nodes under <node_order> node_order.appendChild(node_order_text) #Insert <node_order> under the parent node <testsuite> testsuite1.appendChild(node_order) # ===================================================== #Create node details details=doc.createElement('details') #Create a text node for node_order details_text=doc.createTextNode("<![CDATA[]]>") #Insert text nodes under <node_order> details.appendChild(details_text) #Insert <node_order> under the parent node <testsuite> testsuite1.appendChild(details) #Define the number of lines as the number of lines with data in the document row_num=sheet1.nrows # print(sheet,"The number of cases is:",case_num) row_nums = 0 for i in range(1,row_num): # casenum=casenum+1 # Actions_Number=1 #define the default step number first step 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: #Increase step number # Actions_Number=Actions_Number+1 # # ============================================================= # create node step step = doc.createElement('step') steps.appendChild(step) # ============================================================= # ============================================================= # Create node 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) # ============================================================= # create node actions actions = doc.createElement('actions') actions_text = doc.createTextNode(Actions) actions.appendChild(actions_text) step.appendChild(actions) # ============================================================= # Create node expectedresults expectedresults = doc.createElement('expectedresults') expectedresults_text = doc.createTextNode(Expectedresults) expectedresults.appendChild(expectedresults_text) step.appendChild(expectedresults) # ============================================================= # Create node 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 # Create node testcase testcase = doc.createElement('testcase') # Create testcase node properties testcase.setAttribute('internalid', 'id') testcase.setAttribute('name', TestCase) testsuite1.appendChild(testcase) # ============================================================= # Create node node_order node_order = doc.createElement('node_order') # Create a text node for node_order node_order_text = doc.createTextNode('<![CDATA[1000]]>') # Insert text nodes under <node_order> node_order.appendChild(node_order_text) # Insert <node_order> under the parent node <testsuite> testcase.appendChild(node_order) # ============================================================= # Create node externalid externalid = doc.createElement('externalid') # Create a text node for externalid externalid_text = doc.createTextNode('<![CDATA[216]]>') # Insert text node under <externalid> externalid.appendChild(externalid_text) # Insert <nexternalid> under the parent node <testsuite> testcase.appendChild(externalid) # ============================================================= # create node version version = doc.createElement('version') # Create a text node for node_order version_text = doc.createTextNode('<![CDATA[1]]>') # Insert text nodes under <node_order> version.appendChild(version_text) # Insert <node_order> under the parent node <testsuite> testcase.appendChild(version) # ============================================================= # Create node summary summary = doc.createElement('summary') # Create a text node for node_order summary_conent = '<![CDATA[1]]>' + Summary summary_text = doc.createTextNode(summary_conent) # Insert text nodes under <node_order> summary.appendChild(summary_text) # Insert <node_order> under the parent node <testsuite> testcase.appendChild(summary) # ============================================================= # preconditions # Create node preconditions preconditions = doc.createElement('preconditions') # Create a text node for preconditions preconditions_conent = '<![CDATA[1]]>' + Preconditions preconditions_text = doc.createTextNode(preconditions_conent) # Insert text nodes under <node_order> preconditions.appendChild(preconditions_text) # Insert <node_order> under the parent node <testsuite> testcase.appendChild(preconditions) # ============================================================= # ============================================================= # Create node execution_type execution_type = doc.createElement('execution_type') # Create a text node for preconditions execution_type_text = doc.createTextNode('<![CDATA[1]]>') # Insert text nodes under <node_order> execution_type.appendChild(execution_type_text) # Insert <node_order> under the parent node <testsuite> testcase.appendChild(execution_type) # ============================================================= # Create node importance importance = doc.createElement('importance') # Create a text node for preconditions importance_text = doc.createTextNode('<![CDATA[2]]>') # Insert text nodes under <node_order> importance.appendChild(importance_text) # Insert <node_order> under the parent node <testsuite> testcase.appendChild(importance) # ============================================================= # Create node estimated_exec_duration estimated_exec_duration = doc.createElement('estimated_exec_duration') testcase.appendChild(estimated_exec_duration) # ============================================================= # create node status status = doc.createElement('status') status_text = doc.createTextNode('1') status.appendChild(status_text) testcase.appendChild(status) # ============================================================= # Create node is_open is_open = doc.createElement('is_open') is_open_text = doc.createTextNode('1') is_open.appendChild(is_open_text) testcase.appendChild(is_open) # ============================================================= # create node active active = doc.createElement('active') active_text = doc.createTextNode('1') active.appendChild(active_text) testcase.appendChild(active) # ============================================================= # create node steps steps = doc.createElement('steps') testcase.appendChild(steps) # ============================================================= # create node step step = doc.createElement('step') steps.appendChild(step) # ============================================================= # Create node 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) # ============================================================= # create node actions actions = doc.createElement('actions') actions_text = doc.createTextNode(Actions) actions.appendChild(actions_text) step.appendChild(actions) # ============================================================= # Create node expectedresults expectedresults = doc.createElement('expectedresults') expectedresults_text = doc.createTextNode(Expectedresults) expectedresults.appendChild(expectedresults_text) step.appendChild(expectedresults) # ============================================================= # Create node 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, "The number of use cases is:", row_nums) # Write the dom object to the local xml file with open(xmlfilename, 'w') as f: f.write(doc.toprettyxml(indent='\t')) f.close() print("The total number of use cases is: ",case_num,"Items!")
Tips:
If you want to implement line wrapping, you need to add <p></p> to the line wrapping part of the excel cell
E.g:
<p>1. The current device is bound</p>
<p>2.BD ID: 123456</p>
<p>3. Equipment type: small pile driver</p>
<p>4.mac address: 456789</p>
<p>5. Store: Haha</p>
The effect is as follows: