Skip an RSpec test case at runtime
Asked Answered
S

2

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

Sobranje answered 24/3, 2011 at 6:8 Comment(0)
T
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
Thickwitted answered 24/3, 2011 at 21:38 Comment(1)
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 :/Sobranje
F
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 =)

Flagelliform answered 30/6, 2020 at 8:12 Comment(2)
@Sobranje 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 secFlagelliform
And how to skip depending on configuration. Some tests are valid only in particular configurations. UPDATE: got it, use the if: condition parameter to describe or context.Golter

© 2022 - 2024 — McMap. All rights reserved.