gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:09:56 25 4
gpt4 key购买 nike

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

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 中升级 Sonar 系统,但问题是一样的。我还尝试重命名所有 describe 函数,以在文件夹之间使用 . 形成文件的正确路径(例如:test.unit.controllers.filename. 我意识到它只适用于一个测试。如果你有超过 1 个测试,它就不会工作。

配置:
* Sonar (4.5.2)
* javascript插件(2.7)

npm 模块:
* mocha-sonar-reporter (^0.1.3)
* Mocha :(^2.1.0)
*咕噜 Mocha 测试(^0.12.7)

最佳答案

我实际上使用 istanbul 获得代码覆盖率,使用 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,它不会工作。

配置:
* Sonar (4.5.2)
* javascript插件(2.7)

npm 模块:
* mocha-sonar-reporter (^0.1.3)
* Mocha :(^2.1.0)
*咕噜 Mocha 测试(^0.12.7)

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

关于javascript - 在 sonarqube 中导入 mocha 单元测试结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31359783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com