PHP

Getting Started With Testing in Laravel


A very important part of developing large, quality web applications is having automated tests that ensure every part of your application works as expected, no wicked surprises!.

There are several types of testing but they are mainly classified into Acceptance, Functional and Unit testing.

Acceptance Testing

This kind of testing allows us to test our applications using the normal website viewing process of visiting a web page, filling a form, and submitting the form to see the desired result. We do this all the time when developing applications. This process can be automated.

Functional Testing

This kind of testing is very similar to Acceptance testing. The major difference is that they don’t require a web server to run tests, they are faster and also provide detailed stack trace on errors and failure.

Unit Testing

This involves testing several isolated parts of your application. These several small units are individually and independently scrutinized for proper operation. They involve testing things like functions, classes, interfaces et.c

Laravel 5 is a very awesome framework and it comes bundled with testing abilities. It’s built with Testing in Mind. You can check the doc for more details on the inbuilt testing architecture.

We’re going to use a very good, easy to write and Behavioural Driven testing framework called Codeception. Everything you need for testing is inbuilt and works just out of the box. No more pain in configuring Selenium, data cleanup, writing XPaths, and fixtures. In short some people call it PHPUnit on Steriods.

It extends PHPUnit ( the defacto PHP Unit testing framework ) and allows you to write all kinds of test just out of the box. In later tutorials, we’ll see how we can use Jeffery Ways Integrated Package to perform integration tests for our Laravel apps.

I have a sample web application built in Laravel 4.2 and upgraded to Laravel 5.1. We’re going to write tests to back the application from acceptance to functional and unit tests and see how to connect it with a Continous Integration Service online. The application is here on GitHub.

First Step

Clone the application and run all migrations.

then

Run composer install to get all the dependencies

..now, run the migrations but make sure the database config is all set up

Run the application using

Make sure it works.

Second Step.

We are going to install Codeception. Installation could be done globally or per project. Let’s just install in our project.

 

It installs Codeception into your vendor folder.

Run the following command to generate the required files needed for testing

Now, check your tests folder, you will see a lot new folders like _data, _envs, _output, _support, acceptance, functional, routes and unit. 

Inside the tests folder, we also have our configuration files acceptance.suite.yml, functional.suite.yml, unit.suite.yml, _bootstrap.php. The ExampleTest.php and TestCase.php comes by default with Laravel.

Third Step

Go into the acceptance suite config file: acceptance.suite.yml

Change the URL to that of your project, now our file would look like this:

laranaija2.0v is my root folder name, yours would be laranaija.  8000 is the port I run laravel applications on.

Go into the functional suite config file: functional.suite.yml

Our file would look like this because we’ll add the Laravel5 Module that makes Codeception seamlessly work with Laravel.

Note: create a .env.testing environment file for testing configs.

Unit test suite still remains the same for now unit.suite.yml

Fourth Step

Codeception has commands for generating test files. We’ll start with Acceptance Testing.

From the terminal run:

It will generate a ProjectSubmissionCept.php file

ProjectSubmissionCept.php 

The Beauty about Codeception is how readable and understandable the methods are just from their names.

Let’s just perform a sample test to see how this whole thing works:

Change ProjectSubmissionCept.php to become:

Go to terminal and run this command:

This should be the result:

 

Nice!!..See how easy it is to get started. Our test passed because Google.com.ng is actually a word on google’s home page.

Very Important:

Some of you could get a date.timezone error which is easy to fix by updating your php.ini file and setting something like date.timezone='Africa/Lagos' or date_default_timezone_set(). Note that you need to update your php.ini file responsible for the CLI. You can access that of Mac’s inbuilt PHP by navigating to /private/etc/php.ini.

If that doesn’t work for some reasons..Navigate into your tests directory, you’ll see a  _bootstrap.php file in the root. Open the file and paste date_default_timezone_set(‘Africa/Lagos’) in it. Codeception runs that file before all the tests, so that should make it work.

‘Africa/Lagos’ is my timezone, so please put yours.

Let’s run this command to see the steps involved:

The result should look like this:

Awesome!!!

Before we go further, we need to outline the exact functionalities we want to write acceptance tests for.

1. A user should be able to submit projects for review on the site.

2. A user should be able to submit developer profiles for review on the site.

Those are the core functionalities of the sample app we are testing.

In the next post, we’ll continue on Testing Laravel apps with Codeception and write the required tests for this app.

Thanks for following. If you have any questions, pls ask in the comments section below:

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.