Codeception: A Proven Automated Testing Tool

A reliable automated testing tool for software testing

Tuesday Jan 31, 2017

Author

Mike Mukul

Mike Mukul

Tech Lead

Testing is crucial prior to publicizing a software product. There are two types of tests: Manual and Automatic. Both have their benefits. For this blog, we’ll be doing a quick overview of the time-saving qualities in automatic testing, focusing on a great automated testing tool.

Manual Testing: Time-Consuming
As the name implies, manual testing requires a high level of human resource engagement to test projects. It becomes more time consuming when there is a requirement to test the application/product on different platforms. This requires that the same steps be repeated to validate the expected result. If there’s a bug or defect, this process will be even lengthier, as the manual tester must repeat each step again and again. In short: manual testing takes time – lots of time.

Automated Testing: Time-Saving
Automated testing greatly reduces challenges and shortens the timeline for software testing by decreasing repetitive steps. You can define the steps of the test by writing the test scripts for any feature and allowing the test to run anytime. To write the test script, you have to choose an automated testing tool. Many automation testing tools are available, both paid and open-source.

Benefits of Automated Testing

  • Saves time
    • Executes tests faster than manual testing.
    • Targets multiple platforms at once.
    • Increases test coverage (deep test cases are very hard to repeat every time and here it has to be written only once).
  • Results in a reliable product
    • Ensures accuracy in testing (a tester can lose focus with repetitive steps making a feature prone to error).
    • Allows the development team to use test cases as they build a project.

What Tests Should Be Automated?

Time-extensive: Those features that are going to take time while testing.
Difficult or Boring: The features that are overly mind-engaging or mind-numbing to test manually.
Repetitive: Features that need to be executed every, single time a test is done.
Consistent (frozen) features: Features that are consistent/frozen and do not change.
High-Risk features: Those most important features that involve high risk, and need to be done precisely.

Codeception: An Automated Testing Tool

There are many tools available on the market. Codeception is an open-source, free-testing, automation tool built on top of PHPUnit. It is a Behavior Driven Development (BDD) style testing framework, using the Selenium Web Driver to execute the test cases on different browsers like Chrome, Firefox, or PhantomJS.

Installation

Codeception is very easy to install with Composer. You can find Codeception packages under Packagist, the PHP package repository (you’ll find installation instructions there as well).

To install Codeception, using Composer, use the following commands. Assuming you already have Composer installed, execute the following command:

$ composer require "codeception/codeception"

Once Codeception is installed on your machine you need to generate configuration directories and files by using the following commands:

$ vendor/bin/codecept bootstrap

The bootstrap command will generate the default directory structure in your working directory. Once this command is finished, it creates a tests folder inside your project folder, which contains different subfolders for unit tests, functional tests and acceptance tests, along with configuration files. Now we are ready to start writing test cases for our project with our automated testing tool.

Create Your First Test Case

Codeception comes with a rich set of commands that are pretty straightforward and easy to understand. To see all the available commands and options run the following command:

$ vendor/bin/codecept -help

Create Acceptance Test

To create the test case, use the generate:cept command on your console followed by the acceptance option and then the test case name. Here’s the command that will create our first acceptance test case with this automated testing tool:

$ vendor/bin/codecept generate:cept acceptance FirstTest

The above command will create a file under tests/acceptance folder with the name FirstTestCept.php. So now we can start writing the code in this file.

The following will be a default code inside the file generated by the codeception generate command:

?php
$I = new AcceptanceTester($scenario);
$I->wantTo('perform actions and see result');

Add Test Code Inside the File

?php
$I = new AcceptanceTester($scenario);
$I->amOnPage('/');
$I->see('Home');

Before executing your first test case you have to do some configuration changes. To change your acceptance.suite.yml file you need to add any working url under the PhpBrowser url option.

class_name: AcceptanceTester
modules:
    enabled:
        - PhpBrowser
            url: http://localhost/project_name
        - \Helper\Acceptance

Run Your Test Case

$ vendor/bin/codecept run

This command will run your test and show the result if it passed or failed with colors indicating – pass (green) or fail (red). In order to test on real web browsers, use the Selenium driver, make a few changes to the acceptance.suite.yml file and rebuild the Acceptance class. Codeception includes a lot of functions to write full scenario test cases. For more details check Codeception official documents.

Cest-Formatted Tests

Previously, we created an acceptance file with the Cept format, but that is not the only the way we can create test cases in Codeception. Cest tests are also available in Codeception. The basic difference between those is that in the Cest format you can write multiple test cases.

Input this code:

$ vendor/bin/codecept generate:cest acceptance SecondTest

This will create a file inside the acceptance folder with the name of SecondTestCest.php and the following is a default structure of this file.

class SecondTestCest
{
  public function _before(AcceptanceTester $I)
  {
  }

  public function _after(AcceptanceTester $I)
  {
  }

// tests
  public function tryToTest(AcceptanceTester $I)
  {
  }
}

The before and after function do as their name implies, run the before and after test cases written in the file.

Conclusion

At IT Hands we have found Codeception to be the right automated testing tool for past and present projects. It’s been a good choice for us and we highly recommend it to any other company looking for automated testing.

Are you in need of some automated testing for a current project? Contact us today at solutions@ithands.com.

Subscribe to our blog: