这个脚本作用:
1:从testlink上export testsuite,下载的是xml格式的case文件。
2:用Read_xml_to_csv这个函数 转成csv格式,这个格式excel可以打开编辑case。
3:编辑好的csv文件再用read_csv_to_xml这个函数转换回xml格式。
4:然后testlink上用import导入这个xml文件了。
注意:
我们在excel里面编辑的时候,不要添加或删除表格,只能改内容。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# -*- coding=gbk -*-import sys reload(sys) sys.setdefaultencoding('gbk') import csvfrom xml.etree.ElementTree import iterparseimport xml.etree.ElementTree as ETfrom HTMLParser import HTMLParserclass XML_CSV(): #去掉xml文件中的HTML标签 def strip_tags(self,htmlStr): htmlStr = htmlStr.strip() htmlStr = htmlStr.strip("\n") result = [] parser = HTMLParser() parser.handle_data = result.append parser.feed(htmlStr) parser.close() return ''.join(result) def read_xml_to_csv(self,csv_file,xmlfile): csvfile = open(csv_file, 'wb') spamwriter = csv.writer(csvfile, dialect='excel') spamwriter.writerow(['tag', 'name', 'node_order', 'details', 'internalid','externalid','summary','steps','expectedresults']) #逐行解析XML文件,将每行的内容存入列表,之后逐行写入CSV文件中 for (event,node) in iterparse(xmlfile,events=['start']): if node.tag == "testsuite": suite_list = ['','','','','','','','',''] print node.attrib['name'] suite_list[0] = node.attrib['name'] for child in node: if child.tag == "node_order": print child.text suite_list[2] = child.text if child.tag == "details": print child.text suite_list[3] = self.strip_tags(str(child.text)) spamwriter.writerow(suite_list) if node.tag == "testcase": case_list = ['testcase','','','','','','','',''] print node.attrib['internalid'] print node.attrib['name'] case_list[1] = node.attrib['name'] case_list[4] = node.attrib['internalid'] for child in node: if child.tag == "node_order": print child.text case_list[2] = child.text if child.tag == "externalid": print child.text case_list[5] = child.text if child.tag == "summary": print self.strip_tags(str(child.text)) case_list[6] = self.strip_tags(str(child.text)) if child.tag == "steps": print self.strip_tags(str(child.text)) case_list[7] = self.strip_tags(str(child.text)) if child.tag == "expectedresults": #print child.text print self.strip_tags(str(child.text)) case_list[8] = self.strip_tags(str(child.text)) spamwriter.writerow(case_list) csvfile.close() def read_csv_to_xml(self,csv_file,xmlfile): #逐行读取CSV文件的内容,将内容写进以internalid为键,name,sumary,steps,expectresult为值得字典 csv_file = file(csv_file,'rb') reader = csv.reader(csv_file) case_dic = {} for line in reader: if reader.line_num == 1: continue if line[0] == "testcase": name = str(line[1]) internalid = str(line[4]) summary = line[6] steps = line[7] expectedresults = line[8] case_dic[internalid] = (name,summary,steps,expectedresults) csv_file.close() print case_dic #用ElementTree方法打开xml文件,逐行解析XML文件,发现case为tag的行,就将name,sumary,steps,expectresult,这几项用字典的值替换。 tree = ET.ElementTree() tree.parse(xmlfile) root = tree.getroot() root_suite_name = root.attrib['name'] for node in tree.iter(): if node.tag == "testsuite": print node.attrib['name'] sub_suite_name = node.attrib['name'] if sub_suite_name == root_suite_name: sub_suite_name = "" for child in node: if child.tag == "node_order": #print child.text pass if child.tag == "details": pass if node.tag == "testcase": new_internalid = node.attrib['internalid'] #将根目录和子目录的名字都写进了case名字中。如果不需要可以用下面那行注释掉的替换这一行 node.attrib['name'] = root_suite_name+'_'+sub_suite_name+'_'+case_dic[new_internalid][0] #node.attrib['name'] = case_dic[new_internalid][0] print node.attrib['name'] #解析tag为testcase的节点的子节点,并修改节点的值 for child in node: if child.tag == "node_order": pass if child.tag == "externalid": pass if child.tag == "summary": child.text = case_dic[new_internalid][1] child.text = str(child.text.replace('\n',"<p>")) if child.tag == "steps": child.text = str(case_dic[new_internalid][2]) child.text = str(child.text.replace('\n',"<p>")) if child.tag == "expectedresults": child.text = case_dic[new_internalid][3] child.text = str(child.text.replace('\n',"<p>")) #将修改后的ElementTree对象写入xml文件中。 tree.write(xmlfile,encoding='utf8') if __name__ == "__main__": test = XML_CSV() #test.read_xml_to_csv('testsuites2.csv','testsuites.xml') test.read_csv_to_xml('testsuites2.csv','testsuites.xml') |
原创作品,转载请注明来自drupal0.com
本文介绍了一款用于TestLink平台的用例转换脚本工具,该工具支持将测试套件从XML格式导出并转换为CSV格式进行编辑,再转换回XML格式以便重新导入到TestLink中。
957

被折叠的 条评论
为什么被折叠?



