Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPSpec and coverage report

Does anyone know a method to generate coverage reports from PHPSpec tests?

I thought about xdebug, but as far as I know it cant generate reports for jenkins.

like image 499
spamec Avatar asked Aug 02 '12 18:08

spamec


People also ask

What is a coverage report?

The report shows the percentage of the code that has been executed or covered by tests. You can see the coverage result for classes, methods, and lines. Branch coverage shows the percentage of the executed branches in the source code (normally, these are the if / else and switch statements).

What does the code coverage report provide?

Your code coverage tool will monitor the execution of your test suite and tell you how much of the statements, branches, functions and lines were run as part of your tests.


1 Answers

As it stands (1.4.0) it doesn't yet support code coverage. Happy to hear your feedback on this. Below is my opinion on code coverage.

PHPSpec is a BDD framework. If you were doing BDD you would describe the behaviour of your class prior to writing your class. If you did that way the relevant behaviour of your classes would all be properly covered with "tests".

Code coverage tools and metrics are useful for legacy code (code you wrote without specs/tests). You can use a tool like that to try and get up to a point where you can continue TDD and have the benefit of being protected from regression.

In general that approach isn't really as effective as describing the behaviour first (TDD). A single method may be simple enough to respond to more than one required behaviour. You know that when you are TDDing, you keep refactoring in the process, deleting unneeded code. You end up with 10 specs (tests) hitting the same lines of code, all describing different needed behaviour, all useful to understand the code.

One of the problems with the word "test" is that it makes people think TDD is about verification. It's not. It's about communication. StoryBDD is communication between stakeholders and SpecBDD is communication between classes. Simple, living, just enough documentation.

Code coverage done to guarantee you have tested your code is a fallacy, a poor metric at best. Unfortunately people think testing structure is more important than testing behaviours. That's why BDD was born, to help bring the focus back on the right track. Making sure this part of the code is tested is fake because that part of the code can do more than one thing, it should, if it's nicely refactored. Also you will end up testing stuff like accessors and modifiers and constructors, etc.

But I am open to hear the community on this. I can see where Code Coverage could be useful. Plus since Sebastian Bergmann nicely modularised it out of PHPUnit I could reuse it in PHPSpec. I would prefer you wrote your specs first. You get 100% code coverage of your relevant behaviour for free. In my views, that is what matters for the most part.

like image 116
Marcello Duarte Avatar answered Oct 18 '22 09:10

Marcello Duarte