簡體   English   中英

在 sonarqube 中導入 mocha 單元測試結果

[英]import mocha unit tests result in sonarqube

我使用 mocha 來獲取單元測試結果,使用 istanbul 來獲取代碼覆蓋率。 我正在使用 grunt 來運行這些任務。 它工作正常。 我還使用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"

因此,聲納不保存單元測試結果。 我嘗試在 2.2 中更改 javascript 插件版本,或在 5.1.1 中升級聲納系統,但問題是相同的。 我還嘗試重命名所有describe函數以形成文件的正確路徑. 文件夾之間(例如: test.unit.controllers.filename.我意識到它只適用於一個測試。如果你有超過 1 個測試,它將不起作用。

配置:
* 聲納 (4.5.2)
* javascript 插件 (2.7)

npm 模塊:
* mocha-sonar-reporter (^0.1.3)
* 摩卡咖啡:(^2.1.0)
* 咕嚕摩卡測試 (^0.12.7)

我實際上得到了伊斯坦布爾的代碼覆蓋率和 mocha 的單元測試結果。 事實上,mocha(xunit 記者)失敗了。 只有當你有一個測試和一個正確設置的類名時,它才有效. 文件夾和文件名之間。 這是 mocha 和 grunt 的解決方案,請完全按照以下步驟操作並尊重文件和文件夾的命名:
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(我定義了一個 grunt 任務,用 grunt mocha-test插件運行測試):

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

4.定義grunt任務:

    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. 使用 grunt 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 模塊:
* mocha-sonar-reporter (^0.1.3)
* 摩卡咖啡:(^2.1.0)
* 咕嚕摩卡測試 (^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 for 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"
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM