Tôi đã sử dụng PHPUnit để kiểm tra API. Tôi hy vọng điều này sẽ giúp bạn.
Tôi vừa cung cấp một số dữ liệu đầu vào mẫu cho thử nghiệm này và xác thực rằng nó sẽ trả về mã lỗi/thành công như mong đợi Nếu thử nghiệm không có mã trả về dự kiến, thì nó sẽ nhắc lỗi.
class ForgotPasswordTest extends \TestCase{
/**
* Test Forgot Password API With valid parameters
* It will check forgot password functionality with success conditions
*/
public function testForgotPasswordWithValidParameter()
{
$param=array("email"=>"[email protected]");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 600 in case if API executed successfully
*/
$this->assertEquals("600", $data["code"]);
}
/**
* Test Forgot Password API With Invalid parameters
* It will check whether you have applied user exist condition
*/
public function testForgotPasswordWithInValidParameter()
{
$param=array("email"=>"[email protected]");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 404 if user not found
*/
$this->assertEquals("404", $data["code"]);
}
/**
* Test Forgot Password API With Invalid parameters
* It will check input validations are working fine in the API
*/
public function testForgotPasswordWithInValidEmail()
{
$param=array("email"=>"satish");
$response = $this->call('POST', 'forgotPassword',$param);
$data = json_decode($response->getContent(), true);
if(!isset($data["code"]))
$this->assertFalse(false);
/** check response code
* Return 400 error code if there are some input validation error
*/
$this->assertEquals("400", $data["code"]);
}
}
Bạn cũng có thể đặt một số trường hợp thử nghiệm khác, chỉ cần tạo hàm mới trong lớp này với các trường hợp thử nghiệm khác nhau.
Nguồn
2015-03-17 09:15:38
Điều này có thể phụ thuộc vào thư viện REST bạn sử dụng để thực hiện các cuộc gọi API đó. Nếu không, một giải pháp độc lập về khung thư viện và thư viện sẽ là tạo ra một quy trình máy chủ nhẹ để mô phỏng các phản hồi API và sử dụng máy chủ đó thay vì máy chủ API thực khi thử nghiệm. –
Hãy xem [apiary.io] (http://apiary.io/) Nó có thể được sử dụng để thiết kế và chế nhạo một API – lukasgeiter