Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip an RSpec test case at runtime

I'm running RSpec tests against a website product that exists in several different markets. Each market has subtly different combinations of features, etc. I would like to be able to write tests such that they skip themselves at runtime depending on which market/environment they are being run against. The tests should not fail when run in a different market, nor should they pass -- they're simply not applicable.

Unfortunately, there does not seem to be an easy way to mark a test as skipped. How would I go about doing this without trying to inject "pending" blocks (which aren't accurate anyway?)

like image 386
andrewdotnich Avatar asked Mar 24 '11 06:03

andrewdotnich


2 Answers

Use exclusion filters.

describe "market a", :market => 'a' do
   ...
end
describe "market b", :market => 'b' do
   ...
end
describe "market c", :market => 'c' do
   ...
end

RSpec.configure do |c|
  # Set these up programmatically; 
  # I'm not sure how you're defining which market is 'active'
  c.filter_run_excluding :market => 'a'
  c.filter_run_excluding :market => 'b'
  # Now only tests with ":market => 'c'" will run.
end

Or better still, use implicit filters.

describe "market a", :if => CurrentMarket.a? do # or whatever
   ...
end
like image 66
Robert Speicher Avatar answered Oct 22 '22 01:10

Robert Speicher


I spotted this question looking for a way to really skip examples that I know are going to fail now, but I want to "unskip" them once the situation's changed. So for rspec 3.8 I found one could use skip inside an example just like this:

it 'is going to fail under several circumstances' do
  skip("Here's the reason") if conditions_met?
  expect(something)
end

Actually, in most situations (maybe not as complicated as in this question) I would rather use pending instead of skip, 'cause it will notify me if tests stop failing, so I can "unskip" them. As we all know that skipped tests will never be performed ever again =)

like image 30
Ngoral Avatar answered Oct 22 '22 00:10

Ngoral