PHPUnit严格模式 - setUp() - 覆盖范围

Spa*_*cus 14 php phpunit

strict当我遇到代码覆盖问题时,我目前开始在PHPUnit中使用-Mode:

如果我使用setUp-method创建我的类的新实例,__constructor则在运行测试时,代码覆盖中会列出-method.

这是我的测试设置:

phpunit.config.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
    bootstrap="../vendor/autoload.php"
    backupGlobals="false"
    backupStaticAttributes="false"
    colors="true"
    verbose="true"    
    beStrictAboutOutputDuringTests="true"
    beStrictAboutTestSize="true"
    beStrictAboutTestsThatDoNotTestAnything="true"
    beStrictAboutTodoAnnotatedTests="true"

    checkForUnintentionallyCoveredCode="true"
    processIsolation="false"
>
<testsuites>
    <testsuite name="FooTests">
        <directory suffix="Test.php">../tests</directory>
    </testsuite>
</testsuites>
<filter>
    <whitelist>
        <directory suffix=".php">../src</directory>
    </whitelist>
</filter>
<logging>
    <log type="coverage-html" target="coverage/" higlight="true" showUncoveredFiles="true"></log>    
</logging>
Run Code Online (Sandbox Code Playgroud)

Foo.php

class Foo
{

    protected $_bar;

    public function __construct($bar)
    {
        $this->_bar=$bar;             //Line 10
    }                                 //Line 11

    public function getBar()
    {
        return $this->_bar;
    }

    public function getBar2()
    {
        return $this->_bar;
    }

}
Run Code Online (Sandbox Code Playgroud)

和测试:FooTest.php

class FooTest extends \PHPUnit_Framework_TestCase
{

    protected $_foo;

    protected function setUp()
    {
        $this->_foo=new Foo(10);
    }

    public function testGetBar()
    {
        $this->assertSame(10, $this->_foo->getBar());
    }

    /**
     * @covers Foo::getBar2
     */
    public function testGetBar2()
    {
        $this->assertSame(10, $this->_foo->getBar2());
    }

}
Run Code Online (Sandbox Code Playgroud)

如果我运行测试,我得到这个结果:

PHPUnit 4.5.0 by Sebastian Bergmann and contributors.

Configuration read from C:\xampp\htdocs\unittest\build\phpunit.config.xml

.R

Time: 88 ms, Memory: 3.50Mb

There was 1 risky test:
1) FooTest::testGetBar2
This test executed code that is not listed as code to be covered or used:
- C:\xampp\htdocs\unittest\src\Foo.php:10
- C:\xampp\htdocs\unittest\src\Foo.php:11

OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 2, Risky: 1.

Generating code coverage report in HTML format ... done
Run Code Online (Sandbox Code Playgroud)

一旦我指定@covers测试,问题就出现了.

这是预期的行为吗?

我试过的一些事情:

  • 更改checkForUnintentionallyCoveredCodefalse明显的作品,但我想用这个功能...
  • 也使用processIsolation="true"作品.我不知道为什么?
  • 添加@covers@usessetUp()不工作
  • 添加测试@coverssetUp()用途确实有效,但测试实际上并不涵盖代码.(如果测试变得更复杂,这似乎是写了很多......)
  • 不同的 - phpunit版本:我尝试过这个4.3并且4.5结果相同
  • 不同的PHP设置:我在带有XAMPP和LinuxMint的Win8上试过这个 - 结果相同

有没有办法setUp()从代码覆盖中删除代码并使用@covers他们实际测试的方法进行测试?

编辑:这也会影响继承.因此,如果Bar延伸Foo,并到传递参数Foo::__construct,这将是对代码覆盖率太-这使得编写@covers用于__construct在一个痛**...

附加信息:

PHP 5.6.3 (cli) (built: Nov 12 2014 17:18:08)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
Run Code Online (Sandbox Code Playgroud)

Spa*_*cus 5

自从这个问题开始得到发展以来,这就是我的解决方案。

我的-test()单元将始终使用,因此我将其添加到类中。FooTestFoo Foo@uses Foo

如果函数使用protected/ private函数,这也很重要public,因为如果类在内部使用函数,则必须将每个protected/ private函数添加到测试中。我什至认为如果进行单元测试是错误的,因为单元测试必须不在乎类是如何“填充”的,它只能断言特定的输入会导致特定的输出。

(此外:构造函数只应执行赋值,而不能执行其他任何操作。)

添加@uses错误后将消失。

(您可以添加@covers Foo::_construct到该类中,以具有构造函数的代码覆盖率。)

/**
 * @uses Foo 
 * (optional)@covers Foo::__construct
 */
class FooTest extends \PHPUnit_Framework_TestCase
{
    protected $_foo;

    protected function setUp()
    {
        $this->_foo=new Foo(10);
    }

    public function testGetBar()
    {
        $this->assertSame(10, $this->_foo->getBar());
    }

    /**
     * @covers Foo::getBar2
     */
    public function testGetBar2()
    {
        $this->assertSame(10, $this->_foo->getBar2());
    }
}
Run Code Online (Sandbox Code Playgroud)