User Tools

Site Tools


tips:valgrind-to-xunit-xml-converter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tips:valgrind-to-xunit-xml-converter [31.08.2011 11:10] (current)
vergo created
Line 1: Line 1:
 +====== Valgrind to xUnit XML converter ======
  
 +This Python script converts the XML file output of Valgrind to xUnit XML format that can then be used for example in [[http://www.jenkins-ci.org/|Jenkins]]. Based on shorter Python script by Toni Cebrián found from [[http://www.tonicebrian.com/2010/10/15/continuous-integration-for-c-using-hudson/|here]].
 +
 +<code python valgrind2xunit.py>
 +#!/usr/bin/env python
 +
 +import xml.etree.ElementTree as ET
 +doc = ET.parse('valgrind.xml')
 +errors = doc.findall('//error')
 +
 +out = open("cpputest_valgrind.xml","w")
 +out.write('<?xml version="1.0" encoding="UTF-8"?>\n')
 +out.write('<testsuite name="valgrind" tests="'+str(len(errors))+'" errors="0" failures="'+str(len(errors))+'" skip="0">\n')
 +errorcount=0
 +for error in errors:
 +    errorcount=errorcount+1
 +
 +    kind = error.find('kind')
 +    what = error.find('what')
 +    if  what == None:
 +        what = error.find('xwhat/text')
 +
 +    stack = error.find('stack')
 +    frames = stack.findall('frame')
 +
 +    for frame in frames:
 +        fi = frame.find('file')
 +        li = frame.find('line')
 +        if fi != None and li != None:
 +            break
 +
 +    if fi != None and li != None:
 +        out.write('    <testcase classname="ValgrindMemoryCheck" name="Memory check '+str(errorcount)+' ('+kind.text+', '+fi.text+':'+li.text+')" time="0">\n')
 +    else:
 +        out.write('    <testcase classname="ValgrindMemoryCheck" name="Memory check '+str(errorcount)+' ('+kind.text+')" time="0">\n')
 +    out.write('        <error type="'+kind.text+'">\n')
 +    out.write('  '+what.text+'\n\n')
 +
 +    for frame in frames:
 +        ip = frame.find('ip')
 +        fn = frame.find('fn')
 +        fi = frame.find('file')
 +        li = frame.find('line')
 +        bodytext = fn.text
 +        bodytext = bodytext.replace("&","&amp;")
 +        bodytext = bodytext.replace("<","&lt;")
 +        bodytext = bodytext.replace(">","&gt;")
 +        if fi != None and li != None:
 +            out.write('  '+ip.text+': '+bodytext+' ('+fi.text+':'+li.text+')\n')
 +        else:
 +            out.write('  '+ip.text+': '+bodytext+'\n')
 +
 +    out.write('        </error>\n')
 +    out.write('    </testcase>\n')
 +out.write('</testsuite>\n')
 +out.close()
 +</code>
tips/valgrind-to-xunit-xml-converter.txt · Last modified: 31.08.2011 11:10 by vergo