Search⌘ K
AI Features

Running Smaller Groups of Tests: RSpec

Explore how to optimize your Rails test suite by running smaller groups of RSpec tests. Learn techniques including using file and line number specs, tagging tests with metadata to focus or exclude certain tests, and running only failed tests to enhance development speed and efficiency.

RSpec running smaller groups of tests

We recommend using the rspec command directly rather than going through any rake tasks that RSpec defines because it’s a bit faster. The rspec command can take one or more arguments: files, directories, or file globs. For file globs, all matching files are run the following way:

Terminal 1
Terminal
Loading...

RSpec makes it easy to run a single spec. All we need to do is add the line number to the file along with a colon:

C++
$ bundle exec rspec spec/models/task_spec.rb:5

RSpec has a less user-friendly syntax that identifies the tests by their place within each nested block. This version is more robust against adding lines to tests and is what some RSpec tools (like bisect) will output:

C++
$ bundle exec rspec spec/models/task_spec.rb[1:1:1]

When RSpec’s default formatter runs, RSpec will place the file name and line number of each failing test at the end of the output, ...