2017-08-11 44 views
0

类我测试:PHPUnit的数据提供程序超出范围

class Calculate { 
    public $x; 
    public $y; 

    public function __construct($x, $y) { 
     $this->x = $x; 
     $this->y = $y; 
    } 

    public function add(): int { 
     return $this->x + $this->y; 
    } 
} 

我的测试代码:

use PHPUnit\Framework\TestCase; 

class CalculateTest extends TestCase { 

    public function additionProvider() { 
     return [ 
      [1, 2], 
      [5, 8], 
      [-1, 10], 
      [66, 3], 
      [9, 4] 
     ]; 
    } 

    /** 
    * @dataProvider additionProvider 
    */ 
    public function testGetIsOk($x, $y): Calculate { 
     $c = new Calculate($x, $y); 
     var_dump($c); 
     $this->assertEquals($x, $c->x); 
     $this->assertEquals($y, $c->y); 

     return $c; 
    } 

    /** 
    * @depends testGetIsOk 
    */ 
    public function testAddIsNormal(Calculate $c):void { 
     $this->assertEquals($c->x + $c->y, $c->add()); 
    } 



} 

有dataProvider中的5个元素,但测试结果表明,6号测试是错误的。

PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

.....E 6/6 (100%)

Time: 97 ms, Memory: 8.00MB

There was 1 error:

1) CalculateTest::testAddIsNormal TypeError: Argument 1 passed to CalculateTest::testAddIsNormal() must be an inst ance of Calculate, null given

谢谢。

+0

你可以在这样的行为,请讨论[发生在github上(https://github.com/sebastianbergmann/phpunit/issues/183#issuecomment-816066) – xmike

+0

谢谢,我终于得到了从github回答,感谢它。 – HandsomeWong

+0

建议:不要让测试依赖于其他测试。它会降低易读性和维护性,除了您不能运行单个测试之外,而是最终运行多个测试。 – localheinz

回答