Running Magento 2 Unit Tests With Code Coverage

Over the past several weeks I’ve been preparing a talk on Magento 2 for the PHP Forum 2015 conference in Paris. Part of the talk deals with Magento’s unit tests, so I had to run them many times against the latest updates – but I keep on running into the same issue whenever I executed unit tests with coverage reporting enabled:

Trying to @cover or @use not existing method "\Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::afterImportData".

This looks definitely like an issue on their side, and it always happens at around 17%. But since all I want is to see rough coverage figures for different areas of the code-base, here’s a quick temporary workaround that gets rid of all @covers annotations.

This more a note-to-self than anything else really, and its definitely not a solution. You should know that coverage might be affected (inflated) by this change, which is something I’ll mention in the talk.

Note: this was only tested on Mac OSx 10.11.1 – but regardless of what you’re using: if you choose to execute the following commands, you should know you’re doing it at your own risk.
find . -type f -name "*Test.php" -exec sed -i '' [email protected]/c_over/g {} +
find . -type f -name "*Test.php" -exec sed -i '' [email protected]/u_se/g {} +

The command for linux should be very similar: check the documentation for sed for the correct syntax.

I do hope I can save someone else the 5 minutes it takes to put those commands together in order to at least get some “idea” of how much coverage Magento 2 has 🙂 – for me that’s about 41% at this point, with 2 failed tests and 16000+ assertions. Which is huge when compared to other PHP eCommerce solutions.

Leave a Reply