How to Set Up and Run a Separate Set of Tests with Their Own Spec Helper File

There may come a time when you don’t want to run all of your tests together. A common example is wanting to split out running slow integration tests and unit test. On one of my projects we needed a separate set of tests to run at intervals just to make sure some of the numbers were within safe bounds, so we had to set up completely separate tests. This involved setting up a different spec_helper configuration for the different set of tests. Here, I will do a quick walkthrough of how to set up and run those tests.

  1. Create a directory for your test set under your main application directory (not under spec). I’ll use the directory name 'separate_verification' as an example.
  2. Create a spec helper file under the separate_verification directory, and call it separate_verification_spec_helper.rb and configure it as you need to. I personally prefer to copy from spec/spec_helper.rb and tweak from there—it’s much less scary when you don’t have to start completely from scratch.
  3. Add your tests to separate_verification directory. Make sure to include require "separate_verification" at the top of your spec files.
  4. To run your tests, you have to specify the directory of the spec_helper file to use, in addition to specifying the tests to run: rspec -I separate_verification/ separate_verification/

And that’s it!

Note that it is advisable to also create a separate test environment for your tests (in my example, you would create a separate 'separate_verification' environment) if you’re messing around with specific data. It would be safe so that there aren’t any unexpected side-effects due to leftover data when running your regular specs.