Posted about 1 month ago. Visible to the public. Linked content. Auto-destruct in 28 days

Updated: RSpec: Tagging examples and example groups

Added:

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.

Changes

  • In RSpec you can tag examples or example groups with any tags you like simply by saying
  • ```
  • 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.
  • ```
  • 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:
  • ```
  • 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.
  • ```
  • 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](https://makandracards.com/makandra/6459-run-your-own-code-before-specific-rspec-examples), too.
  • ```
  • RSpec.configure do |config|
  • config.before(:each) do |example|
  • if example.metadata[:slow]
  • # To one thing here
  • else
  • # Or otherwise this
  • end
  • end
  • end
  • ```
  • ```
  • RSpec.configure do |config|
  • config.before(:each, slow: true) do
  • # To 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.
  • +

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
about 1 month ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more