phpunit dataprovider超出范围

时间:2017-08-11 02:39:38

标签: phpunit

I级测试:

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());
    }



}

数据提供者中有5个元素,但测试结果显示第6个测试是错误的。

  

Sebastian Bergmann和贡献者的PHPUnit 6.3.0。

     

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

     

时间:97毫秒,内存:8.00MB

     

有1个错误:

     

1)CalculateTest :: testAddIsNormal   TypeError:传递给CalculateTest的参数1 :: testAddIsNormal()必须是一个inst       计算的ance,给定

感谢。

1 个答案:

答案 0 :(得分:0)

好的,我在github上检查了这个问题,我终于知道我无法将测试结果从数据提供者传递给另一个测试。 有它:https://github.com/sebastianbergmann/phpunit/issues/611

相关问题