Testing Restful API with codeception (phpbrowser module)

Submitted by Mahmoud_Zalt - 9 years ago

example of how to test API with Codeception and Laravel

1. create an API suite
codecept generate:suite api
or in your functional suite you can continue

2. update the api.suite.yml file to include the necessary modules and add their configs 

class_name: ApiTester
modules:
    enabled:
        - PhpBrowser
        - REST
        - ApiHelper
        - Laravel4
        - Asserts
    config:
      PhpBrowser:
        url: http://zzzz.api.xxxx.dev:8008/
    REST:
        url: http://zzzz.api.xxxx.dev:8008/v1/

3. create first test
codecept generate:cest api MyFirstTest

4. write your code, here is a sample

$I = new ApiTester($scenario);
$I->wantTo('create a user via API');
$I->amHttpAuthenticated('service_user', '123456');
$I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded');
$I->sendPOST('users', ['name' => 'mega', 'email' => 'mega@email.com']);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContains('{"result":"ok"}');