Ruby on Rails Integration Testing with Integrity
I did some research on what was new in the Continuous Integration testing field in regards to Ruby this week. We have used CruiseControl.rb in the past which worked fine, but I remember the setup being a bigger pain than I thought it had to be. I was hoping for something simpler.
With this in mind, I decided to give Integrity a shot. Their title of “The Easy and Fun Automated Continuous Integration Server” was exactly what I was looking for. After installing, configuring, and playing around with it for the past day or so, I have not been disappointed.
With that being said, I did have to jump around to a few different sources to get everything running correctly on a server with Phusion Passenger, so I thought I would document the steps I had to take here.
Installation:
This will create an Integrity application directory with a couple files on your server, mainly config.yml, config.ru, and a “public” directory we will point our Apache virtual host to.
Edit config.yml to suit your needs. This file is very will commented and your edits should be self-explanatory. I kept the default Sqlite database configuration to keep things simple. However, due to this, I had to install Sqlite on our Ubuntu server via:
Setup a DNS entry to point to this server and create an Apache virtual host as you would any other Rails or Sinatra web application:
Once this is all complete, simply restart apache to access your new Integrity instance.
Project Creation:
Create a Test Database:
(You can skip this test if you can use Sqlite databases for testing. Our code inevitably has MySql specific SQL syntax in it which requires us to use MySql for our tests.)
Create a MySql user that will be used for Integrity tests:
Note that “IDENTIFIED BY PASSWORD” is optional. If it is not included, your user can log in without a password.
Project Creation:
Assuming the above was successful, you should now be able to browse to the Integrity URL you created. From here, click the “Add new project” link.
- Name: Unique name for the project you will be tracking
- Git Repo: .git URL of the project you will be tracking. For example, for an internal project on GitHub, the url would be something like: git@github.com:acme/my_cool_application.git
- Branch: The git branch Integrity should be tracking
Build Script: The rake command to use for testing after the code is pulled into your CI (Continuous Integration) server.
- rake spec (for rspec)
- rake test (for test unit)
- rake features (for Cucumber)
- etc
Customizing Your Build Script
The build script can be a tad bit tricky. It is our practice not to keep database.yml in Git for password security purposes. Because of this, Integrity will not know the test database configuration settings needed to run the tests. To get around this issue, we created a database.yml.test file with the db credentials Integrity would need to run a test similar to this:
Using MySql:
Using Sqlite:
In my Rails project, I then created the below custom Rake task in “lib/tasks/my_cool_application.rake”:
Once this is done and the files are added/committed/pushed to git, go back to your edit project page in Integrity and change your “Build Script” to be “rake test:move_test_db_config features” for Cucumber tests, for example.
Manual Build
After all of the above is complete, you should be able to click the “manual build” link for your new project to pull your code and run your tests!
Automated Tests Via Git Service Hooks
Integrity tests are automated with Git Service Hooks. Adding a service hook is drop-dead simple with GitHub. To do so:
- Browse to your project in GitHub
- Click on the “Admin” tab
- Click on “Service Hooks” in the header
- In the “Post-Receive URLs” box, add a URL similar to this:
- http://username:password@integrity.example.com/my_cool_application/push
- where “username” and “password” are the HTTP authentication credentials setup in the integrity/config.yml file
Once this is setup, your CI Integrity test will be run everytime you code is pushed to the appropriate branch in GitHub.
Notifiers
Integrity also provides a notification system to inform you when tests are run.
Current notifiers include email, Jabber, Campfire, IRC, Twitter, and Basecamp. These are installed independently of Integrity via Gems. Once a gem is installed on your CI server, just log back into your project within your Integrity web site and setup your notifier credentials. You are also free to write your own custom notifiers for applications that are not supported yet.
Resources:
Thanks to the following sites for assisting me with my original setup and this blog post: