What is Codeception and how does Travis help
Codeception is a testing framework for PHP, with support for unit, functional and acceptance tests. Pre-prepared tests allow for a site to be quickly tested and ensure that no functionality is broken.
For example, how would you test a new feature and imagine that you had to explain how to test to a colleague?:
- I am on the home page ('/')
- I am going to login as a user
- I fill the username field with admin
- I fill the password field with admin
- I Click the login button
- I see the successful login message
- I would expect to be logged in now
- Because I don't see any errors
You'd explain what you were attempting to test in plain english, with replicable and easy steps, wouldn't you?
Now, let's take a look at how these steps could be written in Codeception with some actual code:
$I->amOnPage('/'); $I->amGoingTo('Login as admin'); $I->fillField('input#username', 'admin'); $I->fillField('input#password', 'admin'); $I->click('button#submit'); $I->see('Welcome admin'); $I->expect('To be logged in now'); $I->dontSee('Username and password does not match');
Amazing how similar they are, aren't they? In this way you can write automated tests that other users of the system can understand as well.
Codeception allows us to test different kinds of user perspectives and site scenarios. By doing so, we can simulate a user's natural flow throughout an application to make sure the app is working as expected.
But, how do you ensure these tests are run at appropriate times?
Travis is a solution to help with just this very situation. Travis CI is a hosted, distributed continuous integration service used to build and test software projects hosted at GitHub.
Therefore, each time you make a commit to either staging or master your tests will be run automatically in the background. You'll receive email notifications of both successes and failures!
What's more it's completely free for open source projects. Nice!
Let's quickly run you through how to set this up. First, let's start off with a brand new site:
joomla site:create continuous-integration
Be sure to initialise a new GitHub hosted project for this new site.
Note you will have to sign up to Travis: https://travis-ci.org/ and enable travis for this GitHub hosted project.
Codeception and Travis
Setting up codeception
Installation is easy:
$ joomla plugin:install joomlatools/console-codeception
Next, let's bootstrap your project with a codeception acceptance suite:
joomla codeception:bootstrap continuous-integration
This suite includes some sample tests and best practices we've developed.
In order to get your host machine configured for codeception, please refer to the Requirements of the README
Once installed run your tests by:
codecept run acceptance
Setting up travis
Installation is easy:
$ joomla plugin:install joomlatools/console-travis
Bootstrapping the existing project:
joomla travis:bootstrap continuous-integration
will create your travis integration for you.
Each time you create a new feature branch and push changes, travis will create a virtualised environment from which codeception tests can be run automatically.
And voila, automated testing for your Joomla project!