Testing API codeseption

A couple years ago we had a team for testing applications, but today every webdevelopment has his own test scripts that make sure that codes are working!

I did a lot of tests and today I found codeseption. So I'm going to test it with codeception.

On their quick start http://codeception.com/quickstart it is installed with composer, lets do that:

composer require "codeception/codeception"

to use it from commanline do next:

alias codecept='./vendor/bin/codecept'

now I can call next:

codecept bootstrap

to create my basic structure.

codecept generate:cept acceptance Welcome

that will create welcome file in /tests/acceptence/ dir.

Because this is REST API test lets look at: http://codeception.com/docs/modules/REST
there is say modify acceptance.suite.yml file like this:

modules:
   enabled:
       - REST:
           depends: PhpBrowser
           url: 'http://serviceapp/api/v1/'

and then we can test our app to see if it returns json header like this:

<?php
  $I = new AcceptanceTester($scenario);
  $I->wantTo('ensure that it returns json header');
  $I->haveHttpHeader('Content-Type', 'application/json');
?>

if all set up correctly in command line output will be displayed if failed or passed the test. At my case all worked well and passed the test.
Of course I have to test if it gives me json data of all diseases and that can be done like this:

<?php
  $I = new AcceptanceTester($scenario);
  $I->sendGET('/diseases');
  $I->wantTo('ensure that page is loaded');
  $I->seeResponseIsJson();
  $I->wantTo('ensure that it returns json header');
  $I->haveHttpHeader('Content-Type', 'application/json');

  $I->wantTo('ensure that returns json');
  $I->seeResponseMatchesJsonType(
    ['meta' => [
            'total-results' => 'integer|string'
                ],

    'data' => [
          'diseases' => [
                          [
                            'id' => 'integer|string',
                            'name' => 'string'
                          ]
                        ]
              ],
    'links' => [
            'self' => 'string'
      ]
    ]
    );
?>

Now I the JSON that is returned is tested on next things:

  • It loads our controller
  • There is valid JSON response
  • That header is JSON
  • That responding JSON is format that we are expecting

Lets test respond on individual diseases response, I will take id of disease 44.

$I->sendGET('/diseases/44');
  $I->wantTo('ensure that page is loaded');
  $I->seeResponseIsJson();
  $I->wantTo('ensure that it returns json header');
  $I->haveHttpHeader('Content-Type', 'application/json');
  $I->wantTo('ensure that returns json structure');
  $I->seeResponseMatchesJsonType(
    ['meta' => "string|null",

    'data' => [
          'diseases' => [

                            'id' => 'integer|string',
                            'name' => 'string',
                          'frequencies' => [
                              'string|intiger'
                            ],
                        ]
              ],
    'links' => [
            'self' => 'string'
      ]
    ]
    );

What ever I builded it has been tested on repond of API. Lets develop authentication and other PUT, DELETE and UPDATE and test it all :)

results matching ""

    No results matching ""