Unit Testing with Php from PhpUnit to Coverage Report
Unit Testing with PHP from PHP Unit to Coverage Report
If you want to be confident each time you add a new functionality in your code, or when you need to change some old code of your application unit tests are a necessity. In this article, I will not try to convince the few (I hope) developers reticent with unit testing but rather showing you how to implement it without hustles.
1. Installing our tools
- PhpUnit : it will run all our tests
On a Symfony project simply run
composer require — dev symfony/phpunit-bridge
- Xdebug : it will run our tests and generate a coverage report on our code.
2. Running our tests
phpunit.xml file at the root of your project :
<phpunit colors="true"> <testsuites> <testsuite name="Manager Test Suite"> <directory>./Tests</directory> </testsuite> </testsuites> </phpunit>
In order to get color highlight in our terminal we can configure our
phpunit.xml with the
Get the results
- Run all our test :
- Running only a directory :
- Running only one test :
./bin/phpunit Tests/AmountTest.php --filter=testTotalAmount
Result of our tests
3. Code Coverage
Now that we test our codebase and see which tests are failing, we can go further. Once all our tests are green we need to know what we should test next. Or which tests are too weak.
phpunit.coverage.xmlfile at the root of your project :
<phpunit colors="true"> <testsuites> <testsuite name="Manager test suites"> <directory>./Tests/Manager</directory> </testsuite> </testsuites> <logging> <log type="coverage-html" target="./log/codeCoverage" lowUpperBound="40" highLowerBound="75" showUncoveredFiles="false"/> </logging> <filter> <!-- files with at least one line executed appears in report--> <whitelist addUncoveredFilesFromWhitelist="false"> <directory suffix=".php">./Manager</directory> </whitelist> </filter> </phpunit>
You can now change phpunit options as you want :
: which tests will be executed
: configure code coverage info.
- lowUpperBound which percentage code should be covered to be regarded as low
- highUpperBound : minimum percentage for high coverage
: specify which files will be included in the code coverage report
: specify which files are being included using following options :
addUncoveredFilesFromWhitelist="false"to only show files executed by the test in the report
processUncoveredFilesFromWhitelist="false"all files are added to the report
Run this to see the generated coverage report :
/.bin/phpunit -c phpunit.coverage.xml
Using the previous configuration, this command will run our tests again but thanks to Xdebug it will generate an html report.
The html coverage report can now be found at the
target location configured in our
phpunit.coverage.xml , in our case :