Search code examples
symfonyphpunitcode-coverage

why codecoverage want me to cover define variable?


Good evening, I am creating a bundle for symfony. I created unit tests for my methods and I generate a test coverage report (via travis ci then sent to codecov.io). I still have a part of code that is not covered, namely constants (define):

codecov.io

I tried to add code coverage ignore instructions or add getter and test the return of these getters but the results are always the same. I don't understand why the report requires me to test constant variables which in principle do not have to be tested. Thank you in advance for your help


Solution

  • @codeCoverageIgnore can be used on classes, methods and a single statement. If you want to exclude a block, use @codeCoverageIgnoreStart and @codeCoverageIgnoreEnd:

    // @codeCoverageIgnoreStart
    define('A', 'B');
    define('C', 'D');
    // @codeCoverageIgnoreEnd
    

    Alternatively, flag each statement as ignored:

    define('A', 'B'); // @codeCoverageIgnore
    define('C', 'D'); // @codeCoverageIgnore
    

    The code coverage report doesn't know whether you have to test something or not. It simply reports what code has been executed while running the tests. You should take it as a guide to find spots you've missed. Just because some lines are red doesn't mean you have to test them. And just because lines are green doesn't mean that you have covered all relevant cases.