Using Laravel with Travis CI for Automated Testing

As I mentioned in our post about Growing Pains, it’s time for us to start building some serious automated testing into Tend. What I needed was a simple way to not only create tests, but to also run them automatically whenever changes are pushed to our Github repo.

First I’ll cover how I setup some basic testing in Laravel, then I’ll jump into show how I used Travis CI to automate the testing.

Creating Tests in Laravel 5.1*
As part of our infrastructure updates, we moved Tend to Larvel 5.1, which has PHPunit built in. To get started, use artisan to create a new test from the project root…

php artisan make:test UserTest

Which will create new UserTest class within the tests directory.

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}

I also pulled in the use DatabaseTransactions trait which will automatically rollback any changes made to the database during testing (Really! It’s like magic!) So I end up with this…

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserTest extends TestCase
{
    use DatabaseTransactions;

        /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}

To run your tests, your suppose to be able to execute with the phpunit command. However, for some reason this doesn’t work for me. So I had to use the following from my project root:

vendor/bin/phpunit tests/UserTest

I also opted to use the SQLite database for testing. Only problem is that raw queries don’t play nice with SQLite. You can define in the config/database.php

'sqlite' => [
        'driver'   => 'sqlite',
        'database' => storage_path('database.sqlite'),
        'prefix'   => '',
    ],

And then you just need to tell Laravel that you want to use SQLite in the .env file.

DB_CONNECTION=sqlite

Using Travis CI to Automate Testing

If your like me, creating the testing isn’t enough. I’m not disciplined enough to remember to run the tests each time before I launch. I need for this to be automated, to run in the background, and notify me if anything is wrong.

This is where Travis CI comes in. Travis CI is directly integrated into Github’s UI. This means that every time I make a push changes up to Github, Travis CI automatically reads and runs the tests.

After sining up for Travis CI, the only thing you need to do to integrate it into Laravel’s testing, is to create a travis.yml file. Here is what I used for reference.

language: php

php:
  - 5.5.9

env:
  global:
    - setup=basic
    - APP_ENV=staging
    - APP_DEBUG=true
    - APP_KEY=xxxx
    - DB_CONNECTION=sqlite
    - CACHE_DRIVER=file
    - SESSION_DRIVER=file
    - QUEUE_DRIVER=sync

sudo: false

install:
- if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-source; fi
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-stable; fi
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-source --no-interaction --prefer-lowest --prefer-stable; fi

script: vendor/bin/phpunit

And that is it. Really! When you go to pull push to your Github repo, the changes will be made, and you’ll be notified directly in Github’s UI, and receive an email of the results. Now this is cool…

2015-10-19_12-35-01.png

 
32
Kudos
 
32
Kudos

Now read this

The Entrepreneurial Cook

I was listening to Little Bets by Peter Sims this morning on my run with the kids. Peter mentions a study by Saras Sarasvathy that I found quite interesting. The study titled What makes entrepreneurs entrepreneurial?, examined how... Continue →