Ho usato PHPUnit per il test dell'API. Spero che questo ti possa aiutare.
ho appena fornito alcuni dati di input di esempio per questo test e validare che restituisca il codice di errore/successo come previsto Se il test non ha ottenuto il codice di rendimento atteso, allora vi verrà chiesto un errore.
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"]);
}
}
È possibile impostare alcuni altri casi di test anche, per quel solo bisogno di creare nuova funzione in questa classe con diversi casi di test.
fonte
2015-03-17 09:15:38
Ciò dipenderà probabilmente dalla libreria REST che si utilizza per effettuare tali chiamate API. In caso contrario, una soluzione agnostica per framework e libreria sarebbe quella di creare un processo server leggero che deride le risposte API e utilizzi quel server invece del vero server API durante il test. –
Dai un'occhiata a [apiary.io] (http://apiary.io/) Può essere usato per progettare e simulare un'API – lukasgeiter