Rails Kitchen

It's a place to write on stuff I learned recently.

Rspec Run Specific Set of Tests Using --tag Option

| Comments

In my current project I wanted to avoid running a few set of slow tests every time. Tagging feature in rspec allow us to filter the examples to be run by tag.

We can use the –tag (or -t) option to filter the examples by tags. The tag can be a simple name or a name:value pair. In the first case, examples with :name => true will be filtered. In the second case, examples with :name => value will be filtered, where value is always a string.

Tags can also be used to exclude examples by adding a ~ before the tag. For example ~tag will exclude all examples marked with :tag => true and ~tag:value will exclude all examples marked with :tag => value.

Given example show how to add tags to spec examples,
1
2
3
4
5
6
7
8
describe "group with tagged specs" do
  it "example I'm working now", :focus => true do; end
  it "special example with string", :type => 'special' do; end
  it "special example with symbol", :type => :special do; end
  it "slow example", :skip => true do; end
  it "ordinary example", :speed => 'slow' do; end
  it "untagged example" do; end
end
To run only examples with a simple tag, for examlple to run tests with a tag focus
run ”rspec spec/ –tag focus
Then the output should contain all the tests with tag :focus=>true
To exclude examples with a name:value tag
run ”rspec spec/ –tag ~speed:slow
Then the tests with tag speed:slow” will not be run.

Detailed documenation is given here

Thats it, Happy Testing….

Comments