【问题标题】:PHPUnit + Sonarqube exported coverage do not match with actual xml resultPHPUnit + Sonarqube 导出的覆盖率与实际 xml 结果不匹配
【发布时间】:2016-01-21 18:24:09
【问题描述】:

声纳:5.1.2 声纳运行器:2.2.1 php插件:2.6 PHPUnit 4.2.6

我们正在我们的应用程序上运行 phpunit,但我无法按预期获得正确的声纳覆盖率。

在我的 phpunit.xml 中,我们有过滤器定义我们只想覆盖的文件夹。

<whitelist addUncoveredFilesFromWhitelist="false">
            <directory>./site-library-php/src/main/php/BabelCentral/Model/Content</directory>
        </whitelist>

在我的声纳属性中

sonar.modules=php-module
php-module.sonar.phpUnit.coverage.analyzeOnly=true
php-module.sonar.phpUnit.analyzeOnly=true
php-module.sonar.php.tests.reportPath=site-main-php/src/test/target/reports/phpunit.xml
php-module.sonar.php.coverage.reportPath=site-main-php/src/test/target/reports/phpunit.coverage.xml

查看jenkins上的日志,测试似乎运行良好,以:

Tests: 1479, Assertions: 4165, Failures: 58, Incomplete: 14, Skipped: 55.

Generating code coverage report in Clover XML format ... done

Generating code coverage report in HTML format ... done
// further down
11:47:51.973 INFO  - Analyzing PHPUnit test report: site-main-php/src/test/target/reports/phpunit.xml with org.sonar.plugins.php.phpunit.PhpUnitResultParser@35e7e715
11:47:52.604 INFO  - Analyzing PHPUnit unit test coverage report: site-main-php/src/test/target/reports/phpunit.coverage.xml with PHPUnit Unit Test Coverage Result Parser

一切似乎都运行良好。但是,声纳会报告不同的结果,覆盖整个源文件夹。这些是仪表板上显示的数字。

Unit Tests Coverage 1.8%
line coverage 1.8%
Unit test success 95.9% 

是否有声纳尊重 phpunit 配置的过滤器?

注意: 如果我明确设置,我可以达到所需的覆盖率

php-module.sonar.sources

到我想要的目录/文件。只是,逗号分隔的配置比phpunit的xml配置更难管理。

【问题讨论】:

  • 您会注意到我还以 html 格式导出了覆盖范围 - 这是供开发人员使用的,因此当我们在本地机器上运行它时,我们可以看到我们实际覆盖了多少。

标签: phpunit sonarqube


【解决方案1】:

定义应该由 SonarQube 分析的源文件集至关重要:在这组文件上应用编码规则并计算指标。 sonar.sources 因此是一个强制属性,是配置源文件集的主要方式。

其他属性可用于优化源文件集:

  • 您可以配置sonar.tests:测试文件会自动从sonar.sources中排除。
  • 您可能必须排除其他一些 PHP 文件,例如依赖项或使用 sonar.exclusions 生成的代码。

您还应该知道,几个插件可能有助于项目的全球覆盖率:如果您安装了 JavaScript 插件,并且如果 sonar.sources 包含 JavaScript 文件,那么在计算覆盖率指标时也会将它们考虑在内。

如果您只对调整覆盖率感兴趣,可以使用sonar.coverage.exclusions 从覆盖率指标中排除文件。

【讨论】:

  • 是的.. 我认为声纳特性是唯一的方法。我只是很难管理逗号分隔的配置。有没有办法让功能请求允许数组?类似于: sonar.sources[]=src/modules sonar.sources[]=src/models sonar.sources[]=src/helpers
  • 抱歉,我看不出这样的配置如何更容易管理。
  • 嗨,如果我不清楚,对不起。我只是觉得跟踪比较难。但这对我来说可能是独一无二的。你看,我们正在将代码迁移到更加测试驱动的风格,现在我们只想将覆盖范围集中在 14 个文件/文件夹上。我想我们只需要使用逗号分隔的配置。当越来越多的代码可测试并且我们能够减少配置时,这应该不那么痛苦。将此标记为答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 2011-01-18
  • 2019-09-12
  • 1970-01-01
  • 1970-01-01
  • 2014-06-07
  • 1970-01-01
  • 2016-02-29
  • 2017-03-06
相关资源
最近更新 更多