PHPUnit + Sonarqube exported coverage do not match with actual xml result

sonarqube: 5.1.2 sonar-runner: 2.2.1 php plugin: 2.6 PHPUnit 4.2.6

We're running phpunit over our application but I'm not able to get the correct coverage % on sonar as expected.

In my phpunit.xml we have filters defining folders that we only want to cover.

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

In my sonar properties

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

Viewing the log on jenkins, tests seem to run fine and ends with:

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

All seems to be working well. However, sonar would report a different result, covering the entire source folder. These are the number shown on the dashboard.

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

Is there anyway to have sonar respect phpunit's configured filter?

Note: I'm able to achieve the desired numbers on coverage if I explicitly set

php-module.sonar.sources

to the directories/files that I want. It's just that, comma-separated config is hard to manage than phpunit's xml config.

Upvotes: 2

Views: 3337

Answers (1)

Pierre-Yves
Pierre-Yves

Reputation: 1546

Defining the set of source files which should be analysed by SonarQube is crucial: it's on this set of files that coding rules are applied and that metrics are computed. sonar.sources is therefore a mandatory property and is the main way to configure the set of source files.

Other properties may be used to refine the set of source files:

  • You may configure sonar.tests: test files are automatically excluded from sonar.sources.
  • You may have to exclude some other PHP files like dependencies or generated code with sonar.exclusions.

You should also be aware that several plugins may contribute to the global coverage of the project: if you installed the JavaScript plugin and if sonar.sources contains JavaScript files, they will also be taken into account when calculating the coverage metrics.

If you're only interested in adjusting coverage, you can exclude files from coverage metrics with sonar.coverage.exclusions.

Upvotes: 1

Related Questions