PHPUnit reports different coverage of% code for the same test when it is executed in isolation

First, let me say that I went through Achieving 100% code coverage with PHPUnit SO , and How to get 100% code coverage with PHPUnit SO . I also could not solve the problem using.

My problem is that PHPUnit reports different coverage of the% code for the same test when it is executed in isolation compared to when the entire test package is launched. So when I run only a specific test using phpunit test/src/artax/UsesRequestTraitTest , I get the following coverage:

enter image description here

However, if I run the full test suite using phpunit without specifying a specific test, I get the following for the same file:

enter image description here

Here is the source code for the test:

 <?php class UsesRequestTraitTest extends PHPUnit_Framework_TestCase { public function testIsInitiallyEmpty() { $traitObj = $this->getObjectForTrait('artax\UsesRequestTrait'); $this->assertAttributeEmpty('request', $traitObj); return $traitObj; } /** * @depends testIsInitiallyEmpty * @covers artax\UsesRequestTrait::setRequest * @covers artax\UsesRequestTrait::getRequest */ public function testSetterAssignsRequestProperty($traitObj) { $r = new artax\blocks\http\HttpRequest(); $traitObj->setRequest($r); $this->assertEquals($r, $traitObj->getRequest()); } } 

If you notice, you will see that I am testing the PHP5.4 trait. I am using PHP5.4RC6 and version 2.5 xdebug for the bleeding edge version because the version of xdebug does not yet support PHP5.4.

Is it possible (probably?) That this is an error in xdebug or phpunit that has not yet been developed due to the still developing support for the new version of PHP, but I wanted to get other opinions before wasting interested parties time with an unnecessary error message. Does anyone know what might cause this or how to fix it, so my Geek OCD is not driving me crazy about this gap in 100% coverage?

PS Has an actual graphical coverage report for a coverage version of 60%. The lines shown below are green (covered) when the test is executed in isolation:

enter image description here

UPDATE

I also tried adding @covers annotations for the trait methods to tests of specific classes that use attribute, but this did not help either ...

+4
source share
1 answer

This is apparently a problem with the way the PHPUnit new getObjectForTrait reports code code coverage for the traits file. You can read about getObjectForTrait on the Sebastian Bergmann blog .

The error disappears when I use a specific implementation class to test the attribute instead of the built-in getObjectForTrait method. Thus, the following changes to the above code fix the problem:

 <?php class UsesRequestTraitTest extends PHPUnit_Framework_TestCase { public function testIsInitiallyEmpty() { // trait code coverage not reported correctly using getObjectForTrait //$traitObj = $this->getObjectForTrait('artax\UsesRequestTrait'); // use a concrete implementation instead (see bottom for class definition) $traitObj = new TraitImplementationClass(); $this->assertAttributeEmpty('request', $traitObj); return $traitObj; } /** * @depends testIsInitiallyEmpty * @covers artax\UsesRequestTrait::setRequest * @covers artax\UsesRequestTrait::getRequest */ public function testSetterAssignsRequestProperty($traitObj) { $r = new artax\blocks\http\HttpRequest(); $traitObj->setRequest($r); $this->assertEquals($r, $traitObj->getRequest()); } } // using this class to test instead of getObjectForTrait resolves the issue class TraitImplementationClass { use artax\UsesRequestTrait; } 
+3
source

Source: https://habr.com/ru/post/1394207/


All Articles