To generate and view PHPUnit test reports and code coverage reports, you can follow these steps:
Step 1: Install PHPUnit Make sure you have PHPUnit installed on your system. You can do this by running the following command:
composer require --dev phpunit/phpunit
This will install PHPUnit as a development dependency in your project.
Step 2: Configure PHPUnit Create a phpunit.xml file in the root directory of your project. This file will contain the configuration for PHPUnit. Here's a sample configuration:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<!-- Add your test suite(s) here -->
<!-- Configuration for code coverage -->
<logging>
<log type="coverage-html" target="coverage-report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
</logging>
</phpunit>
This configuration enables code coverage and specifies that the coverage report should be generated in the coverage-report
directory.
Step 3: Write PHPUnit Tests
Write your PHPUnit tests in separate files using the naming convention *Test.php
. For example, MyClassTest.php
would contain tests for the MyClass
class.
Step 4: Run PHPUnit To generate the test reports and code coverage reports, run the following command from the command line in the root directory of your project:
./vendor/bin/phpunit
This will execute all the PHPUnit tests and generate the reports.
Step 5: View the Reports
After running PHPUnit, you can view the test reports and code coverage reports in the specified directories. For example, if you configured the coverage report directory as coverage-report
, you can open the index.html
file in that directory to view the code coverage report in your browser.
That's it! You have now generated and viewed PHPUnit test reports and code coverage reports for your project.