CODESYS - the IEC 61131-3 automation software

Welcome to the official CODESYS Forum by 3S-Smart Software Solutions GmbH | A member of the CODESYS Group
Deutsche Version English version russian version 
It is currently Wed Dec 11, 2019 2:41 pm

All times are UTC+01:00




Post new topic  Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Aug 13, 2019 5:59 pm 
Offline

Joined: Thu Jul 27, 2017 7:54 pm
Posts: 13
Hello,

In this thread user Goof van de Weg mentioned that they had created a python script to convert exported Test Manager reports into a Junit format for Jenkins to recognize. As I don't believe he is active anymore I have created my own and wish to share it to those who would like to do the same.

Modify as you see fit - mine assumes that the test FBs are named FB_{FBNAME)_UNITTESTS and names the cases by each step of a multitest.

Install untangle, dateutil and junit_xml from pip or where ever you get your modules from

Code:
import untangle
import sys
from dateutil import parser
from junit_xml import TestSuite, TestCase

# Get time taken to execute test in seconds for JUnit report
def getTimeTaken(startTime, endTime):
    st = parser.parse(startTime)
    et = parser.parse(endTime)
    timedelta = et-st
    return timedelta.seconds


if len(sys.argv) == 1:
    o = untangle.parse('scripttest.xml')
    print("No argument received, using scripttest.xml")
else:
    print("Argument received, using TestManagerReport.xml")
    o = untangle.parse(
        'TestManagerReport.xml')

JunitTestCases = []
exitCode = 0
for CodesysTestCases in o.TestReport.Details.Sequence.TestCase:
    codesysTCName = str(CodesysTestCases['Name'])
    newTC = TestCase("","")
    newTC.status = CodesysTestCases.Result['State']
    newTC.elapsed_sec = getTimeTaken(CodesysTestCases.Timing.StartTime.cdata, CodesysTestCases.Timing.EndTime.cdata)
    chunksByUnderscore = codesysTCName
    chunksByUnderscore = chunksByUnderscore.split("_")

    chunksByColon = codesysTCName
    chunksByColon = chunksByColon.split(":")
    if chunksByUnderscore[0] == "FB":
        newTC.classname = chunksByUnderscore[0] + "_" + chunksByUnderscore[1]
        newTC.name = chunksByColon[1].strip() + ":" + chunksByColon[2]
    else:
        newTC.name = chunksByUnderscore[0]
        newTC.classname = "Test Script"
    if CodesysTestCases.Result['State'] == 'Failed':
        newTC.add_failure_info(CodesysTestCases.Result.AdditionalInformation.cdata)
        exitCode = 1
    JunitTestCases.append(newTC)

ts = TestSuite("Test Suite", JunitTestCases)

with open('junitreports.xml', 'w') as f:
    TestSuite.to_file(f, [ts], prettyprint=True)
if exitCode != 0:
    print("Failing Jenkins build due to test failure")
sys.exit(exitCode)


Top
   
PostPosted: Tue Aug 20, 2019 8:17 pm 
Offline
User avatar

Joined: Fri Jan 19, 2018 3:38 pm
Posts: 77
Thank you!

_________________
CfUnit, free and open-source IEC61131-3 unit-testing framework
My profile


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 2 posts ] 

All times are UTC+01:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited