📜 ⬆️ ⬇️

Combining Code Coverage from PHPUnit and phpspec

Today, this or that library on Github, which has no tests, is no longer taken seriously. Tests help us boldly do refactoring and make sure that the module, class, or function works as intended. They allow us to test our code on different versions of PHP and detect errors in advance. This is a guarantee of the quality and stability of your code.



There is no point in striving for 100% code coverage, however, understanding on average what percentage of code is covered by your tests is a good metric with continuous integration.
')
We can set alerts when a percentage of coverage drops, for example, below 50, we can add automatic comments from bots to pool requests, show the trend of Code Coverage on charts over time, etc.

image

But what if you use multiple libraries for testing? How to get general code coverage?

This is where the phpcov library comes to the rescue.

So, what will we do with each build (on the example of Travis-CI):


For example, let's take a project on Symfony3, where PHPUnit is used together with phpspec.

The PHPUnit test configuration might look like this:

phpunit.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- https://phpunit.de/manual/current/en/appendixes.configuration.html --> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="app/autoload.php" > <php> <ini name="error_reporting" value="-1" /> <server name="KERNEL_DIR" value="app/" /> </php> <testsuites> <testsuite name="Project Test Suite"> <directory>tests</directory> </testsuite> </testsuites> <filter> <whitelist> <directory>src</directory> <exclude> <directory>src/*Bundle/Resources</directory> <directory>src/*/*Bundle/Resources</directory> <directory>src/*/Bundle/*Bundle/Resources</directory> </exclude> </whitelist> </filter> <logging> <log type="coverage-php" target="/tmp/coverage_phpunit.cov"/> </logging> </phpunit> 


This is the standard config, with the exception of a few lines:

 <logging> <log type="coverage-php" target="/tmp/coverage_phpunit.cov"/> </logging> 

Here we say that the PHP report format will be used and put the file in the right folder.

Further similar actions are done with phpspec:

 # phpspec.yml ... extensions: PhpSpecCodeCoverage\CodeCoverageExtension: format: - php output: php: /tmp/coverage_phpspec.cov 

It remains to run this whole thing for each build and to have the results:

 # .travis.yml ... script: - bin/phpspec run --format=pretty - bin/phpunit - wget https://phar.phpunit.de/phpcov.phar && php phpcov.phar merge /tmp --clover coverage.xml 

phpcov collects all the data from your folder, merges it and saves the result in Clover format, which can be used to display code coverage, including such a badge

That's all. Write tests, refactor with pleasure.

UPD: badges, comments in the pool of requests and much more is done using the codecov.io service (or alternatively, coveralls.io )

After setting up the repository, integration with Travis-CI is done in one line:

 # .travis.yml ... after_success: - bash <(curl -s https://codecov.io/bash) 


Codecov will automatically take the generated report in Clover format and analyze it.

Also, when installing an add-on to the browser, you can see directly on Github which lines are covered with tests and which are not:

image
image

Source: https://habr.com/ru/post/316210/


All Articles