问题描述
我使用 mocha 来获取单元测试结果,并使用 istanbul 来获取代码覆盖率.我正在使用 grunt 来运行这些任务.它工作正常.我还使用 grunt-sonar-runner
plugin 将这些结果导入声纳.当前代码覆盖率已导入,但单元测试结果并非如此.在构建期间,声纳向我报告:
I use mocha to get unit tests results and istanbul to get code coverage. I'm using grunt to run these tasks. It works fine. I'm also using grunt-sonar-runner
plugin to import these result in sonar. Currently code coverage is imported but that is not the case for unit tests results. During the build, sonar report me this :
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 个测试,它就不起作用了.
Because of this, sonar don't save unit tests results. I tryed to change the javascript plugin version in 2.2, or upgrade sonar system in 5.1.1 but the problem is the same. I also try to rename all describe
function to form the right path to the file with .
between folders (e.g: test.unit.controllers.filename.
And I realized that it works for only one test. If you have more than 1 tests, it will not work.
配置:
* 声纳 (4.5.2)
* javascript 插件 (2.7)
configuration:
* sonar (4.5.2)
* javascript plugin (2.7)
npm 模块:
* mocha-sonar-reporter (^0.1.3)
* 摩卡:(^2.1.0)
* 咕噜摩卡测试 (^0.12.7)
npm modules:
* mocha-sonar-reporter (^0.1.3)
* mocha: (^2.1.0)
* grunt-mocha-test (^0.12.7)
推荐答案
我实际上得到了 istanbul 的代码覆盖率和 mocha 的单元测试结果.事实上,摩卡咖啡(xunit 记者)失败了.仅当您在文件夹和文件名之间使用 .
正确设置了一个测试和类名时,它才有效.这是 mocha 和 grunt 的解决方案,请完全按照这些步骤操作并尊重文件和文件夹的命名:
1. 使用 npm 模块:sonar-mocha-reporter
2. 在你的 package.json 中添加这些行:
I actually get code coverage with istanbul and unit tests results with mocha. Indeed, the mocha (xunit reporter) fails. It only works if you have one test and a classname correctly set with .
between folders and filename.
Here is the solution with mocha and grunt, please follow exactly these steps and respect naming of files and folders :
1. Use npm module : sonar-mocha-reporter
2. Add these lines in your package.json:
"config": {
"mocha-sonar-reporter": {
"classname": "Test",
"testdir": "test",
"outputfile": "report/TEST-results.xml"
}
}
3.定义 npm test cmd(我定义了一个 grunt 任务来使用 grunt mocha-test
插件运行测试):
3. Define npm test cmd (I defined a grunt task to run test with grunt mocha-test
plugin ):
"scripts": {
"test": "grunt runTests"
}
4.定义 grunt 任务:
4. Define the grunt task:
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
插件配置您的报告,如果还没有,请添加这些行(选项对象):
5. Configure your reporting with grunt sonar-runner
plugin and add these lines if it not already the case (options object):
dynamicAnalysis: 'reuseReports',
tests: 'test',
javascript: {
jstestdriver: {
reportsPath: 'report'
},
lcov: {
reportPath: 'report/lcov.info'
}
},
- 使用
npm test
命令运行您的测试.使用grunt
或gulp
将无法正常工作.
- Run your test with
npm test
command. withgrunt
orgulp
, it won't work.
配置:
* 声纳 (4.5.2)
* javascript 插件 (2.7)
configuration:
* sonar (4.5.2)
* javascript plugin (2.7)
npm 模块:
* mocha-sonar-reporter (^0.1.3)
* 摩卡:(^2.1.0)
* 咕噜摩卡测试 (^0.12.7)
npm modules:
* mocha-sonar-reporter (^0.1.3)
* mocha: (^2.1.0)
* grunt-mocha-test (^0.12.7)
帮助我解决这个问题的有用文档:sonarqube javascript 插件文档
Useful documentation which helped me to solve this problem : sonarqube javascript plugin docs
这篇关于导入 mocha 单元测试导致 sonarqube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!