声纳无法读取我的 CPPUnit 报告

Sonar unable to read my CPPUnit report

我尝试在我的Sonar中集成一个CPPUnit测试用例,但是当我放入报告时,解析出现问题。

# 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

然后,这是生成我的 CPPUnit 报告的 C++ 代码:

#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;
}

然后,这是此 C++ 代码生成的报告:

<?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>

最后,这是 SonarQube 在阅读这份报告时的回答:

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

我的问题是我完全不明白为什么会这样...我的猜测是关于这份报告是 XML 1.0...:/ 我成功地集成了 CPPCheck、RATS,还生成了一个 XML 报告……但我没有使用 CPPUnit

来管理它

在此先感谢您的帮助,

我们最终发现,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