Usage of @covers annotation in unit test leads to "risky test"

If checkForUnintentionallyCoveredCode is set to true in the PHPUnit configuration file (which is the case in typo3/buildessentials's UnitTests.xml), unit test like the following one are [1] marked as “risky” by PHPUnit and [2] therefore will not increase code coverage:

/**
 * @test
 * @covers \Foo\Bar\Domain\Model\Baz::getBam
 * @return void
 */
public function canReturnBam() {
	$baz = new \Foo\Bar\Domain\Model\Baz();
	$this->assertEquals('', $baz->getBam());
}

If I omit the @covers annotation the tests are not marked as “risky” anymore and will increase code coverage.

What is the problem with this test?

Related (but does not solve the problem): http://stackoverflow.com/questions/28602022/phpunit-strict-mode-setup-coverage

I couldn’t reproduce this with Flow and the provided bootstrap:

<?php
namespace Wwwision\Test;

class SomeClass {

	public function someMethod() {
		return 'foo';
	}
}

<?php
namespace Wwwision\Test\Tests\Unit;

use TYPO3\Flow\Tests\UnitTestCase;
use Wwwision\Test\SomeClass;

class TypoScriptViewTest extends UnitTestCase {

	/**
	 * @test
	 * @covers \Wwwision\Test\SomeClass::someMethod
	 */
	public function foo() {
		$class = new SomeClass();
		$this->assertSame('foo', $class->someMethod());
	}

}

phpunit --colors -c Build/buildessentials/PhpUnit/UnitTests.xml Packages/Application/Wwwision.Test/Tests/Unit
PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

Configuration read from Build/buildessentials/PhpUnit/UnitTests.xml

.

Time: 40 ms, Memory: 4.00Mb

OK (1 test, 1 assertion)

You need to add the configuration for coverage logging to the PHPUnit configuration file UnitTests.xml to be able to reproduce it.

<logging>
    <log type="coverage-html" target="../../Reports/Coverage"/>
</logging>

…and Xdebug must be enabled so that code coverage can generally be calculated.

Still works:

PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

Configuration read from Build/buildessentials/PhpUnit/UnitTests.xml
.
Time: 1.09 seconds, Memory: 15.25Mb

OK (1 test, 1 assertion)
Generating code coverage report in HTML format ... done

…if I change the @covers annotation to a non-existing class/method I get

Trying to @cover or @use not existing method "\Wwwision\Test\NonExistingClass::someMethod".

Process finished with exit code 2

Sorry for my late answer - I had no idea what could be the difference between our setups.

Can you please try it again with PHPUnit 4.8.6?

I guess the following was the problem: my example was oversimplyfied (but not tested by itself). Sorry for that!

As far as I can recall a call of a __construct() method was most probably involved which seems to lead to “unintentionally covered code”. I added a @codeCoverageIgnore annotation to the __construct(). The test is not marked risky any longer.

I am not sure if using a @codeCoverageIgnore is a good practice here. I already tried various things using the @uses and @coversannotations but couldn’t come with a better solution.