Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Functional testing with Codeception

Saturday, November 5th, 2016 by Servage

Functional-testingCodeception is a tool that can be used to automate the testing process of web applications. You can use it to write various types of automated tests, and this time we will focus on functional tests. You will learn two things: What functional tests are, and how to write them.

What is functional testing?

Let us first briefly get familiar with the concept of functional testing. Functional testing is one of the many software testing methods that allows you to verify that your application meets its requirements and does what it should do. In unit tests you test a small individual piece of code in your application. A functional test takes those small pieces and tests how well they work together.

Let’s imagine an application has two functions to register a new user: one to validate user input and one to save what the user enters in a database. These functions could be tested individually using unit tests. However, if you want to test the sign-up feature as a whole, you will be doing functional testing.

Functional tests in Codeception

In the world of Codeception, functional testing means sending of HTTP requests to an application and checking the results. For example, you send a POST request by submitting a form and see a “Thank you for signing up” notification.

Usually you would do this type of testing using a real web browser. This is the case with acceptance testing where you can control a real browser with PHP code. In Codeception, the only difference to functional testing is that there is no need for a graphical web browser.

Writing functional tests

Codeception tries to make writing tests as simple as possible, and it has for sure succeeded in it. To start writing a test, you can generate a sample test file using Codeception from the command line after you have downloaded it: codecept generate:cept functional SignUp. This will generate a PHP test for you. The file has the variable $I already instantiated with the FunctionalTester class of Codeception.

This is where writing tests gets simple and also fun. Things you most likely want to do in your tests include the ability to click something and fill in text fields. After all, these two actions make up at least 90% of what you do using a real browser.

Let’s start with clicking a button. You can think of the $I variable as “I want to do something”. To click a button, you can call the method $I->click(). Inside the parenthesis, enter a reference to the element you want to click. The reference can be one of various things: a CSS class, ID, name attribute or even Xpath.

To type in a text field, you can use the $I->fillField() method. As you probably guessed, the first parameter to this field is also a reference to the element to fill. The second parameter is the value you want to fill in. Check the Codeception documentation for more information on how these work and soon enough you will find out how surprisingly fun testing can be, while ensuring your applications work properly.

Functional testing with Codeception, 5.0 out of 5 based on 1 rating
Categories: Software & Webapps, Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.