rspec / rspec-core

RSpec runner and formatters
http://rspec.info
MIT License
1.23k stars 763 forks source link

Execute all specs including the given shared examples #2564

Open Startouf opened 6 years ago

Startouf commented 6 years ago

Subject of the issue

Ability to run all specs reusing a shared example directly from the command line

Given the specs

# spec/examples/cuteness_examples.rb
shared_examples_for 'a cute pet' do 
   it('is small enough') { expect(subject.cute?).to eq(true) }
   it('makes a cute sound') { expect(subject.sound.cute?).to eq(true) }
end

# spec/models/octocat_spec.rb
describe 'Octocat'
  it_behaves_like 'a cute pet'
end

# spec/models/the_doge_spec.rb
describe 'The Doge'
  it_behaves_like 'a cute pet'
end

Expect the following commands to run (and only run) Octocat & The Doge's cuteness specs

rspec --run-shared-examples spec/examples/cuteness_examples.rb
# =>
Octocat
  is small enough
  makes a cute sound
The Doge
  is small enough
  makes a cute sound

An advanced version of this feature would even support specifying the line of shared example you wish to see specs of

rspec --run-shared-examples spec/examples/cuteness_examples.rb:21
# =>
Octocat
  is small enough
The Doge
  is small enough

rspec --run-shared-examples spec/examples/cuteness_examples.rb:42
# =>
Octocat
  makes a cute sound
The Doge
  makes a cute sound

It would be very useful for people breaking their tests into multiple files with shared examples instead of having to either figure out the devil string, or copy it from failed examples (octocat_spec.rb[1:1:2:5:1])

JonRowe commented 6 years ago

This can already be accomplished after a sort (running a set of predetermined specs) through tagging, if you tag the shared examples you can filter to run just those specs through the cli.