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
謝謝。
你可以在這樣的行爲,請討論[發生在github上(https://github.com/sebastianbergmann/phpunit/issues/183#issuecomment-816066) – xmike
謝謝,我終於得到了從github回答,感謝它。 – HandsomeWong
建議:不要讓測試依賴於其他測試。它會降低易讀性和維護性,除了您不能運行單個測試之外,而是最終運行多個測試。 – localheinz