How to ignore or skip a test method using RSpec?
Asked Answered
D

6

46

please guide how to disable one of the below test methods using RSpec. I am using Selenuim WebDriver + RSpec combinations to run tests.

require 'rspec'
require 'selenium-webdriver'

describe 'Automation System' do

  before(:each) do    
    ###
  end

  after(:each) do
    @driver.quit
  end

  it 'Test01' do
      #positive test case
  end

  it 'Test02' do
      #negative test case
  end    
end
Drinkwater answered 4/12, 2014 at 7:49 Comment(1)
I found Programmatically skip a test in RSpec relevant even though it's not a suitable dupe target. You can use around hooks and call example.skip on a condition.Woodhead
L
71

You can use pending() or change it to xit or wrap assert in pending block for wait implementation:

describe 'Automation System' do

  # some code here

  it 'Test01' do
     pending("is implemented but waiting")
  end

  it 'Test02' do
     # or without message
     pending
  end

  pending do
    "string".reverse.should == "gnirts"
  end

  xit 'Test03' do
     true.should be(true)
  end    
end
Locoweed answered 4/12, 2014 at 8:4 Comment(2)
Also, you can change scenario to xscenario too :)Venitavenite
Turns out xcontext & xdescribe also works :)Visional
E
19

Another way to skip tests:

# feature test
scenario 'having js driver enabled', skip: true do
  expect(page).to have_content 'a very slow test'
end

# controller spec
it 'renders a view very slow', skip: true do
  expect(response).to be_very_slow
end

source: rspec 3.4 documentation

Exculpate answered 4/7, 2016 at 16:29 Comment(0)
S
11

Pending and skip are nice but I've always used this for larger describe/context blocks that I needed to ignore/skip.

describe Foo do
  describe '#bar' do
    it 'should do something' do
      ...
    end

    it 'should do something else' do
      ...
    end
  end
end if false
Sandpiper answered 30/11, 2016 at 13:43 Comment(2)
This is really undersold as a solution to this problem. No it's not the most clear approach, but it's really effective.Arachne
Bonus here is that it does NOT print pending and you can have bad code inside the exampleDominik
D
9

Here is an alternate solution to ignore (skip) the above test method (say, Test01) from sample script.

describe 'Automation System' do

  # some code here

  it 'Test01' do
     skip "is skipped" do
     ###CODE###
     end
  end

  it 'Test02' do
     ###CODE###         
  end    
end
Drinkwater answered 5/12, 2014 at 10:5 Comment(1)
I like this one. Semantically, "skip" "xit" and "pending" are different thingsSiege
F
6

There are a number of alternatives for this. Mainly marking it as pending or skipped and there is a subtle difference between them. From the docs

An example can either be marked as skipped, in which is it not executed, or pending in which it is executed but failure will not cause a failure of the entire suite.

Refer the docs here:

Fredenburg answered 28/6, 2016 at 6:56 Comment(0)
R
1

There are two ways to skip a specific block of code from being running while testing.

Example : Using xit in place of it.

  it "redirects to the index page on success" do
    visit "/events"
  end

Change the above block of code to below.

xit "redirects to the index page on success" do #Adding x before it will skip this test.
    visit "/event"
end

Second way: By calling pending inside the block. Example:

 it "should redirects to the index page on success" do 
  pending                             #this will be skipped
    visit "/events" 
end
Randolph answered 9/12, 2014 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.