PHPSpec and coverage report PHPSpec and coverage report jenkins jenkins

PHPSpec and coverage report


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.


Use the Code Coverage extension to PHPSpec to generate clover.

If you would like to merge coverage data from PHPSpec and other tools (eg) PHPUnit, then use the PHP_CodeCoverage output format with the phpcov tool in merge mode.

Examples:

# phpspec.ymlextensions:    - PhpSpec\Extension\CodeCoverageExtensioncode_coverage:    output: /tmp/coverage/phpspec.phpcoverage    format: php# phpunit.xml<logging>   <log type="coverage-php" target="/tmp/coverage/phpunit.phpcoverage" /></logging># from the command linephpcov merge --clover coverage.xml /tmp/coverage

That will give you coverage from both tools in a final clover format, suitable for like Jenkins.


I think a code-coverage generator would be usefull for legacy systems that is in the process of being subjected to BDD style testing as you will be able to tell what code is not tested. I for one would appreciate such a feature in PHPSpec.