Jenkins automated tests for Rails
Today is very hard to find a clear and concise post about how to run automated Rails tests on Jenkins. So, to end with this tyranny of imprecise explanations, this post aims to configure a totally new Jenkins' job that can run Rails tests, keep the results on Rcov format and display an Rcov coverage graphic. We will assume for this that you have Jenkins configured and running.
We are going to need these plugins:
Ok, so here is where all the magic begins. First of all, we need to create a Github Api Token. Go to your Github user's settings, and then to "Personal access tokens". Generate a new token with these permissions:
Feel free to use a different Token description.
Copy your personal token and keep it safe.
Back to Jenkins, go to credentials and create a "secret text". Paste your just created personal Github token on "Secret" field and save it. We are going to need another credential, so let's create it now. Create a "Username with password" one and fill it with your Github user's credentials. Let's go to Jenkins configuration now. Look for "Github" section and complete it with this:
Clicking on "Test connection" should return a Credentials verified message.
Let's scroll down to "GitHub Pull Request Builder" now. All this should be already filled in. Just make sure you are using the username with password credentials we just created, and that "Auto-manage webhooks" is selected. Ok, so now we can save it and create a new item (Jenkins job). At this point we need to configure the job so the repo can be cloned/pulled. First we need to specify the Github project, so select "GitHub project" and fill it in with project https url. Next, Under Source Code Management, select "Git" and configure it like this:
This configuration will ensure that Jenkins is "hearing" on every PR as well. Moving on to Build Triggers section, select "Build when a change is pushed to GitHub", "GitHub Pull Request Builder" and "Use github hooks for build triggering". Now that we had configured Github on our new job, we need to tell Jenkins wich commands we want to run. Select Add build step/Execute shell and add the commands you think are necessary. To finish configuring the job, add a Post build action called "Publish Rcov report". On Rcov report directory, we need to specify the path where the Rcov file is going to be available; for that, use "coverage/rcov". "Coverage metric targets" is for tests results in percentage. I like to establish a high standard:
this way only higher than 90% is acceptable. Lower than that will mark the build as unstable.
So far so good. We installed the necessary plugins, created the credentials, configured Jenkins and created the job that will run on every PR and merge to master.
To finish, we need to add some gems to our Gemfile. RubyMetrics expects coverage report in Rcov format, so we need to output the tests results into this format. To do that, we need to include the next code in our Gemfile's test group:
gem "rspec" gem "simplecov" gem "simplecov-rcov"
And finally the following into spec_helper.rb:
require "simplecov" require "simplecov-json" require "simplecov-rcov" SimpleCov.formatters = [ SimpleCov::Formatter::HTMLFormatter, SimpleCov::Formatter::JSONFormatter, SimpleCov::Formatter::RcovFormatter ] SimpleCov.start
That's it! Now your job should have been triggered and an Rcov coverage graphic must have appeared on your Jenkins job: