Run RSpec with Jenkins

Inspired by this post  I started integrate the Rails RSpec test with Jenkins, the most popular Continuous Integration platform.

The original post is written almost 2 years ago, however,  some steps need to be updated.

Here are my steps, start from configuring Jenkins:

Assumption

A Jenkins instance is already avaiable

Stage 1: Prepare Jenkins plugins

Three plugins are required and one is optional.

Required:

  • ruby-runtime                            // fundamental plugin
  • Jenkins ruby metrics plugin // generate Rcov report
  • Rake plugin                              // generate rails stats report

Optional:

  • Rvm                                           // setup ruby environment

At the time of this post is written, Rake plugin 1.8.0 has a serious issue which breaks Jenkins ruby metrics plugin, so please consider use 1.7.8 if possible.

For more information of this issue, please refer to https://issues.jenkins-ci.org/browse/JENKINS-22293

Rvm plugin, which is optional, can be used to select Ruby version before build step. It can be seen in “Build Environment” section if installed.

Screenshot of Jenkins Rvm plugin

Screenshot of Jenkins Rvm plugin

Alternative way to setup Ruby environment is to configure it in build step, as the following image:

Alternative for Jenkins Rvm plugin

Alternative for Jenkins Rvm plugin

Add

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
rvm use 1.9.3

at the head of Execute Shell section to switch between Ruby versions without Rvm plugin.

After Ruby metrics plugin and Rake plugin are installed, two configuration options can be seen:

Configuration from Jenkins Rake plugin

Configuration from Jenkins Rake plugin

Configuration from Jenkins Ruby metrics plugin

Configuration from Jenkins Ruby metrics plugin

Stage 2: Configure Jenkins job with necessary source code change

Choose Ruby version

Add build step

Shell build step

Shell build step

Add this in Rake file

Update Rakefile

Update Rakefile

Add this in Gemfile

  gem "simplecov"
  gem "simplecov-rcov"
  gem "ci_reporter"

Prepare Test Data

Currently test data preparation is implemented by database restoration, so it is also necessary to override rake’s db:test:load operation:

rspec.with.jenkins_08

Restore MySQL database from backup file

Always Return Zero in Build Step

If any test case fails, rake command will return non-zero code and thus Jenkins will mark this build as Failure instead of Unstable. Therefore, make sure always return zero in Shell.
I resolved this by this trick:

Always return 0 in build step

Always return 0 in build step

Configure Post-build Actions

Enable these 3 actions: “Publish JUnit test result report”, “Publish Rails stats report” and “Publish Rcov report”

Post-build actions

Post-build actions

Stage 3: Modify Rails code

Add this in spec/spec_helper

# ...
Sport.prefork do
  # ...
  require 'simplecov'
  require 'simplecov-rcov'
  SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
  # ...
end

Now just build your Jenkins job and have fun!

NOTE: Rails stat report has an issue, that there is always no result generated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s