A bit of sugar and parallelism for Rails and RSpec

Posted by The Sauce Labs Team in Selenium Resources

Even though we focus very heavily on full-stack acceptance testing for the rails world, we know other forms of automated tests are critical as well. Our rails developers here make pretty heavy use of RSpec unit tests, and it's nice to understand how to run those in parallel as well. If you're looking at how to setup a rails and selenium testing environment, check out our last post.

Parallelize the specs

We'll use the excellent parallel_specs to beat a bit of parallelism into our specs. It prepares a separate database for each test environment, groups the specs to divide amongst processes, and then starts up a rails environment with a separate database for each group of processes. I'll paraphrase the installation instructions for convenience. Install the required plug-in/gem:

sudo gem install parallel
script/plugin install git://github.com/grosser/parallel_specs.git

Here's the semi-ingenious point - yaml can interpret ERB, so we can pass in an environment variable to the database.yml specifying at launch which database we want it to connect to. Open config/database.yml and add the following:

  adapter: sqlite3
  database: db/xxx_test<% ENV['TEST_ENV_NUMBER'] %>.sqlite3
  pool: 5
  timeout: 5000

(You can of course replace xxx_ with your project name) So for example, to have our tests run against the xxx_test2 database, we would use:

export TEST_ENV_NUMBER=2; rake db:test:prepare

But it doesn't make much sense to invoke it manually. That's what plug-ins are for! Let's go head and create/migrate a few test databases:

export TEST_ENV_NUMBER=0; rake db:test:create; rake db:test:migrate;
export TEST_ENV_NUMBER=1; rake db:test:create; rake db:test:migrate;
export TEST_ENV_NUMBER=2; rake db:test:create; rake db:test:migrate;

Great, now you're able to run your non-Selenium tests in parallel!

But what about Selenium tests?

Stay tuned for our article on Sauce Labs' SpecStorm plugin, that allows you to run your Selenium tests in true parallel fashion with Selenium Grid or our very own Sauce OnDemand service.

Discuss: A bit of sugar and parallelism for Rails and RSpec

Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.