Posted over 6 years ago. Visible to the public. Repeats. Linked content.

RSpec: Tagging examples and example groups

In RSpec you can tag examples or example groups with any tags you like simply by saying

Copy
describe ReportCreator, slow: true do # .. end describe ReportCreator do it 'generates reports', slow: true do # ... end end

You can then only run examples with these tags.

Copy
rspec --tag slow rspec -t slow # Using the parallel_tests gem rake "parallel:spec[,,--tag slow]"

Or you can run all examples except the ones with a certain tag:

Copy
rspec --tag ~slow # note the ~ rspec -t ~slow # Using the parallel_tests gem rake "parallel:spec[,,--tag ~slow]"

You can also assign values to tags, and only run or exclude those tags where the values match a given string.

Copy
describe ReportCreator do it 'generates reports', speed: 'slow' do # ... end end rspec . --tag speed:slow # Only run examples tagged with speed: 'slow' rspec . --tag ~speed:slow # Run examples except the ones tagged with speed: 'slow'

You can use the tags for hooks, too.

Copy
RSpec.configure do |config| config.before(:each, slow: true) do # Do one thing here end end

Note that RSpec will, although it prints your tags inclusion/exclusion config before the suite run, ignore tag config when you pass it a spec file with a line number. That's even when the line number references a describe or context block, where only some examples should be excluded.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Thomas Klemm
Last edit:
12 days ago
by Judith Roth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Thomas Klemm to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more