PHPUnit 9 以及之前測試是否有 throw exception 的方法,是在 test case 前加上 annotation:
/**
* @expectedException \Exception
*/
public function testFailed() { }
在 PHPUnit 10 則直接在 test case 中呼叫 assertion 即可:
public function testFailed()
{
$this->expectException(\Exception::class);
}
但要注意,必須在測試開始之前。若 exception 比 expectedException() 還要早出現仍會視為 test failed。