Sonar unable to read my CPPUnit report

时间:2016-10-13 06:52:58

标签: c++ xml continuous-integration sonarqube cppunit

I try to integrate a CPPUnit Test cases in my Sonar, but when I put the report, there is a problem with the parsing.

# must be unique in a given SonarQube instance
sonar.projectKey=test

# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=test

sonar.projectVersion=1.0

sonar.sources=./core/

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

sonar.language=c++

sonar.cxx.xunit.reportPath=cppunit-report.xml

sonar.cxx.rats.reportPath=rats-report.xml

sonar.cxx.cppcheck.reportPath=cppcheck-report.xml

Then, here is the c++ code in order to generate my report of CPPUnit:

#include "PropositionalVariableTest.h"

CPPUNIT_TEST_SUITE_REGISTRATION( PropositionalVariableTest );

int main(int argc, char* argv[])
{
    // informs test-listener about testresults
    CPPUNIT_NS::TestResult testresult;

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener (&collectedresults);

    // register listener for per-test progress output
    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener (&progress);

    // insert test-suite at test-runner by registry
    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
    testrunner.run(testresult);

    // output results in compiler-format
    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write ();

    // Output XML for Jenkins CPPunit plugin
    ofstream xmlFileOut("../cppunit-report.xml");
    XmlOutputter xmlOut(&collectedresults, xmlFileOut);
    xmlOut.write();

    // return 0 if tests were successful
    return collectedresults.wasSuccessful() ? 0 : 1;
}

Then, here is the report generated by this C++ code:

<?xml version="1.0" encoding='ISO-8859-1' standalone='yes' ?>
<TestRun>
  <FailedTests></FailedTests>
  <SuccessfulTests>
    <Test id="1">
      <Name>PropositionalVariableTest::testInitCorrect</Name>
    </Test>
  </SuccessfulTests>
  <Statistics>
    <Tests>1</Tests>
    <FailuresTotal>0</FailuresTotal>
    <Errors>0</Errors>
    <Failures>0</Failures>
  </Statistics>
</TestRun>

And finally, here is what SonarQube answers when it reads this report:

INFO: Sensor CxxXunitSensor
INFO: Scanner found '1' report files
INFO: Parser will parse '1' report files
INFO: Processing report '/.../cppunit-report.xml'
ERROR: Cannot feed the data into SonarQube, details: 'javax.xml.stream.XMLStreamException: ParseError at [row,col]:[16,1]
Message: Can not call 'getAttributeValue(): cursor does not point to a valid node (curr event [null]; cursor state CLOSED)'
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 18.886s
INFO: Final Memory: 57M/364M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution

My question is that I totally don't understand why it is like this... My guess is that it is about the fact that this report is in XML 1.0... :/ I managed to integrate CPPCheck, RATS without any problem, by also generating a XML report... But I don't manage it with CPPUnit

Thanks in advance for the help,

1 个答案:

答案 0 :(得分:1)

正如我们最终发现的那样,XML必须通过一些XSL转换为不同的格式,这个格式在网站上为插件提供:

https://github.com/SonarOpenCommunity/sonar-cxx/blob/master/sonar-cxx-plugin/src/main/resources/xsl/cppunit-1.x-to-junit-1.0.xsl