PHPUnit - How To Generate An HTML Code Coverage Report

PHPUnit - How To Generate An HTML Code Coverage Report

In this article, we will explore generating an HTML code coverage report with PHPUnit. In this article, you will install php-xdebug and generate an HTML code coverage report using PHPUnit. When you're finished, you'll be able to understand how to generate an HTML code coverage report using PHPUnit. 

Pre-requisites

To complete this tutorial, you will need:

Step 1 — Install The Required php-xdebug Library

In this step, we are going to install the php-xdebug library required to generate the html code coverage.

Run the following command to install the library:


sudo apt-get install php-xdebug

You'll see the following output:


Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libllvm11 libllvm11:i386 shim
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
php-xdebug
0 upgraded, 1 newly installed, 0 to remove and 93 not upgraded.
Need to get 473 kB of archives.
After this operation, 2,174 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 php-xdebug amd64 2.9.2+2.8.1+2.5.5-1build1 [473 kB]
Fetched 473 kB in 2s (235 kB/s)
Selecting previously unselected package php-xdebug. (Reading database ... 270790 files and directories currently installed.)
Preparing to unpack .../php-xdebug_2.9.2+2.8.1+2.5.5-1build1_amd64.deb ...
Unpacking php-xdebug (2.9.2+2.8.1+2.5.5-1build1) ...
Setting up php-xdebug (2.9.2+2.8.1+2.5.5-1build1) ...
Processing triggers for php7.4-cli (7.4.3-4ubuntu2.6) ...
Processing triggers for libapache2-mod-php7.4 (7.4.3-4ubuntu2.6) ...
 

Step 2 — Create a phpunit.xml file in your project directory

We are going to use the following project directory:


/home/digitalriver/phpprojects/helloworld-im-unit-testing

Navigate to the created directory:


cd /home/digitalriver/phpprojects/helloworld-im-unit-testing

Let's create a phpunit.xml file:

touch phpunit.xml

Modify the contents of the class file using your favorite text editor for this example we are using vi:


vi phpunit.xml

Once the text file opens up in the terminal window, type out the following:


<phpunit colors="true">
    <testsuites>
       <testsuite name="Test Suite">
            <directory>./</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processuncoveredfilesfromwhitelist="true">
            <directory suffix=".php">./</directory>
        </whitelist>
    </filter>
</phpunit>

Save the file contents and quit the text editor.

Step 3 — Generate the code coverage

Once we have configured our test coverage options in our phpunit.xml, we can now run the coverage report by running the following command:

Once the text file opens up in the terminal window, type out the following:


phpunit --coverage-html reports helloworldtest.php

You’ll see the following output:


PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

. 1 / 1 (100%)
Time: 241 ms, Memory: 6.00 MB

OK (1 test, 1 assertion)

Generating code coverage report in HTML format ... done [36 ms]

The tests will now have generated in the reports directory of your project.

Conclusion

In this article, you should have installed PHPUnit, created a class and associated test case, and run your first test case using PHPUnit. Unit testing has proven valuable in detecting a large percentage of defects during use. Unit testing allows software developers to identify all kinds of issues very early on.

Comments