20

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?)

andrewdotnich
  • 16,195
  • 7
  • 38
  • 57

2 Answers2

23

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
Robert Speicher
  • 15,382
  • 6
  • 40
  • 45
  • Implicit filters seem like the way to go. I'm surprised that there seems to be no way for the test to report to the formatter that it was excluded :/ – andrewdotnich Mar 24 '11 at 22:53
6

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 =)

Ngoral
  • 4,215
  • 2
  • 20
  • 37
  • @andrewdotnich Actually 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 =) Will edit my reply in a sec – Ngoral Jul 21 '20 at 11:05