HomePhabricator
Integrating code coverage metrics with your development workflow

In Changes and improvements to PHPUnit testing in MediaWiki, I wrote about efforts to help speed up PHPUnit code coverage generation for local development.[0] While this improves code coverage generation time for local development, it could be better.

As the Manual:PHP unit testing/Code coverage page advises, adjusting the whitelist in the PHPUnit XML configuration can speed things up dramatically. The problem is, adjusting that file is a manual process and a little cumbersome, so I usually didn't do it. And then because code coverage generation reports were slow locally[1], I ended up not running them while working on a patch. True, you will get feedback on code coverage metrics from CI, but it would be nicer if you could quickly get this information in your local environment first.

This was the motivation to add a Composer script in MediaWiki core that will help you adjust the PHPUnit coverage whitelist quickly while you're working on a patch for an extension or skin.

You can run it with composer phpunit:coverage-edit -- extensions/$EXT_NAME, e.g. composer phpunit:coverage-edit -- extensions/GrowthExperiments.

The ComposerPhpunitXmlCoverageEdit.php script copies the phpunit.xml.dist file to phpunit.xml (not version controlled), and modifies the whitelist to add directories for that extension/skin. vendor/bin/phpunit then reads phpunit.xml instead of the phpunit.xml.dist file. Tip: Make sure "Edit configurations" in your IDE (PhpStorm in my case) is using vendor/bin/phpunit and phpunit.xml, not phpunit.xml.dist, when executing the tests.

generating phpunit.xml and running code coverage in phpstorm

When you want to reset your configuration, you can rm phpunit.xml and vendor/bin/phpunit will read from phpunit.xml.dist again.

Further improvements to the script could include:

  • Reading the extension.json file to determine which directories to add to the whitelist, rather than using a hardcoded list (T235029)
  • Allow passing arbitrary directories/filenames, e.g. for working with subsections of core or of a larger extension (T235030)
  • Adding a flag for flipping the addUncoveredFilesFromWhitelist property, so that phpunit-suite-edit.py in the integration/config repo could be removed in favor of the Composer script (T235031)

Thanks to @Mainframe98 and @Krinkle for review of the patch and to @AnneT for reviewing this post. Happy hacking!


[0] One patch changed <whitelist addUncoveredFilesFromWhitelist="true"> to false to help speed up PHPUnit code coverage generation, the second patch flipped the flag back to true in CI for generating complete coverage reports.
[1] For GrowthExperiments, generating coverage reports without a customized whitelist takes ~17 seconds. With a custom whitelist, it takes ~1 second. While 17 seconds is arguably not a lot of time, the near-instant feedback with a customized whitelist means one is less likely to face interruptions to their flow or concentration while working on a patch.

Written by kostajh on Oct 9 2019, 10:04 AM.
Senior Software Engineer (Growth)
Projects
Subscribers
1Lordevss1, Jrbranaa, Mainframe98 and 2 others
Tokens
"Like" token, awarded by Niedzielski."Like" token, awarded by mmodell."Love" token, awarded by Jrbranaa."Like" token, awarded by Ammarpad."Love" token, awarded by AnneT."Love" token, awarded by Mainframe98.

Event Timeline