简体   繁体   English

如何让 SonarQube 分析单元测试中的代码气味,但不将它们计入覆盖率报告?

[英]How do I get SonarQube to analyse Code Smells in unit tests but not count them for coverage reports?

I have C++ project being analysed with the commercial SonarQube plugin.我使用商业 SonarQube 插件分析了 C++ 项目。

My project has, in my mind, an artificially high code coverage percentage reported as both the "production" source code lines and Unit Test code lines are counted.在我看来,我的项目有一个人为的高代码覆盖率百分比报告为“生产”源代码行和单元测试代码行都被计算在内。 It is quite difficult to write many unit test code lines that are not run as a part of the unit testing, so they give an immediate boost to the coverage reports.编写许多不作为单元测试的一部分运行的单元测试代码行是非常困难的,因此它们可以立即提升覆盖率报告。

Is it possible to have the Unit Test code still analysed for Code Smells but not have it count towards the test coverage metric?是否有可能仍然对单元测试代码进行代码气味分析,但不计入测试覆盖率指标?

I have tried setting the sonar.tests=./Tests parameter (where ./Tests is the directory with my test code. This seems to exclude the test code from all analysis, leaving smells undetected. I would rather check that the test code is of good quality than hope it is obeying the rules applied to the project.我已经尝试设置sonar.tests=./Tests参数(其中 ./Tests 是包含我的测试代码的目录。这似乎从所有分析中排除了测试代码,使气味未被发现。我宁愿检查测试代码是否为质量好于希望它遵守适用于项目的规则。

I tried adding the sonar.test.inclusions=./Tests/* in combination with the above.我尝试将sonar.test.inclusions=./Tests/*与上述结合添加。 However, I either got the file path syntax incorrect or setting this variable causes a complete omission of the Test code, so that it no longer appears under the 'Code' tab at all as well as being excluded.但是,我要么文件路径语法不正确,要么设置此变量导致完全省略测试代码,因此它根本不再出现在“代码”选项卡下,也被排除在外。

The documentation on Narrowing the Focus of what is analysed is not all the clear on what the expected behaviour is, at least to me.关于缩小所分析内容的焦点文档并不完全清楚预期的行为是什么,至少对我而言。 Any help would be greatly appreciated as going through every permutation will be quite confusing.任何帮助将不胜感激,因为经历每一个排列都会非常混乱。

Perhaps I should just accept the idea that with ~300 lines of "production" code and 900 lines of stubs, mocks and unit tests a value of 75% test coverage could mean running 0 lines of "production" code.也许我应该接受这样一个想法,即使用约 300 行“生产”代码和 900 行存根、模拟和单元测试,75% 的测试覆盖率可能意味着运行 0 行“生产”代码。 I checked and currently, my very simple application is at about that ratio of test code to "production" code.我检查了一下,目前,我非常简单的应用程序大约是测试代码与“生产”代码的比率。 I'd expect the ratio to move more towards 50:50 over time but it might not do.我预计随着时间的推移,该比率会进一步向 50:50 移动,但可能不会。

One solution I found was to have two separate SonarQube projects for a single repository.我发现的一个解决方案是为单个存储库设置两个单独的 SonarQube 项目。 The first you setup in the normal way, with the test code excluded via sonar.tests=./Tests .第一个以正常方式设置,通过sonar.tests=./Tests排除测试代码。 The second you make a -test repository where you exclude all your production code.第二个是创建一个-test存储库,在其中排除所有生产代码。

This adds some admin and setup but guarantees that coverage for the normal project is a percentage of only the production code and that you have SonarQube Analysis performed on all your test code (which can also have coverage tracked and would be expected to be very high).这增加了一些管理和设置,但保证正常项目的覆盖率仅是生产代码的一部分,并且您对所有测试代码执行了 SonarQube 分析(也可以跟踪覆盖率并且预计会非常高) .

I struggle to remember where I found this suggestion a long time ago.我很难记住很久以前我在哪里找到了这个建议。 Possibly somewhere in the SonarQube Community Forum , which is worth a look if you are stuck on something.可能在SonarQube 社区论坛的某个地方,如果您遇到某些问题,值得一看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SONARQUBE中的单元测试范围 - Unit Tests coverage in SONARQUBE 如何为Android单元测试生成代码覆盖率报告 - How to generate code coverage reports for Android unit tests 如何在Xcode 4中获得测试和构建iPhone的代码覆盖率? - How do I get code coverage for tests and build for iPhone in Xcode 4? 如何在Android单元测试期间使用emma进行代码覆盖 - How To do code coverage with emma during Android Unit Tests TeamCity针对C#单元测试使用dotCover报告了错误的代码覆盖率 - TeamCity reports incorrect code coverage with dotCover for c# unit tests sonarqube显示0个单元测试的数据覆盖率 - sonarqube displays 0 unit tests data coverage 为什么我没有在SonarQube中看到“单元测试覆盖”小部件的“单元测试成功”部分 - why am I not seeing “unit test success” section of “unit tests coverage” widget in SonarQube sbt单元和集成测试的聚合覆盖率报告 - sbt aggregate coverage reports for unit and integration tests 如何通过对该组件的单元测试获得 100% 的代码覆盖率并访问 else 部分? - How do I get hundred percent code coverage with my unit testing on this component and access the else part? Jenkins / SonarQube JavaScript的单元测试代码覆盖率 - Jenkins/SonarQube unit testing code coverage for JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM