Programmatically skip a test in RSpec
Asked Answered
U

3

2

I am curious if there is a way to skip a test in RSpec in a before block? I am thinking something like this:

RSpec.configure do |config|
  config.before(:each) do |spec|
    if some_condition?
      spec.skip
    end
  end
end

The above call to skip does not work. This is for an idea I am working on, so it's important that it works this way and not have to modify the test with metadata for instance. I imagine it's possible using RSpec internals, for instance tagging the status, but I wanted to check first to see if there is any API for this.

Urbana answered 5/9, 2019 at 1:31 Comment(4)
You could break out of the blockSabaean
That does not work, just tried. By that I mean that it does exit the before block but the example still runs.Urbana
I see, then you could use pending() to suspend the test.Sabaean
That doesn't do it either, I already went through the options listed online.Urbana
U
2

I asked in the rspec mailing list, and it turns out skip 'reason' does the trick. The example is marked as pending, which is not ideal, but that answers the question.

Urbana answered 5/9, 2019 at 13:43 Comment(0)
I
3

Looks like you have to explicitly call the skip inside the block:

around(:each) do |example|
  if some_condition?
    example.run
  else
    example.skip
  end
end

Documentation for around hooks

Turns the example into a "pending" (skips it, prints output differently, etc)

Illustrious answered 5/9, 2019 at 2:39 Comment(2)
Does not work, as expected since this will surely run the example regardless.Urbana
I updated my comment with a working example that I tested. Call example.skip to skip a spec, example.run to run itIllustrious
U
2

I asked in the rspec mailing list, and it turns out skip 'reason' does the trick. The example is marked as pending, which is not ideal, but that answers the question.

Urbana answered 5/9, 2019 at 13:43 Comment(0)
L
0

You can implement tag for section and calculate value in lambda:

# spec/rails_helper.rb
RSpec.configure do |config|
  config.filter_run_excluding if: false
  ...
end

# spec/your_spec.rb
describe Something do
  # First example - hardcoded skip:
  it 'This example isnt start', if: false do
    ...
  end

  # Second example - progamatically skip:
  let(:skip_clause) { 3 }

  it 'This example also not run', if: -> { skip_clause > 2 } do
    ...
  end
end

These examples will not be marked as pending.

Litt answered 28/6 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.