【问题标题】:Unit test result does not show on SonarQube Dashboard for nodejs单元测试结果未显示在 Nodejs 的 SonarQube 仪表板上
【发布时间】:2018-03-01 01:32:50
【问题描述】:

SonarQube 版本:6.0.0 SonarJS:2.17.0.3154

问题

主要目的是在 SonarQube 仪表板中显示覆盖率报告和单元测试结果。虽然我能够获得覆盖率报告,但无法在 SonarQube 仪表板中获得单元测试结果。 我一直在使用 mocha 进行单元测试,使用 istanbul nyc 进行代码覆盖。 mocha 生成的报告是 xml 格式,使用包 mocha-sonar-reporter,覆盖率报告是 lcov 格式。

sonarqube dashboard

Sonar.Property 文件

sonar.projectKey=unittest
sonar.projectName=unittest 
sonar.projectVersion=1.0.0
sonar.sources=src/
sonar.tests=test/
sonar.language=js
sonar.sourceEncoding=UTF-8
sonar.javascript.jstestdriver.reportsPath=/home/jenkinsbot/workspace/devops_stuffs/reports
sonar.javascript.lcov.reportPath=/home/jenkinsbot/workspace/devops_stuffs/reports/lcov.info

SonarQube 日志

_[Pipeline] wrap
Injecting SonarQube environment variables using the configuration: Sonar(dk.147)
[Pipeline] {
[Pipeline] sh
[e_feature_COS-239-unit-test-P23YKERWYLNSD6K3ISKUHA5NSJTRN6K7DRJGKI5QLG6LTD3HHOPA] Running shell script

/home/jenkinsbot/sonar-scanner-2.9.0.670/bin/sonar-scanner
INFO: Scanner configuration file: /home/jenkinsbot/sonar-scanner-2.9.0.670/conf/sonar-scanner.properties
INFO: Project root configuration file: /home/jenkinsbot/workspace//sonar-project.properties
INFO: SonarQube Scanner 2.9.0.670
INFO: Java 1.8.0_131 Oracle Corporation (64-bit)
INFO: Linux 4.4.0-92-generic amd64
INFO: User cache: /home/jenkinsbot/.sonar/cache
INFO: Load global repositories
INFO: Load global repositories (done) | time=94ms
INFO: User cache: /home/jenkinsbot/.sonar/cache
INFO: Load plugins index
INFO: Load plugins index (done) | time=4ms
INFO: SonarQube server 6.0
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Process project properties
INFO: Load project repositories
INFO: Load project repositories (done) | time=100ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=17ms
INFO: Load active rules
INFO: Load active rules (done) | time=231ms
INFO: Publish mode
INFO: ------------- Scan cosmicunittest
INFO: Language is forced to js
INFO: Load server rules
INFO: Load server rules (done) | time=71ms
INFO: Base dir: /home/jenkinsbot/workspace/
INFO: Working dir: /home/jenkinsbot/workspace/eP23YKERWYLNSD6K3ISKUHA5NSJTRN6K7DRJGKI5QLG6LTD3HHOPA/.scannerwork
INFO: Source paths: src
INFO: Test paths: test
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Index files
INFO: 152 files indexed
INFO: Quality profile for js: LogPoint Way
INFO: Sensor Lines Sensor
INFO: Sensor Lines Sensor (done) | time=33ms
INFO: Sensor SCM Sensor
INFO: Sensor SCM Sensor (done) | time=20ms
INFO: Sensor JavaScript Squid Sensor
INFO: 138 source files to be analyzed
INFO: Unit Test Coverage Sensor is started
INFO: 138/138 source files have been analyzed
INFO: Analysing [/home/jenkinsbot/workspace/reports/lcov.info]
INFO: Integration Test Coverage Sensor is started
INFO: Overall Coverage Sensor is started
INFO: Analysing [/home/jenkinsbot/workspace/reports/lcov.info]
INFO: Sensor JavaScript Squid Sensor (done) | time=4852ms
INFO: Sensor XmlFileSensor
INFO: Sensor XmlFileSensor (done) | time=1ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=76ms
INFO: Sensor Code Colorizer Sensor
INFO: Sensor Code Colorizer Sensor (done) | time=3ms
INFO: Sensor CPD Block Indexer
INFO: DefaultCpdBlockIndexer is used for js
INFO: Sensor CPD Block Indexer (done) | time=1ms
INFO: Calculating CPD for 129 files
INFO: CPD calculation finished
INFO: Analysis report generated in 222ms, dir size=903 KB
INFO: Analysis reports compressed in 300ms, zip size=512 KB
INFO: Analysis report uploaded in 24ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://test/dashboard/index/unittest
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at test
INFO: Task total time: 7.649 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 8.784s
INFO: Final Memory: 61M/551M
INFO: ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS_

【问题讨论】:

    标签: node.js sonarqube sonar-runner sonarjs


    【解决方案1】:

    在这里回答https://github.com/SonarSource/sonar-javascript/issues/751

    SonarJS 不再提供单元测试结果导入。为此,您应该使用 SQ 的原生功能 Generic Test Data(自 SQ 6.2 起)。如果您使用 SQ https://docs.sonarqube.org/display/PLUG/Generic+Test+Coverage。

    【讨论】:

    • 我尝试使用 Generic Test Coverage 插件,但是使用 mocha-sonar-reporter 插件的 mocha 工具生成的 XML 未被此插件解析。我已将 sonar.property 添加为 sonar.genericcoverage.unitTestReportPaths= testpath。我检查了通用测试覆盖率的官方文档,其中显示的示例 XML 的格式与我的 mocha 生成的格式不同。
    • @sudeepparajuli 即使我尝试使用 sonar.property 生成通用测试覆盖率作为 sonar.genericcoverage.unitTestReportPaths= 添加到 sonar.property 中的测试路径。生成的 xml 将 testsuite 作为根节点,根据官方文档,它应该将 testExecutions 作为根节点。将解析错误作为未知 XML 节点,预期为“testExecutions”但得到“testsuite”。我仍在寻找解决方案,您是否找到了适合您的解决方案以及如何解决,请帮助。
    猜你喜欢
    • 2016-02-03
    • 2017-11-18
    • 2017-12-11
    • 1970-01-01
    • 2018-09-19
    • 2016-08-24
    • 2018-08-08
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多