Introduction to Codeception

Zeeshan Bashir
tajawal
Published in
5 min readApr 24, 2018
https://codeception.com/

Why Testing Is Important?

Writing test case is like doing exercise, it is painful but we all know exercise can improve our health, mostly developers never try to write tests because they find it boring or time wasting. Some of them who try, they don’t cover the alternative scenarios or will write basic tests.

So it is important to write tests for your code because until you don’t execute your line of code you don’t know if that line can work at all. You have to write some basic set of tests to check your code.

Tests are important for specially large scale application as we all know details can be changed so there should be way which can tell us we are in safe hands.

What Testing Can Do?

  • In refactoring it provides fast automated way to test your code.
  • Unit tests prove that your code actually works.
  • If you can’t write test for something, it means you don’t understand it.
  • If you or other developer modify the code, there is a chance to break the expected behavior so we can overcome this issue by running unit tests.
  • Spending a small amount of time to write the tests can save hours of future debugging.

No amount of testing can prove a software right, a single test can prove a software wrong ~Amir Ghahrai

  • We can handle unexpected input scenarios.
  • Stress test to make sure application can handle high loads.
  • Bugs detected earlier are easier to fix, bugs detected later are usually the result of many changes, and we don’t know which one caused the bug.

What Testing Can’t Do?

  • Tests are great to support maintainable application but it can’t verify the application end to end behavior.
  • Tests don’t improve quality, actually developers do.

What is Codeception?

Codeception is a framework written in PHP for testing application and its multi-featured test framework powered by famous PHPUnit Framework.

~ Elegant and Efficient Testing for PHP ~

It can manage Unit, Functional and Acceptance for web application, let us discuss about Codeception suites.

Suites of Codeception

The main advantage of Codeception is that we do not have to work only one type of testing, Codeception comes up with an idea of suites (collection), Codeception consists of three suites by default which are following:

  1. Unit Test.
  2. Acceptance Test.
  3. Functional Test.

Optionally, you can add any other suite of your choice, so here, we will add API suite.

4. API Test.

1. Unit Tests

Testing a smallest unit of functionality, typically function/method. Unit tests should be focused on testing a particular feature, e.g, testing pop method when stack is empty should throw exception.

We can write many test function for a single method which can handle validation, throw exception and success scenario. So Unit test helps us to improve our code quality and save us from surprises.

Unit class has predefined __before and _after functions, you can use them to created object before each test and destroy afterward.

In above example, we have created a class User and made function to validate length of password, if length is less then 4 character return false otherwise return true.

Then we created out unit test for password class, in this test class, we have two test functions so we can test every line of validLength from password class.

2. Acceptance Test

Acceptance test allows us to test our application just like visiting browser, filling form and submit but the only difference is that we don’t need to open browser to repeat this step, thanks to acceptance test, this testing is done on client side. It is usually called User Acceptance Test, any non-technical can performed this testing.

We can perform this testing by using phpBrowser or real browser by using web selenium driver. But we also have some drawbacks for phpBrowser:

  • You click only valid links
  • You can’t fill outside form fields.

3. Functional Test

Functional testing is almost same as acceptance testing except one difference that it doesn’t require browser to run the tests. It directly interacts with your application/api and it has separate module for php frameworks like:

  • Symfony2
  • Laravel5
  • Yii2
  • Zend Framework
Supported frameworks by codeception

All framework share same interface so your tests are not dependent on framework, you just need to enable your desired module in functional configuration suite, it does not mean Codeception supports only the above mentioned frameworks but list of supported frameworks are sufficient in most cases for developers.

Functional tests are verification activity to answer the question, “did we build the correctly working product?”, whereas, acceptance tests are sort validation activity to make sure we build the right thing.

Note: If you are not using framework you can’t write functional tests, because it is just supported by the framework itself. Check the example:

4. API Tests

Codeception has facility to make custom suites, so we can create API suite with command provided by Codeception.

vendor/bin/codecept generate:suite api

Above command will create new folder called tests/api. Every suite will have its own configuration file. Open api.suite.yml and add necessary information. your file should be look like this:

  • If you are using Laravel, then you can add Laravel module instead of phpBrowser.

Here we have example of API test:

You can download source code with suite examples from here.

Codeception Installation?

We can install Codeception from composer:

composer require codeception/codeception — dev

After that run following command which will create /tests folder in your project.

/vendor/bin/codecept bootstrap

Codeception Configuration

We will have codeception.yml in our root directory which is generated from above mentioned command. Let’s have a look into codeception.yml file and i have also added some comments to describe each line:

you can read more about the available configuration here.

Codeception Directory Structure

If you go in /tests directory which is created as a result of ./vendor/bin/codecept bootstrap command. There you will see following directory structure:

Directory Structure of tests folder
  • _data
    This directory can have DB /file/Fixtures 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.

--

--