Getting Started With Codeception in Laravel 4
BDD or behavioral driven development has become very popular these days and everybody talks about it. Our goal here is to make you familiar with a testing framework called Codeception which is the best and certainly the simplest one out there and it also has a Laravel4 module to easily get started with testing your Laravel applications.
First lets explain how to install codeception and put it in use with Laravel 4.
We will cover how to install codeception globally. Lets assume you are running a unix based system (if you are not you should really consider running a virtual machine under Vagrant).
What you want to do is to download it first
after we downloaded it we move it to our bin folder so its globally accessible.
mv codecept.phar /usr/local/bin/codecept
and we make it executable
chmod +x /usr/local/bin/codecept
If you run windows you can download codeception to a folder that is added to your execution path. Than you create a file called codecept.bat and add to it
@ECHO OFF php "%~dp0codecept.phar" %*
This was the easiest part. Lets see if it runs correctly by typing
If it runs correctly you will see the version and usage instructions.
Lets go to our project root file. There we'll run
codecept bootstrap app
This will create all of the needed files and put the into
Since we are running codeception from the root of our project we will have to
codeception.yml there. Of course the paths need to be updated so our file
will look like this
paths: tests: app/tests log: app/tests/_log data: app/tests/_data helpers: app/tests/_helpers settings: bootstrap: _bootstrap.php suite_class: \PHPUnit_Framework_TestSuite colors: false memory_limit: 1024M log: true modules: config: Db: dsn: '' user: '' password: '' dump: app/tests/_data/dump.sql
Now if you run
codecept run you will get a mesaage
No tests executed! which means
that we have set everything up correctly. The only thing left is writing our tests :)
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 = 'Europe/Berlin'. Note that
you need to update you php.ini file responsible for the CLI.
Lets write our first acceptance test. We will test if we can sign in to our application. It's good to know that codecption supports three test formats, scenario-based Cept format, PHPUnit test format and hybrid scenario-unit Cest format. As a starting point we'll be using the Cept format. Back to our acceptance test. We will generate the required file using the command
codecept generate:cept acceptance Signin
This will generate a file called
SigninCept.php located in
It will look like this
<?php $I = new WebGuy($scenario); $I->wantTo('log in as regular user'); $I->amOnPage('/login'); $I->fillField('Email','email@example.com'); $I->fillField('Password','pass'); $I->click('Login'); $I->see('logout', 'a'); ?>
If we run
codecept run we'll see that the test passes. Here you can see a couple of tests
including Login test successfully passed.
Don't forget to modify
app/tests/acceptance.suite.yml. The PHPBrowser needs to match your application url.
This tutorial is the first one in a series about testing with Codeception. Next tutorial will deal with interacting with a database.Tweet Follow @tntstudiohr
comments powered by Disqus