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.
1 |
git clone https://github.com/busayo/laranaija.git |
then
1 |
cd laranaija |
Run composer install to get all the dependencies
1 |
composer install |
..now, run the migrations but make sure the database config is all set up
1 |
php artisan migrate |
Run the application using
1 |
php artisan serve |
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.
1 |
composer require "codeception/codeception:*" |
It installs Codeception into your vendor folder.
Run the following command to generate the required files needed for testing
1 |
vendor/bin/codecept bootstrap |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
# Codeception Test Suite Configuration # # Suite for acceptance tests. # Perform tests in browser using the WebDriver or PhpBrowser. # If you need both WebDriver and PHPBrowser tests - create a separate suite. class_name: AcceptanceTester modules: enabled: - PhpBrowser: url: http://localhost:8000/laranaija2.0v - HelperAcceptance |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Codeception Test Suite Configuration # # Suite for functional (integration) tests # Emulate web requests and make application process them # Include one of framework modules (Symfony2, Yii2, Laravel5) to use it class_name: FunctionalTester modules: enabled: - HelperFunctional - Asserts - Laravel5: environment_file: .env.testing |
Note: create a .env.testing environment file for testing configs.
Unit test suite still remains the same for now unit.suite.yml
1 2 3 4 5 6 7 8 9 |
# Codeception Test Suite Configuration # # Suite for unit (internal) tests. class_name: UnitTester modules: enabled: - Asserts - HelperUnit |
Fourth Step
Codeception has commands for generating test files. We’ll start with Acceptance Testing.
From the terminal run:
1 |
codecept generate:cept acceptance ProjectSubmission |
It will generate a ProjectSubmissionCept.php file
ProjectSubmissionCept.php
1 2 3 4 |
<?php $I = new AcceptanceTester($scenario); $I->wantTo('perform actions and see result'); |
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:
1 2 3 4 5 6 7 8 9 10 |
<?php $I = new AcceptanceTester($scenario); $I->wantTo('Check If Google Home page contains Google.com.ng'); $I->amOnPage('http://google.com'); $I->see('Google.com.ng'); ?> |
Go to terminal and run this command:
1 |
codecept run acceptance |
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:
1 |
codecept run acceptance --steps |
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:
- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016