2

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:

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

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"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
         colors="true"
         bootstrap="./framework/bootstrap.php"
        >
    <testsuite name="Magento Unit Tests">
        <directory suffix="Test.php">../../../vendor/vendorname/m2-module-name/Test/Unit</directory>
    </testsuite>
    <php>
        <ini name="date.timezone" value="America/Los_Angeles"/>
        <ini name="xdebug.max_nesting_level" value="200"/>
    </php>
</phpunit>

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

1

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, privacy policy and cookie policy

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