Ruby Yagi 🐐

Ruby, Rails, Web dev articles

Run specific tests on Rspec using tag

by Axel Kee 15 August 2020

Sometimes when you are modifying a feature on a large Rails app, it would be faster to just run specs related to that feature instead of running the full test spec (eg: 1000+ feature specs that takes 20 minutes to run).

You can use tag for this, say we want to tag a few test cases related to checkout flow, with the tag β€œ:checkout” on the second parameter of it / describe / context .

# checkout_discount.spec
# ...
it 'should discount price when promo code applied', :checkout do
  # your test code here

You can use the same tag on different spec file :

# checkout_referral.spec
# ...
describe 'referral code applied', :checkout do
  it 'should give bonus to referral account' do
  # your test code here ...

Now you can specify rspec to only run tests which are tagged with :checkout like this :

rspec spec --tag checkout

This command means that rspec will check test files located inside the β€œspec” folder, and only run blocks (it / describe / context) which are tagged with β€˜checkout’. You can change this tag name to any name you like.