Skip to main content

Second thoughts

Blog about product engineering, long-term games and making better decisions

TIL: Run single test in the spec

This one is about Ruby, rspec, and feedback loops.

When you work with Ruby code and rspec, you can run only single group (describe/context) or single test in the whole spec file.

First, you have to enable inclusion filtering, it’s disabled by default:

# spec_helper.rb
RSpec.configure do |c|
  c.filter_run_when_matching :focus
  # c.treat_symbols_as_metadata_keys_with_true_values = true
end

Then mark single test (or group) in huge spec file with focus: true:

# spec/some_class_spec.rb
describe SomeClass do
  describe '#method' do
    context 'when one'
    context 'when two'
    context 'when three', focus: true
  end
end

After that rspec spec/some_class_spec.rb will run only when three test, and that speeds up feedback loop a lot.

Or if you set c.treat_symbols_as_metadata_keys_with_true_values to true, you could add just symbol:

# spec/some_class_spec.rb
describe SomeClass do
  describe '#method' do
    context 'when one'
    context 'when two'
    context 'when three', :focus
  end
end

or just prefix f to methods describe, context, it:

# spec/some_class_spec.rb
describe SomeClass do
  describe '#method' do
    context 'when one'
    context 'when two'
    fcontext 'when three'
  end
end