importing javascript xml junit tests to SONAR using jstestdriver fails

时间秒杀一切 提交于 2019-12-04 00:17:36

For anyone reading this and using Grunt (instead of Gulp) there is a plugin for grunt and karma to convert unit test results into the appropriate format to import into SonarQube. Install the plugin and set the Grunt configuration. For example:

        my_target: {
            project: {
                key: 'projectKey',
                name: 'projectName',
                version: package.version
            },
            paths: [{
                cwd: '.',
                src: './src', // source being tested
                test: './tests/unit', // tests for the source
                reports: {
                    // karma output for unit test results
                    unit: './tests/reports/dev/completion/unit.xml',
                    // karma output for unit test coverage
                    coverage: './tests/reports/dev/coverage/lcov/lcov.info'
                }
            }]
        }

By default this will take the files karma created and convert into ./tmp/sonar/results

Then in your sonar properties add something like

sonar.tests=./tests/unit
sonar.sources=./src
sonar.javascript.jstestdriver.reportsPath=.tmp/sonar/results/
sonar.javascript.lcov.reportPath=.tmp/sonar/results/coverage_report.lcov

Note: One thing that bit me at first was that tests named like test.spec.js will get converted to test_spec when Sonar runs, so then when it goes to map back the results to the files, it still looks for test_spec.js instead of test.spec.js. I fixed that just by changing my test names to us the underscore off the bat.

Another Note: When using this approach with Jenkins, I used it just for the conversion of the test results, not for anything else. Because of this, the Grunt task for karmaSonar would fail (since there was no instance of SonarQube configured or found on the box), so if you don't want your whole build to fail make sure to use the --force flag with Grunt. I found that the files were converted just fine even though the task failed. Then I used Invoke Standalone SonarQube Analysis step in my job to run the static code analysis and pick up the test results.

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!