Everytime I run unit tests of one class or a whole folder, phpunit generate coverage for the whole system, because that's configured in phpunit.xml.
This is bad because it takes longer and exhausts PHP's memory.
My phpunit.xml
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false"
bootstrap = "Bootstrap.php" >
<testsuites>
<testsuite name="Application Module Suite Test">
<directory>./Module1Test</directory>
<directory>./Module2Test</directory>
<directory>./Module3Test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>../module/Module1</directory>
<directory>../module/Module2</directory>
<directory>../module/Module3</directory>
</whitelist>
</filter>
</phpunit>
Is there a way to generate coverage of only what I'm testing right now, dynamically?
Example
For the command below I'd like to generate coverage for Controller/ExampleController.php
path only.
phpunit Controller/ExampleController.php --coverage-html ~/Desktop/tests
I'm using PHPUnit 4.8 and 3.7, Sublime Text Editor and the application is using Zend Framework 2.
How to Calculate Test Coverage. Calculating test coverage is actually fairly easy. You can simply take the number of lines that are covered by a test (any kind of test, across your whole testing strategy) and divide by the total number of lines in your application.
Test coverage defines what percentage of application code is tested and whether the test cases cover all the code. It is calculated as. For example, if you have 10,000 lines of code, your test cases should be able to test the entire codebase. If only 5,000 lines of code are tested out of 10,000, the coverage is 50%.
With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.
From the PHPUnit version 5.6 manual:
The @covers annotation (see Table B.1) can be used in the test code to specify which method(s) a test method wants to test. If provided, only the code coverage information for the specified method(s) will be considered. Example 11.2 shows an example.
See this link for an example: https://phpunit.de/manual/current/en/code-coverage-analysis.html#code-coverage-analysis.specifying-covered-methods.examples.BankAccountTest.php
Can this be useful for your scenario?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With