在 sonarqube 中导入 mocha 单元测试结果

import mocha unit tests result in sonarqube

我使用 mocha 获取单元测试结果,使用 istanbul 获取代码覆盖率。我正在使用 g运行t 来 运行 这些任务。它工作正常。我还使用 grunt-sonar-runner 插件将这些结果导入声纳。目前已导入代码覆盖率,但单元测试结果并非如此。在构建过程中,声纳向我报告:

20:40:19.410 WARN  - Test result will not be saved for test class "Account Controllers User Controller forgot password", because SonarQube associated resource has not been found using file name: "Account Controllers User Controller forgot password.js"
20:40:19.411 WARN  - Test result will not be saved for test class "Account Controllers User Controller login", because SonarQube associated resource has not been found using file name: "Account Controllers User Controller login.js"
20:40:19.413 WARN  - Test result will not be saved for test class "Account Controllers User Controller logout", because SonarQube associated resource has not been found using file name: "Account Controllers User Controller logout.js"

因此,sonar 不保存单元测试结果。我尝试在2.2更改javascript插件版本,或在5.1.1升级声纳系统,但问题是一样的。我还尝试重命名所有 describe 函数以形成文件夹之间带有 . 的文件的正确路径(例如:test.unit.controllers.filename. 我意识到它只适用于一次测试。如果你有超过 1 次测试,它将不起作用。

配置:
* 声纳 (4.5.2)
* javascript 插件 (2.7)

npm 模块:
*摩卡声纳记者(^0.1.3)
* 摩卡咖啡:(^2.1.0)
* g运行t-mocha-test (^0.12.7)

实际上,我使用 istanbul 获得代码覆盖率,使用 mocha 获得单元测试结果。事实上,mocha(xunit 记者)失败了。仅当您有一个测试并且在文件夹和文件名之间使用 . 正确设置了类名时,它才有效。 这是 mocha 和 g运行t 的解决方案,请严格按照以下步骤操作并尊重文件和文件夹的命名:
1. 使用 npm 模块:sonar-mocha-reporter
2. 在您的 package.json 中添加这些行:

    "config": {
        "mocha-sonar-reporter": {
            "classname": "Test",
            "testdir": "test",
            "outputfile": "report/TEST-results.xml"
        }
    }

3。定义 npm test cmd(我定义了一个 g运行t 任务来 运行 test with g运行t mocha-test plugin ):

    "scripts": {
        "test": "grunt runTests"
    }

4。定义 g运行t 任务:

    module.exports = function(grunt) {

        grunt.config.set('mochaTest', {
            test: {
                options: {
                    timeout:           6000,
                    reporter:          'mocha-sonar-reporter',
                    quiet:             false,
                    clearRequireCache: true
                },
                src:     ['test/**/*.js']
            }
        });

        grunt.loadNpmTasks('grunt-mocha-test');
    };

5。使用 g运行t sonar-runner 插件配置您的报告并添加这些行(如果还没有)(选项对象):

   dynamicAnalysis: 'reuseReports',
   tests: 'test',
   javascript: {
       jstestdriver: {
           reportsPath: 'report'
       },
       lcov: {
           reportPath: 'report/lcov.info'
       }
   },
  1. 运行 你用 npm test 命令测试。使用 gruntgulp,它将不起作用。

配置:
* 声纳 (4.5.2)
* javascript 插件 (2.7)

npm 模块:
*摩卡声纳记者(^0.1.3)
* 摩卡咖啡:(^2.1.0)
* g运行t-mocha-test (^0.12.7)

帮助我解决这个问题的有用文档:sonarqube javascript plugin docs

在节点应用程序中,我通过以下步骤解决了这个问题:

首先:在 package.json 文件中,我包含了这一行:

"scripts": {  
    ...,  
    ...,  
    "test": "nyc --reporter=lcov --reporter=text-lcov mocha test/*/*.js"  
},  

第二:安装nyc

npm install nyc --save-dev

最后一个:运行 声纳扫描仪

我在这个案例的 repo 中创建了一个示例:

https://github.com/macielbombonato/docker-sonar/blob/master/tools/scripts/scanner-npm.sh

希望对您有所帮助。

我将 Bamboo 用于 CI + SonarQube,因此这个答案(对于 NodeJS)结合了两者,以便在 Bamboo 中获得测试报告的可见性,并在 SonarQube 中获得测试执行可见性和代码覆盖可见性:

  • yarn add -D mocha-bamboo-reporter mocha-multi-reporters mocha-sonarqube-reporter

  • sonar-project.properties:

# this is for code coverage
sonar.javascript.lcov.reportPaths=coverage/lcov.info
# this is for test reports
sonar.testExecutionReportPaths=coverage/test_results.xml
  • package.json:
    "testWithCoverage": "nyc -r lcov -e .ts -x \"*.test.ts\" mocha --reporter mocha-multi-reporters --reporter-options configFile=mocha-multi-config.json -r ts-node/register test/**/*.spec.ts && nyc report",
  • mocha-multi-config.json:
{
  "reporterEnabled": "mocha-bamboo-reporter, mocha-sonarqube-reporter",
  "mochaSonarqubeReporterReporterOptions": {
    "output": "coverage/test_results.xml"
  },
  "mochaBambooReporterReporterOptions": {
    "output": "coverage/mocha.json"
  }
}