Setting Command-Line Defaults

We’ve brought these command-line options together in one chapter so that you can refer to them whenever you want to do something special for a single test run. Sometimes, though, you may need custom behavior for every run.

Rather than wasting time typing the same options over and over, you can save a set of arguments as command-line defaults. As the term implies, RSpec will use these by default for every run—but you can still override them.

To set defaults, save your desired options in a text file at any of the following three paths:

~/.rspec

Use this file in your home directory to store global personal preferences. RSpec will use it for any project on your machine. For instance, you may prefer to stop your test run on the first failed example, while your teammates might not. In this case, you could put --fail-fast in your ~/.rspec file, and this setting would apply just for you.

./.rspec

This file in a project’s root directory is for project-level defaults. Use restraint here; only put options that are necessary for the suite to run correctly, or for standards that your team agrees upon. For example, if there’s a file you always want loaded, you can --require it automatically. (Indeed, when you generate a new project with rspec --init, RSpec puts --require spec_helper into the project’s .rspec file for you.)

./.rspec-local

This file, which lives alongside a project’s .rspec file, is for your personal preferences for that project. Since everyone may have their own version of this file, make sure to exclude it from your source control system.

Options take precedence in the order we’ve listed them here, meaning that local options will override more global ones. For instance, if your project has --profile 5 set in its .rspec file, you could override this setting by putting --no-profile in the project’s .rspec-local file.

You can also set command-line defaults in the SPEC_OPTS environment variable; values set here will override ones set in text files.

Before we move on to the next way to configure RSpec, let’s take a moment to put this knowledge into practice. We’re going to set up RSpec to use a custom formatter to get the exact report output we want.