-
-
Save zh-f/e718251d8f8073d9e988c06f372277d4 to your computer and use it in GitHub Desktop.
Small Python 2 script to convert exported TestLink test cases (test suite) to CSV.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# -*- coding=gbk -*- | |
# | |
# The original version can be found at https://www.programmersought.com/article/60554969634/. | |
# The differences from original version are as follows: | |
# * added shbang line so that we can know this is the python 2 script. | |
# * added SUITE_NAMES column. | |
# | |
import glob | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('UTF-8') | |
import csv | |
import re | |
import os | |
from xml.etree.ElementTree import iterparse | |
from HTMLParser import HTMLParser | |
importance_map = ['low', 'medium', 'high'] | |
status_map = ['draft', 'to review', 'reviewing', 'rewritting', 'invalid', 'later versions', 'final'] | |
class TestSuite: | |
def __init__(self, node): | |
self.id = node.attrib['id'] | |
self.name = node.attrib['name'].encode('utf-8') | |
self.parent = None | |
self.children = [] | |
self.cases = [] | |
def append(self, suite): | |
suite.parent = self | |
self.children.append(suite) | |
return suite | |
def canonicalname(self): | |
"""Returns the canonicalized name of this instance. | |
The canonical name is here a string consisting of the canonical name | |
of the parent suite and the name of this suite concatenated with " > ". | |
Note that in this version, the names of the top-level suites are | |
excluded from the canonical names of the other suites. | |
""" | |
if self.parent is not None and self.nestlevel() > 1: | |
name = self.parent.canonicalname() + " > " | |
else: | |
name = "" | |
return name + self.name | |
def nestlevel(self): | |
return 0 if self.parent is None else 1 + self.parent.nestlevel() | |
class XML_CSV(): | |
def strip_tags(self, htmlStr): | |
htmlStr = htmlStr.strip() | |
htmlStr = htmlStr.strip("\n") | |
# Remove style tag | |
re_style = re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>', re.I) | |
htmlStr = re_style.sub('', htmlStr) | |
result = [] | |
parser = HTMLParser() | |
parser.handle_data = result.append | |
htmlStr = parser.unescape(htmlStr) | |
parser.feed(htmlStr) | |
parser.close() | |
return ''.join(result) | |
def convert_xml2csv(self, csv_file, xmlfile): | |
suites = [] | |
suite = None | |
csvfile = open(csv_file, 'wb') | |
spamwriter = csv.writer(csvfile, dialect='excel', delimiter=';') | |
spamwriter.writerow(['TCID', 'SUITE_NAMES', 'CASE_NAME', 'IMPORTANCE', 'STATUS', 'SUMMARY', 'STEP', 'Result']) | |
for (event, node) in iterparse(xmlfile, events=['start', 'end']): | |
if node.tag == "testsuite": | |
if event == 'start': | |
if suite is None: | |
suite = TestSuite(node) | |
suites.append(suite) | |
else: | |
suite = suite.append(TestSuite(node)) | |
else: | |
suite = suite.parent | |
elif node.tag == "testcase": | |
if event == 'start': | |
continue | |
case_list = ['', '', '', '', '', '', '', ''] | |
steps_list = ['', '', '', '', '', '', '', ''] | |
if suite: | |
case_list[1] = suite.canonicalname() | |
case_list[2] = node.attrib['name'] | |
for child in node: | |
if child.tag == "externalid": | |
text = re.sub('\n|<p>|</p>|\t', '', str(child.text)) | |
# print self.strip_tags(text) | |
TCID = self.strip_tags(text) | |
elif child.tag == "summary": | |
text = re.sub('\n|<p>|</p>|\t', '', str(child.text)) | |
# print self.strip_tags(text) | |
case_list[5] = self.strip_tags(text) | |
if "steps" not in [item.tag for item in node]: | |
case_list[0] = TCID | |
spamwriter.writerow(case_list) | |
break | |
elif child.tag == "importance": | |
# text = re.sub('\n|<p>|</p>|\t', '', str(child.text)) | |
# print self.strip_tags(text) | |
case_list[3] = importance_map[int(self.strip_tags(child.text)) - 1] | |
elif child.tag == "status": | |
# text = re.sub('\n|<p>|</p>|\t', '', str(child.text)) | |
# print self.strip_tags(text) | |
case_list[4] = status_map[int(self.strip_tags(child.text)) - 1] | |
elif child.tag == "steps": | |
if len(child) > 0: | |
for i in range(len(child)): | |
if i == 0: | |
for n in range(len(child.getchildren()[i])): | |
if child.getchildren()[i].getchildren()[n].text is not None: | |
text = self.strip_tags(child.getchildren()[i].getchildren()[n].text).encode('UTF-8') | |
else: | |
text = '' | |
# print text | |
if child.getchildren()[i].getchildren()[n].tag == 'actions': | |
case_list[6] = text | |
elif child.getchildren()[i].getchildren()[n].tag == 'expectedresults': | |
case_list[7] = text | |
case_list[0] = TCID | |
spamwriter.writerow(case_list) | |
else: | |
for n in range(len(child.getchildren()[i])): | |
if child.getchildren()[i].getchildren()[n].text is not None: | |
text = self.strip_tags(child.getchildren()[i].getchildren()[n].text).encode('UTF-8') | |
else: | |
text = '' | |
# print text | |
if child.getchildren()[i].getchildren()[n].tag == 'actions': | |
steps_list[6] = text | |
elif child.getchildren()[i].getchildren()[n].tag == 'expectedresults': | |
steps_list[7] = text | |
steps_list[0] = TCID | |
spamwriter.writerow(steps_list) | |
csvfile.close() | |
if __name__ == "__main__": | |
test = XML_CSV() | |
# test.convert_xml2csv('output.csv', 'input.xml') | |
for xml_file in glob.glob(os.curdir + '/*.xml'): | |
test.convert_xml2csv(os.path.splitext(xml_file)[0] + '.csv', xml_file) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment