[英]Test cases to test a function phpunit
我是php單元測試的新手,以下功能的有效測試用例是什么?
protected function validateParams($graph, $start, $destination)
{
if (!is_object($graph)) {
throw new \InvalidArgumentException('Graph param should be an object !');
}
if (empty($start)) {
throw new \InvalidArgumentException('Start param is empty !');
}
if (empty($destination)) {
throw new \InvalidArgumentException('Graph param is empty !');
}
return true;
}
首先,測試將正確的參數傳遞給方法時,該方法返回true
。
public function testParamsValidation();
然后,檢查任何一個參數為空時是否引發InvalidArgumentException
。 請注意,您應該有3個測試,每個參數一個。 在每個測試中,您只能傳遞一個空參數。 您可能希望使用不同的參數值(如null,false,標量等)將這些測試分別執行幾次。 為此使用dataProviders 。
public function testInvalidArgumentExceptionIsThrownWhenGraphIsNotAnObject(;
public function testInvalidArgumentExceptionIsThrownWhenStartIsEmpty();
public function testInvalidArgumentExceptionIsThrownWhenDestinationIsEmpty();
附帶說明:您可能想在方法定義中顯式顯示所需的對象類。 $ graph對象應該屬於某個類或實現某個接口?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.