Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate coverage only of what was just tested

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.

like image 516
Edson Horacio Junior Avatar asked Oct 27 '16 17:10

Edson Horacio Junior


People also ask

How do you calculate test coverage?

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.

What is meant by test coverage?

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%.

What is good test coverage?

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.


1 Answers

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?

like image 87
Tudor Ilisoi Avatar answered Oct 03 '22 01:10

Tudor Ilisoi