Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHPUnit code coverage with interfaces

I'm using PHPUnit (3.6.7) to test and provide code coverage reports on my application, everything is set-up and working as expected.

I have complete coverage for all of the code except for my interfaces, even though I have tests that for classes that implement the interfaces. The report just states that the interface was not executed

Is there a way to cover the interfaces? Or is it a case of telling PHPUnit to ignore them for code coverage?

like image 389
Paul Dixon Avatar asked Jan 16 '12 14:01

Paul Dixon


People also ask

How do you exclude an interface from code coverage?

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.

What is code coverage in Phpunit?

Code Coverage Analysis. Wikipedia: In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite.


3 Answers

You can specify that tests for a concrete class cover methods from parent abstract classes/interfaces.

See Specifying Covered Methods section in Code Coverage Analysis chapter in the manual.

In the same chapter you'll also find ways to ignore blocks of code or entire files from code coverage analysis.

like image 152
Mchl Avatar answered Oct 14 '22 03:10

Mchl


Just as an additional answer:

The next release of PHPUnit (3.7.) will ignore all interfaces for coverage by default.

So it will not necessary to use any sort of includes or //@codeCoverageIgnore ways to work around then.

like image 21
edorian Avatar answered Oct 14 '22 01:10

edorian


Interfaces contain no executable code, so there's nothing there to test.

like image 43
GordonM Avatar answered Oct 14 '22 01:10

GordonM