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.
Alternative way to setup Ruby environment is to configure it in build step, as the following image:
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:
Stage 2: Configure Jenkins job with necessary source code change
Choose Ruby version
Add build step
Add this in Rake file
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:
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:
Configure Post-build Actions
Enable these 3 actions: “Publish JUnit test result report”, “Publish Rails stats report” and “Publish Rcov report”
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.







