Import JUnit reports in SonarPython

Our testing framework for our python code generates JUnit reports but unfortunately the Python plugin for SonarCloud only supports XUnit reports.

Since SonarCloud supports both formats it would be good to add support for JUnit as well for Python code.

In the meantime we are trying to find a tool to convert from JUnit to XUnit but so far our search is not fruitful.

Which testing framework do you use?
Did you try feeding the “Junit” reports to sonar.python.xunit.reportPath?

We are using green: https://github.com/CleanCut/green

Feeding the JUnit reports to sonar.python.xunit.reportPath gives us a warning and it also shows up as a warning in the SonarCloud GUI. The same xml files are fed to Jenkins and they get parsed properly there.

INFO: Sensor PythonXUnitSensor [python]
WARN: Cannot read report 'build/test_reports/py/*-pytests.xml', the following exception occurred: null
INFO: Sensor PythonXUnitSensor [python] (done) | time=67ms

For reference here is a sample xml file that is generated:

<?xml version='1.0' encoding='utf-8'?>
<testsuites>
  <testsuite name="test_run_py_tests.TestClass" tests="3" failures="0" errors="0" skipped="0">
    <testcase name="test_foo" classname="TestClass"/>
    <testcase name="test_bar" classname="TestClass"/>
    <testcase name="test_baz" classname="TestClass"/>
  </testsuite>
</testsuites>

Thanks for the sample xml.

Indeed, our XUnit report parser expects some attributes which are not present in your file (the error message is not helpful at all and we should change that).

Adding the missing attributes would be possible but I think the real problem you face is deeper. When importing the test results, we have to match the test cases of the report against project test files. However, test reports usually give class names instead of test files. Our current implementation tries to match test files based on how nose behaves. That may work for other testing frameworks, but we don’t try to cover all testing frameworks.

One possible workaround for you would be to use SonarQube generic format for test reports. As it’s also an XML format, you may try to setup an XSL transformation.