Code Happy: Unit Testing

← Back to Index

Please note that this chapter was written for VERSION 3 of the Laravel PHP Framework.

Unit testing can be a very useful tool for a web developer. It can be used to check whether adding a feature, or modifying the codebase in some way has accidentally altered another feature, causing it to fail. Some developers even practice Test-Driven Development (TDD), where tests are written before the code to ensure that the code being written meets all requirements.

Laravel provides the tests directory to contain all of your applications tests, and even adds a helper command to Artisan the CLI interface for running PHPUnit test cases.

Not only can the application be tested, but bundles can also contain their own test suites. In fact Laravel has a bundle devoted to testing the core features of the framework, it can be found in the Laravel tests github repository.

Installation

No we're not going to cover the Laravel installation again!

Laravel uses the PHPUnit software to execute its tests, before we can use the unit testing features of Laravel we will need to install this software.

Installation of PHPUnit can vary from operating system to operating system, therefore I think it would be best to look at the official documentation for PHP Unit to find installation instructions. You will find the installation page here.

Creating a Test

Let's take a look at a test case, fortunately Laravel has included an example test case for us!

<?php

// application/tests/example.test.php

class TestExample extends PHPUnit_Framework_TestCase {

    /**
     * Test that a given condition is met.
     *
     * @return void
     */
    public function testSomethingIsTrue()
    {
        $this->assertTrue(true);   
    }

}

As you can see we create our test cases in a file with the extension test.php, the name of the class must start with the word Test and it must extend the class PHPUnit_Framework_TestCase. These are limitations set not by Laravel, but by the PHPUnit software.

A PHPUnit test case may contain any number of tests, as camel-cased actions, prefixed with the word test. Our tests can contain a number of different assertions that decide whether our tests will pass or fail.

A full list of assertions can be found on the PHPUnit documentation website.

Running Tests

Tests can be ran using the Artisan command line interface. Simply use the test command to run all test cases.

php artisan test

and the result..

PHPUnit 3.6.10 by Sebastian Bergmann.

Configuration read from /home/daylerees/www/laravel/develop/phpunit.xml

.

Time: 0 seconds, Memory: 6.25Mb

OK (1 test, 1 assertion)

With the OK part appearing in bright green to show us that the tests have passed.

Of course we knew that the test would succeed because we are using the assertTrue() method to check the value true. There is no way it could fail.

Let's bully the test so that it will fail, we will simply change the parameter to false.

...
$this->assertTrue(false);
...

and the result :

PHPUnit 3.6.10 by Sebastian Bergmann.

Configuration read from /home/daylerees/www/laravel/develop/phpunit.xml

F

Time: 0 seconds, Memory: 6.50Mb

There was 1 failure:

1) TestExample::testSomethingIsTrue
Failed asserting that false is true.

/home/daylerees/www/laravel/develop/application/tests/example.test.php:12
/usr/bin/phpunit:46

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Now we have some bright red lines to indicate that the tests have failed, including some details about why the test failed.

If we wanted to test a bundle we would simply pass the bundle name as a parameter to the test command, for example.

php artisan test mybundle

Testing The Core

Laravel's core is well unit tested, if you would like to run the tests yourself, here is how you can do it.

Install the tests bundle

Download the tests bundle from github and extract it in the bundles/laravel-tests directory. Or use php artisan bundle:install laravel-tests to achieve the same goal.

Now simply use the command test:core to execute the core test package.

php artisan test:core

My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.

It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!