This is a brief step by step guide to install CodeCeption on your local machine and to create a basic but useful test for a fictional ‘Categories’ API endpoint.
Codeception is a Testing framework that you can use for creating Unit Tests, Acceptance tests and Web Services/API tests too. For more information, http://codeception.com/
The test created below makes sure an API endpoint such as http://api.domain.com/categories is online and responding as expected. It tests for a HTTP 200 code in the response, that the response is in a JSON format and that the response contains ‘success’.
This same test could be recreated for many other endpoints in your API. Also, If you store your code using Github or Bitbucket, you could use a service such as http://codeship.io or https://circleci.com/ to run those tests automatically any time you commit a change to your code to let you know that all of your API endpoints are online and responding as expected and only deploy changes Live if all your tests pass.
- Download codecept.phar from http://codeception.com/ and place it in the root of your project
- In the terminal, in the root folder, write ‘php codecept.phar bootstrap’, this creates the Tests folder and other bootstrap related files
- In the terminal, write ‘php codecept.phar generate:suite api ApiGuy’, this creates the /test/api folder where we will keep our API tests.
- Update the existing file called /tests/api.suite.yml with the code written below to give CodeCeption some basic information about our API, replacing ‘domain.com’ with your own URLs.
- In the terminal, write ‘php codecept.phar build’ to build the classes that we will need
- In the terminal, wite ‘php codecept.phar generate:cept api Categories’ to create a ‘Categories’ test in /tests/api/CategoriesCept.php
- Update CategoriesCept.php with the code below to tell it what to test.
- Finally, In the terminal, write ‘php codecept.phar run’ to run your tests!
enabled: [PhpBrowser, REST, ApiHelper]
$I = new ApiGuy($scenario);
$I->wantTo(‘Return all categories’);