In my last post I showed how to create a simple Redmine plugin from scratch. Being good developers we also created some test cases, but wouldn’t it be nice to have them run automatically against a range of Redmine versions?
Enter Travis CI
Just in case you’ve been living somewhere on a very remote island for the last years - Travis CI is an awesome continuous integration testing service that is tightly integrated with GitHub and totally free for open source projects.
So if you host your Redmine plugin on GitHub anyway it’s a no-brainer to use Travis CI. For me, Travis CI is actually the reason why I mirror all of my Redmine plugins (and also other stuff) on GitHub, even if I host the primary repositories elsewhere.
Set up Travis CI for your Redmine plugin
In order to make use of Travis CI you need to create a file called
.travis.yml at the top level of your Repo which tells Travis what to do, i.e.
which Ruby versions you want to run on, what other software packages your tests
depend on and any other setup steps that need to be performed.
For running Redmine plugin tests we need to prepare a few things that are common for each plugin:
- get a copy of Redmine
- install the plugin inside
- set up a test database
- execute Redmine’s
The set of possible variables that we might (and should) change to test our code in different environments are:
- version of the plugin that is to be tested (i.e. master branch, any released and still supported versions)
- Ruby version
- Redmine version
- Database (PostgreSQL, MySQL)
The first two items are easily covered by using a
.travis.yml like this:
This would run tests only against the
master branch of our plugin, but with
three different Ruby versions.
Travis CI doesn’t know anything about Redmine and it’s versions, but it has a generic way to extend the build matrix further via environment variables:
Now the Travis CI build matrix has three dimensions - Travis will run tests for each unique combination of Ruby version, branch and environment. You might add any number of combinations of environment variables to expand this even further, i.e. to pick different databases like Redmine does.
But who’s going to make use of the environment variables? I have a shell script for this which I originally borrowed from the Redmine backlogs plugin. Given a set of environment variables, this will do the right thing for any Redmine plugin, so I simply copy that over for every new plugin I build, along with a test database configuration.
Here’s the remainder of
First we instruct Travis to set up a recent version of PostgreSQL.
Next we override the install step (which by default would run bundle install
for Ruby projects), because we do not want this at this stage.
before_script simply creates the test database, and the main
part creates some more environment variables and finally invokes the test
The only thing that I have to change from plugin to plugin is the
NAME_OF_PLUGIN environment variable, everything else stays the same unless I
want to run tests against different Redmine versions and / or databases.
Git tag step-five shows the addition of the travis config and supporting files to the Redmine Issue Done Ratio Plugin.