PHPunit学习与实践(二)
PHPUnit_Frameworkd_TestCase的断言方法
异常的测试
可以测试类的方法是否有对某些情况抛出异常
public function testSetGoods_longname() { $this->setExpectedException("Exception"); $this->Goods_obj->SetGoods("双肩包的名字不能太长"); }
上例如果在商品名称过长未抛出异常时,测试时会显示错误
2) BaoGoodsTest::testSetGoods_longname Failed asserting that exception of type "Exception" is thrown.
如果在SetGoods方法有对商品名过长作处理如下,则不会提示错误
public function SetGoods($goods_name) { if(strlen($goods_name)>10) { throw new \Exception("goods_name too long"); } $this->goods_name=$goods_name; }