使用 jstestdriver 将 javascript xml junit 测试导入 SONAR 失败 [英] importing javascript xml junit tests to SONAR using jstestdriver fails

查看:18
本文介绍了使用 jstestdriver 将 javascript xml junit 测试导入 SONAR 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么?我目前正在为前端应用程序设置持续集成/交付管道.我们计划使用 SonarQube 作为代码质量工具,并希望在 SONAR 中导入 junit 测试结果/代码覆盖率以进行进一步分析.

What I am doing? I am currently working on setting up Continuous integration / delivery pipeline for front end applications. We are planning to use SonarQube as code quality tool and want to import junit test results/code coverage in SONAR for further analysis.

我能够成功地将 javascript 代码覆盖率报告导入 SONAR.我们正在使用 Gulp/Karma/PhantomJS/Jasmine 等,并使用 Karma 覆盖率/Junit 记者来生成测试结果.Junit 测试文件名为 TEST-xunit.xml.

I was able to successfully import javascript code coverage report to SONAR. We are using Gulp / Karma / PhantomJS / Jasmine etc and using Karma coverage / Junit reporters to generate test results. Junit test file name is TEST-xunit.xml.

我被困在哪里了?关于将 Javascript 测试结果导入 SONAR,我需要您的帮助/专家指导.我尝试了不同的方法,但不知何故无法将单元测试源/结果导入 SONAR.

Where I am stuck? I need your help/expert guidance on importing Javascript test results to SONAR. I tried different things but somehow not able to import the unit test sources/results to SONAR.

我都尝试过什么?早些时候,我们使用的是 SONAR Runner 2.3、SONAR Qube 4.5/Javascript 插件 2.2,并且能够导入 Junit 高级统计信息,例如测试通过/失败等.但是,我们无法导航到单个测试以查看哪个测试通过/失败.

What all I have tried? Earlier we were using SONAR Runner 2.3, SONAR Qube 4.5 / Javascript plugin 2.2 and able to import Junit high level stats like tests passed / failed etc. However, we were not able to navigate to individual tests to see which test passed / failed.

在进一步研究中,注意到如果 Javascript 插件 2.3 没有找到测试源,它会为该场景添加更多消息传递.此外,还有一些其他依赖项,因此将 SONAR qube 升级到 5.0.使用 SONAR runner 2.4 进行测试并观察到以下消息.目前,还没有导入高级统计数据.

On researching further, noticed that Javascript plugin 2.3 added more messaging for the scenario if it does not find test sources. Also, there were some other dependencies so upgraded SONAR qube to 5.0. Tested with SONAR runner 2.4 and observed following messages. Currently, high level stats are also not getting imported.

JSTestDriverSensor.java代码来自github上的SONAR Javascript插件)

JSTestDriverSensor.java code is from SONAR Javascript plugin on github)

LOG.warn("Test result will not be saved for test class "{}", because SonarQube associated resource has not been found using file name: "{}"",
    getUnitTestClassName(classKey), getUnitTestFileName(classKey));

SONAR gulp 插件生成的sonar-runner.properties(使用SONAR Runner 2.3)

sonar.projectBaseDir=C:/xxxxx/homepage2
sonar.host.url=http://xxxxx:9000/sonarqube/
sonar.jdbc.url=jdbc:mysql:// xxxxx:3306/sonar
sonar.jdbc.username= xxxxx
sonar.jdbc.password= xxxxx
sonar.projectKey=homepage:0.0.0
sonar.projectName=homepage
sonar.projectVersion=0.0.0
sonar.sources=src/app,src/components
sonar.exclusions=src/**/*spec.js
sonar.tests=test
sonar.language=js
sonar.sourceEncoding=UTF-8
sonar.javascript.lcov.reportPath=reports/coverage/lcov.info
sonar.javascript.jstestdriver.reportsPath=reports/unit

我什至尝试单独安装 SONAR runner 2.4,但始终收到相同的错误消息.

I even tried separate SONAR runner 2.4 installation but keep getting same error messages.

问题?1. 一定有一些东西我忽略了,并且不知何故无法确定是什么原因造成的.我认为要么我设置测试目录路径错误,要么 JSTestDriverSensor 解释测试目录文件错误.建议?2. 首先,我只想导入一个示例 junit xml 文件,看看它是否导入成功.之后,我可以修改业力 junit 生成器输出以匹配输入 junit xml.这里需要一些参考示例.

Question? 1. There must be something which I am overlooking and somehow not able to determine what might be causing this. I think either I am setting test directory path wrong or JSTestDriverSensor is interpreting test directory file wrong. Suggestions? 2. To start with, I just want to import a sample junit xml file to see if it gets imported successfully. After that, I can modify karma junit generator output to match input junit xml. Need some reference examples here.

附加一些日志.我修改了 junit xml 以设置不同的类名,以查看 JsTestDriverSensor 是否找到它.但事实并非如此!

Attaching some of the logs. I modified junit xml to set different classnames to see if JsTestDriverSensor finds it. But it’s not!

[23:31:24] 23:31:24.122 DEBUG - Language of file 'src/app/main/textOverride.controller.js' is detected to be 'js'
23:31:24.122 DEBUG - Language of file 'src/app/main/main.routes.js' is detected to be 'js'
23:31:24.123 DEBUG - Language of file 'test/app/main/testSpec.js' is detected to be 'js'

[23:31:24] 23:31:24.138 DEBUG - Language of file 'src/components/navbar/navbar.controller.js' is detected to be 'js'

[23:31:24] 23:31:24.169 INFO  - 7 files indexed

[23:31:26] 23:31:26.632 INFO  - Quality profile for js: Sonar way

[23:31:26] 23:31:26.670 DEBUG - Sensors : QProfileSensor -> JavaScriptSquidSensor -> JsTestDriverSensor -> CoverageSensor -> InitialOpenIssuesSensor -> ProjectLinksSensor -> VersionEventsSensor -> FileHashSensor -> SCM Sensor (wrapped) -> CPD Sensor (wrapped)
23:31:26.671 INFO  - Sensor QProfileSensor...

[23:31:26] 23:31:26.680 INFO  - Sensor QProfileSensor done: 9 ms
23:31:26.680 INFO  - Sensor JavaScriptSquidSensor...

[23:31:26] 23:31:26.950 INFO  - 6 source files to be analyzed

[23:31:27] 23:31:27.081 INFO  - 6/6 source files analyzed

[23:31:27] 23:31:27.113 INFO  - Sensor JavaScriptSquidSensor done: 433 ms

[23:31:27] 23:31:27.114 INFO  - Sensor JsTestDriverSensor...
23:31:27.114 INFO  - Parsing Unit Test run results in Surefire format from folder C:xxxxxhomepage2
eportsunit

[23:31:27] 23:31:27.176 WARN  - Test result will not be saved for test class "app.main.testSpec", because SonarQube associated resource has not been found using file name: "app/main/testSpec.js"

[23:31:27] 23:31:27.177 WARN  - Test result will not be saved for test class "src.app.main.testSpec", because SonarQube associated resource has not been found using file name: "src/app/main/testSpec.js"

推荐答案

对于阅读本文并使用 Grunt(而不是 Gulp)的任何人,都有一个 grunt 和 karma 将单元测试结果转换为适当的格式以导入 SonarQube.安装插件并设置 Grunt 配置.例如:

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'
                }
            }]
        }

默认情况下,这将获取创建的文件 karma 并转换为 ./tmp/sonar/results

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

注意:一开始让我很不爽的一件事是,名为 test.spec.js 的测试会在 Sonar 运行时转换为 test_spec,所以当它将结果映射回文件,它仍然寻找 test_spec.js 而不是 test.spec.js.我只是通过将我的测试名称更改为我们的下划线来解决这个问题.

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.

另一个注意事项:在 Jenkins 中使用这种方法时,我只是将它用于测试结果的转换,而不是用于其他任何事情.因此,karmaSonar 的 Grunt 任务将失败(因为在盒子上没有配置或找到 SonarQube 的实例),所以如果您不希望整个构建失败,请确保使用 Grunt 的 --force 标志.我发现即使任务失败,文件也转换得很好.然后我在工作中使用 Invoke Standalone SonarQube Analysis 步骤运行静态代码分析并获取测试结果.

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.

希望这会有所帮助!

这篇关于使用 jstestdriver 将 javascript xml junit 测试导入 SONAR 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆