- API Testing : Why to write Test Cases and use Automated Testing Tools?
- API Testing: Selecting Testing Framework ( PHP Unit vs Codeception vs Behat )
- API Testing: Installing and Using Codeception
In this post, we will have a look on how we can use Codeception for API testing. First we need to install codeception.
Installation:
Here is Codeception’s quick start guide that show multiple ways to install codeception. Here we will be installing through Composer, you can follow any other method as you like.
First go to directory where you want to install Codeception and place your tests. In my case that directory name is “app1”.
composer require "codeception/codeception:*"
or
php composer.phar require "codeception/codeception:*"
It will take some time, and fetch codeception. Once it is done, you will be able to use codeception command line tool. Try running codeception following command to make sure that you have codeception installed.
vendor/bin/codecept --version
Setup Codeception:
Here is something you need to do to setup your codeception testing environment. Run following command from root of your app directory.
vendor/bin/codecept bootstrap
It will create ‘tests’ folder at root of your app directory. This is where all your tests files and information will live. This tests directory will have many directories including acceptance, functional and unit . These directories are related to different types of tests suites. We need to have api test suite, that isn’t yet created here. So let’s create that now.
vendor/bin/codecept generate:suite api
ok now in tests directory it will have a new directory named “
api” . That will have Api Test suites in it. Ok, before moving further, let’s first get rid of this long path “vendor/bin/codecept” so that we can just use “codecept” command instead of whole path.
So if you are on linux, ubuntu you would probably be interesting in installing codeception globally. So that you can simply use “codecept” command from anywhere. Here is how you can do this: http://stdout.in/en/post/install_codeception_globally_in_ubuntu
If you are on windows then you can simply add it in your system path.
Ok once done that you can now to use codecept command you can simply say “codecept” and it will work. Simply try following command from anywhere.
codecept --version
Now you are ready for next things but let’s first understand directory structure of codeception.
Codeception Directory structure:
If you go in “tests” directory which is created as a result of “codecept bootstrap” command. There you will see following directory structure.
- _data
This directory can have DB file if you need to use that. - _output
This directory contains output of tests in case of failure. - _support
This directory can have helpers if you write that to support your tests. - acceptance
This directory is useful if you need to write acceptance tests. - api
This directory is useful if you are writing api tests. This directory is not present by default but it will be created as result of api suite generation command. - functional
This directory is useful if you need to write functional tests. - unit
This directory is useful if you need to write unit tests. - _bootstrap.php
This file is useful for autoloading any file that you want to include. - acceptance.suite.yml
This file contain configuration of acceptance suite. - api.suite.yml
This file contain configuration of api suite. - functional.suite.yml
This file contain configuration of functional suite. - unit.suite.yml
This file contain configuration of unit suite.
Also in api directory there is _bootstrap.php . It is useful if you want to include and autoload any file inside api suite only.
API test suite configuration:
Open api.suite.yml and add PhpBrowser helper and add URL, it should look like something like this:
class_name: ApiTester modules: enabled: [ApiHelper,PhpBrowser, REST] config: PhpBrowser: url: http://localhost/app
Note: Above code snipper for yml file contains 4 spaces per tab and not tab character.
Laravel or PhpBrowser module :
If you are using Laravel, then you can add Laravel module instead of PhpBrowser. So that in case of error, if you are using Laravel module then error message will be more debug-able than a 500 error that can be seen on browser screen too. I was previously using Laravel module and error was more clear with better insights but problem is that Laravel module is much slower than PhpBrowser module so I switched to PhpBrowser. However if you have good RAM and not yet concerned about speed of execution of your test cases, specially if they are less than 50, then using Laravel module is better option. So it all depends on you.
Writing Test Case:
There are two ways to write test cases based on two types of files. Cept and Cest. Cept is a scenario-based format and Cest is a class based format. Actually it depends on how you are comfortable writing code for test cases. In strucute way or using classes. I prefer and recommended class based format because it will give you OOP power for writing more test cases with less code in long run.
So generate your first Cest file by this command:
codecept generate:cest api CreateUser
Now open that file and put some code in that as below.
wantTo('create a user via API'); $I->amHttpAuthenticated('service_user', '123456'); $I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded'); $I->sendPOST('users', ['name' => 'davert', 'email' => 'davert@codeception.com']); $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->;seeResponseContains('{"result":"ok"}'); } }
Here $I contains the object of ApiTester that is respresting a user state who is testing a scenario.
Running Test Case:
If you are running testcase first time in api suite, then in your api directory you will not have api tester file. You need to generate that so run following command:
codecept build
Then run
codecept run api
It will show you error or failure if at mentioned URL you will have not have intended path or functionality working. If you want to see what’s going on behind the scene for your test case then use -vv or -vvv for even more verbose.
codecept run api -vv
To know more about Codeception, simply check codeception documentation: http://codeception.com/docs/10-WebServices
However, if you want to know more about Automated testing, better ways of testing, why and how to write tests. Then I recommend reading Laravel Testing Decoded by Jeffrey Way. And if you are a PHP guy then I even strongly recommend reading this book because you will most probably be lacking something that is in this book.
You’re probably familiar with Jeffrey’s Laracast, this book is even better than that to understand Testing and follow better approaches to both development and testing.
This seems to be enough to let me create and delete models for Unit test, but not the functional API tests
There are definitely a lot of details like that to take into consideration. That could be a nice point to deliver up.
[RuntimeException] Call to undefined method ApiTester::sendPOST
Why i am gettting this error?