I am trying to generate the code coverage report in HTML format for a module I am writing so I can work out where I need to improve coverage.

Normally when I run tests for my module I use the following command:

vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist vendor/vendorname/m2-module-name/

I tried to generate the report using the following command:

vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist --coverage-html ./report vendor/vendorname/m2-module-name/

PHPUnit HTML output

As you can see PHPUnit is generating the report for the setup module.

Does anybody know how to run code coverage on Magento 2?

I have also tried editing the phpunit.xml.dist to include the following line but it doesn't seem to do anything:

        <log type="coverage-html" target="/usr/share/nginx/html/test-reports/coverage" charset="UTF-8" yui="true" highlight="true"/>

2 Answers 2


I got it working.

I copied dev/tests/unit/phpunit.xml.dist to dev/tests/unit/phpunit.xml and used the following configuration:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <testsuite name="Magento Unit Tests">
        <directory suffix="Test.php">../../../vendor/vendorname/m2-module-name/Test/Unit</directory>
        <ini name="date.timezone" value="America/Los_Angeles"/>
        <ini name="xdebug.max_nesting_level" value="200"/>

Then I ran the phpunit command:

vendor/bin/phpunit -c dev/tests/unit/phpunit.xml --coverage-html ./report vendor/vendorname/m2-module-name/

And it produced the output for the vendor directory instead:

PHPUnit HTML output


To generate code coverage report in HTML format for your custom module, run below command from your Magento root directory:

php ./vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/[vendor Name]/[Module Name]/ --coverage-html coverage

Also, You can generate code coverage report for the specific file. To generate code coverage for the specific file you need to give full path of your test case file.

php ./vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/[vendor Name]/[Module Name]/Test/Unit/Model/[File Name] --coverage-html coverage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.