Introduction to automated testing with Codeception in PHP projects

What is automated testing? How do we implement it at WATA Factory? What are the reasons that make automated testing so interesting?

In this article we will talk about Codeception, one of the tools we use for automated testing in our PHP implemented web projects.

What is automated testing?

When we talk about automated testing in the field of software testing, we want to achieve that manual tests can be executed unattended, with the help of a tool that automatically performs and improves the process, but without replacing 100% of the benefits of manual testing. We already reported on this in an earlier article.

If we can design the tests to be run by a computer, then we achieve continuity of testing while evolvingthe application.

Why is automated testing interesting for us?

At WATA Factory, we saw the need to implement this type of testing as the development of our projects required new functionalities to be tested along with the already existing ones in order to continue ensuring the quality of our products.

Automating tests brings a number of advantages that help to increase the quality level of the product and reduce its cost, as the repetition of tests does not require a large time investment in the phases of improvement or development of the product.

Some of the benefits we have achieved at WATA Factory through the implementation of automated testing are:

  • Automatic verification of compliance with the defined specifications using BDD (Behaviour Driven Development). One of the big advantages of this is the use of a common language between the client and the development team to confirm that the behaviour of the code is correct from the user’s point of view.
  • Reduction in the number of bugs, as we ensure that both new and old functionality continues to work correctly.
  • Saving time in testing, as we avoid repetitive tasks and ensure that changes in our programme do not affect other parts of the system.
  • Creating solid documentation.
  • Making teamwork easier and safer.

To start developing automated testing, we researched various testing frameworks and came across Codeception, which combined everything we needed into a single tool.

What is Codeception and how can it be installed?

Codeception is a PHP test framework that aims to create readable tests and describe what is happening from the user’s point of view. It allows us to perform acceptance, functional and unit tests.

The key to Codeception is its actors. Codeception understands tests as processes that are initiated by a person. In summary, a UnitTester initiates actions and tests the code, a FunctionalTester tests the application as a whole, and an AcceptanceTester interacts with the interface.

Each actor executes so-called steps, which are individual instructions within a scenario. The instructions that an actor can generate are grouped into modules: PhpBrowser, WebDriver, DB, FileSystem, etc.

For the installation we follow the steps described in the official documentation.

During installation, a folder called tests is created and within this folder a basic structure for coding the different types of tests is created.

As an example, we will test the classic login. Although there are more scenarios in this test, we will only show some of them in order not to go too far.

To create the test, run the following command:

php vendor/bin/codecept g:feature acceptance Login

This creates a ‘.feature‘ file where we write our user story in a formal language called Gherkin, with the special feature that we can run these scenarios as automated tests.

Feature: Login page
Check that the user can access correctly with correct credentials, in other case a warning text is shown.
 
Scenario: User inserts empty password and empty username
   Given I am on login page
   When I fill fields with empty value
     And I click on login button
   Then I see the entered user data are not correct
 
Scenario: Successful access with valid credencials
   Given I am on login page
   When I fill the fields with correct credencials
     And I click on login button
   Then I see Welcome Dummy Name

Scenarios are written step by step using GIVEN-WHEN-THEN criteria.

  • The GIVEN part is the precondition for the test Where to start from.
  • The WHEN part is the specific behaviour, i.e. the actions we need.
  • And finally THEN, the part where we get the expected result.

Defining the tests

Our next step is to define and convert the feature file into a valid test. It is defined in the AcceptanceTester file.

To get the necessary steps, the following command is executed:

php vendor/bin/codecept gherkin:snippets acceptance

Since we are in the Acceptance Suite, we use WebDriver as a module to implement, which allows us to interact with the browser. This means that we can use its methods within the tester file, just as we can with the script tests, with

$I->

One can use various methods that have already been developed (e.g. amOnPage or click). Each step of the Gherkin scenario is extended by the methods defined in Codeception.

We now show how this can be implemented in our case:

/**
 * @Given I am on login page
 */
public function iAmOnLoginPage()
{
 $I->amOnPage('/login');
}

/**
 * @When I fill fields with empty value
 */
public function iFillFieldsWithEmptyValue()
{
 $I->fillField('#username','');
 $I->fillField('#password','');
}

/**
 * @When I fill the fields with correct credencials
 */
public function iFillTheFieldsWithCorrectCredencials()
{
 $I->fillField('#username','correctUsername');
 $I->fillField('#password','correctPassword');
}

/**
 * @When I click on login button
 */
public function iClickOnLoginButton()
{
 $I->click('Login');
}

/**
 * @Then I see the entered user data are not correct
 */
public function iSeeTheEnteredUserDataAreNotCorrect()
{
 $I->see('the entered user data are not correct');
}

/**
 * @Then I see Welcome Dummy Name
 */
public function iSeeWelcomeDummyName()
{
 $I->see('Welcome Dummy Name,');
}

Finally, we run our .feature file with the following command:

php vendor/bin/codecept run acceptance Login.feature --steps 

to see that Given/When/Then is expanded with substeps and shows the expected result when executed.

Acceptance Tests (2)
-------------------------------------------------------------
Login page: User inserts empty password and empty username Signature: User inserts empty password and empty username
Test: App/Tests/surveys/Login.feature:User inserts empty password and empty username

Scenario -- 
Check that the user can access correctly with correct credentials, in other case a warning text is shown.

Given i am on login page
 I am on url "https://###########/login"
When i fill fields with empty value
 I fill field "#username"," "
 I fill field "#password"," "
And i click on login button 
 I click "LOGIN"
 I wait 5
Then i see the entered user data are not correct 
 I see "the entered user data are not correct" 
PASSED 

Login page: User inserts proper information - Successful access with valid credencials 
Signature: Successful access with valid credencials 
Test: App/Tests/surveys/articulo_test.feature: Successful access with valid credencials

Scenario --
Check that the user can access correctly with correct credentials, in other case a warning text is shown.

Given i am on login page 
 I am on url "https://########/login"
When i fill the fields with correct credencials 
 I fill field "#username","correctUsername"
 I fill field "#password","correctPassword"
And i click on login button 
 I click "LOGIN"
 I wait 5
Then i see welcome dummy name 
 I see "Welcome Dummy Name,"
PASSED 
-------------------------------------------------------------
Time: 19.57 seconds, Memory: 18.00 MB OK (2 tests, 2 assertions)

This tool offers us many advantages, it also has an application known as Webception where the client can run all the tests from the browser and thus be able to track the status of the application.