使用phpunit测试多个异常

whe*_*hys 2 phpunit unit-testing exception

我是单元测试的新手,并编写了以下测试:

/**
 * @expectedException Exception
 */
public function testCantGetInvalidCampsite() {
    $invalidIds = array(300000, "string");
    foreach($invalidIds as $id) {
        $this->campsites->getCampsite($id); // will throw an exception
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定这是否实际测试了所有无效的id,或者只是在它遇到第一个异常时就停止了.这是我应该如何测试多个异常,还是我需要将其拆分为多个不同的测试,还是我应该采用另一种方式?

此外,如果我的异常消息是动态生成的,例如"无法检索ID为30000的记录",如何测试是否正在生成正确的动态消息?

Nic*_*ini 10

我在这种情况下使用的方法是使用phpunit dataProviders:

class MyTest extends PHPUnit_Framework_TestCase
{
    public function invalidIds()
    {
       return array(
           array(300000),
           array("string")
       );
    }


    /**
     * @dataProvider invalidIds
     * @expectedException Exception
     */
    public function testCantGetInvalidCampsite($invalidId)
    {
        $this->campsites->getCampsite($invalidId); // will throw an exception
    }
}
Run Code Online (Sandbox Code Playgroud)