|
| 1 | +import string |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from xml.sax.saxutils import escape |
| 5 | + |
| 6 | +def createXML(testData, outputfilepath): |
| 7 | + template_testsuites = string.Template("""<?xml version="1.0" encoding="UTF-8"?> |
| 8 | +<test-results> |
| 9 | + <test-suite> |
| 10 | + <results> |
| 11 | +${successtestcases} |
| 12 | +${failingtestcases} |
| 13 | + </results> |
| 14 | + </test-suite> |
| 15 | +</test-results>""") |
| 16 | + |
| 17 | + template_successtestcase = string.Template(""" <test-case classname="${classname}" name="${name}" result="${result}" time=""/>""") |
| 18 | + template_failingtestcase = string.Template(""" <test-case classname="${classname}" name="${name}" result="${result}" time=""> |
| 19 | + <failure> |
| 20 | + <message>${message}</message> |
| 21 | + </failure> |
| 22 | + </test-case>""") |
| 23 | + |
| 24 | + contents_successtestcase = [template_successtestcase.substitute(id=id, classname=classname, name=name, result=result) for (id, classname, name, result, message) in testData if result != "Failed"] |
| 25 | + contents_failingtestcase = [template_failingtestcase.substitute(id=id, classname=classname, name=name, result=result, message=message) for (id, classname, name, result, message) in testData if result == "Failed"] |
| 26 | + result = template_testsuites.substitute(successtestcases='\n'.join(contents_successtestcase), failingtestcases='\n'.join(contents_failingtestcase)) |
| 27 | + #print (result) |
| 28 | + with open(outputfilepath, 'w') as outputfile: |
| 29 | + outputfile.write(result) |
| 30 | + |
| 31 | + print("Finished converting data, result written to " + outputfilepath) |
| 32 | + |
| 33 | +def readFileContent(inputfilepath, outputfile): |
| 34 | + TestBlock = False |
| 35 | + print (" Reading testresults from file " + inputfilepath) |
| 36 | + with open(inputfilepath, 'r') as inputfile: |
| 37 | + testData = [] |
| 38 | + |
| 39 | + for line in inputfile: |
| 40 | + line = line.replace("FAIL:", "FAIL->").strip().replace("[FAILED]", "Failed").replace("[PASSED]", "Success") |
| 41 | + #print (line) |
| 42 | + if line.strip() == "" and TestBlock == True: |
| 43 | + TestBlock = False |
| 44 | + print (" End of testing block") |
| 45 | + if TestBlock == True: |
| 46 | + parts = re.split(":|\t", line) |
| 47 | + print (" Found Test Case: " + str(parts)) |
| 48 | + if (len(parts) > 4): |
| 49 | + testData.append((parts[1], parts[0], parts[2], parts[4], parts[3])) |
| 50 | + else: |
| 51 | + testData.append((parts[1], parts[0], parts[2], parts[3], '')) |
| 52 | + if line.__contains__("Testing...") == True: |
| 53 | + TestBlock = True |
| 54 | + print (" Found testing block - start reading test cases") |
| 55 | + print (" Found " + str(len(testData)) + " Test Cases, exporting them now to output file") |
| 56 | + createXML(testData, outputfile) |
| 57 | + |
| 58 | +print ("#################################################################################################") |
| 59 | +print ("# Test Results Parser 1.0 by @tschissler") |
| 60 | +print ("#################################################################################################") |
| 61 | +print () |
| 62 | +print ("This tool is parsing test results from PlatformIO unit tests and converting into NUnit format") |
| 63 | +print () |
| 64 | +print ("Usage: TestResultsParser.py <inputfile> <outputfile>") |
| 65 | +print () |
| 66 | + |
| 67 | +print (sys.argv) |
| 68 | +if (len(sys.argv) != 3): |
| 69 | + print ("Wrong number of parameters [" + str(len(sys.argv)) + "], expecting 2 parameters") |
| 70 | + sys.exit([-1]) |
| 71 | + |
| 72 | +print (" -- Starting conversion process --") |
| 73 | +readFileContent(sys.argv[1], sys.argv[2]) |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +#<?xml version="1.0" encoding="UTF-8"?> |
| 81 | +#<testsuites disabled="" errors="" failures="" name="" tests="" time=""> |
| 82 | +# <testsuite disabled="" errors="" failures="" hostname="" id="" |
| 83 | +# name="" package="" skipped="" tests="" time="" timestamp=""> |
| 84 | +# <properties> |
| 85 | +# <property name="" value=""/> |
| 86 | +# </properties> |
| 87 | +# <testcase assertions="" classname="" name="" status="" time=""> |
| 88 | +# <skipped/> |
| 89 | +# <error message="" type=""/> |
| 90 | +# <failure message="" type=""/> |
| 91 | +# <system-out/> |
| 92 | +# <system-err/> |
| 93 | +# </testcase> |
| 94 | +# <system-out/> |
| 95 | +# <system-err/> |
| 96 | +# </testsuite> |
| 97 | +#</testsuites> |
0 commit comments