codeception - 模拟api调用数据

Ada*_*ski 11 laravel-4 codeception

我正在为前端应用程序构建和编写测试,该应用程序正在调用所有数据的API.我正在使用Codeception进行测试.到目前为止,功能和验收测试工作正常,我希望功能测试独立于API,以便我可以运行它们而不依赖于API服务应用程序.

有没有办法模拟来自API调用的数据?或者这是单元测试的领域?

Sat*_*nde 1

我使用 PHPUnit 进行 API 测试。我希望这能帮到您。

我刚刚为此测试提供了一些示例输入数据,并验证它将按预期返回错误/成功代码。如果测试没有获得预期的返回代码,那么它将提示错误。

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"=>"shindesatishsss@gmail.com");
        $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"=>"test@test.com");
        $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"]);
    }

} 
Run Code Online (Sandbox Code Playgroud)

您还可以设置一些其他测试用例,因为只需在此类中使用不同的测试用例创建新函数即可。