In Changes and improvements to PHPUnit testing in MediaWiki, I wrote about efforts to help speed up PHPUnit code coverage generation for local development. 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, 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.
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)
 [[ https://gerrit.wikimedia.org/r/c/mediawiki/core/+/520459 | One patch changed <whitelist addUncoveredFilesFromWhitelist="true"> to false ]] to help speed up PHPUnit code coverage generation, the [[ https://gerrit.wikimedia.org/r/c/integration/config/+/521190 | second patch flipped the flag back to true in CI ]] for generating complete coverage reports.
 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.