The Codeception framework was developed to actually make testing fun, it allows writing unit, functional, integration, and acceptance tests in a single and coherent style. It can be called a BDD (Behavior Driven Development) framework.

In this blog, I am going to share steps to setup Codeception and run basic scripts. I am using PhpStorm with Codeception, as we have very good experience with it while using it in tajawal. However, you can choose any IDE of your choice.

Now open terminal and go to the created project directory and perform below-mentioned steps:

Install Composer

To quickly install Composer in the current directory, run the following script in your terminal. To automate the installation, use the guide on installing Composer programmatically:

Install Codeception

Run below commands:

Codeception (with installed PHPUnit) can be run as:

php vendor/bin/codecept

Initialize your testing environment with the last command mentioned above.

Update composer.json File

Update composer.json file with below, as these will be required for project.

In this project, we are using selenium-appium-server. This is an open source initiative from Tajawal.com and it serves both as Selenium and Appium server.

After updating composer.json file, run the below mentioned commands:

composer install

Codeception framework has built in support for Functional, API & Unit tests, below are brief examples for each type:

“It’s automation, not automagic.” — Jim Hazen

Below is the practical implementation of writing tests:

1. Functional Test

Below is the functional.suite.yml:

functional.suite.yml

I am using Page Object Model to manage & maintain element locators with ease; as it is an object-oriented approach that serves as an interface to a page of your Application Under Test.

Page Object class will look like:

PageObjects.php

Here, how do we test:

basicTestCest.php

Run codecept build before actual execution of scripts in order to run function suite, run below command:

./vendor/codeception/codeception/codecept run functional

2. API Test

API suite is not generated by default, so, run below command to generate API test suite:

vendor/bin/codecept generate:suite api

After suite is generated, open api.suite.yml and add PhpBrowser helper and add URL, it should look like below:

api.suite.yml

API Test Script:

ListingPackagesCest.ph

Run codecept build before actual execution of scripts. In order to run API suite, run below command.

codecept run api

Test Execution

It will show you error or failure if depending on results. If you want to see what’s going on behind the scene for your test case, so you add the options -vv or -vvv for even more verbose.

codecept run api -vv

3. Unit Test

Unit tests are focused around a single component of an application. All external dependencies for components should be replaced with test doubles.

Create a test using generate:test command with a suite and test names as parameters:

php vendor/bin/codecept generate:test unit Example

A typical unit test may look like this:

ExampleTest.php

As always, you can run the newly created test with this command:

php vendor/bin/codecept run unit ExampleTest:functionName

Or simply run the whole set of unit tests with:

Run Unit Tests

I have created sample project containing Functional, API & Unit tests, find it here, don’t forget to contribute:

Reference: