josef.van.niekerk
josef.van.niekerk

Reputation: 12121

Excluding a PHP Interface from PHPUnit code coverage

I've got a PHPUnit test that tests a class called HelpTokenizerTest. This class implements TokenizerInterface. For some weird reason I cannot exclude the TokenizerInterface from code coverage.

It shows up in code coverage reports as not covered, despite using @codeCoverageIgnore or even @codeCoverageIgnoreStart/End.

Any ideas?

I don't want the interface included in my test coverage, as it doesn't do anything. What's the point of testing an Interface.

Upvotes: 4

Views: 3279

Answers (2)

Samuel Herzog
Samuel Herzog

Reputation: 3611

When using a phpunit.xml you can set up filters to exclude files with particular names, in particular folders or with an particular extension.

see the documentation for it

Example:

<testsuite name="Application Test Suite">
    <directory>./application/</directory>
</testsuite>

<filter>
    <blacklist>
        HERE
    </blacklist>
       or alternatively
    <whitelist>
        <directory suffix=".php">../library/</directory>
        <directory suffix=".php">../application/</directory>
        <exclude>
            AND HERE
            <directory suffix=".phtml">../application/</directory>
        </exclude>
    </whitelist>
</filter>

Upvotes: 3

Darren Gordon
Darren Gordon

Reputation: 729

You can use @codeCoverageIgnore as a comment at the beginning of the file to get 100% 0/0 coverage.

<?php // @codeCoverageIgnoreStart 
interface MyInterface
{
    public function myfunction();
}

Note that it doesn't work in a comment block.

Issue open on PHPUnit's github: https://github.com/sebastianbergmann/phpunit/issues/497

Upvotes: 2

Related Questions