44

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
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
  • I found [Programmatically skip a test in RSpec](https://stackoverflow.com/a/57797745/6243352) relevant even though it's not a suitable dupe target. You can use `around` hooks and call `example.skip` on a condition. – ggorlen Apr 16 '21 at 01:13

6 Answers6

66

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
Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103
17

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

Mugur 'Bud' Chirica
  • 4,246
  • 1
  • 31
  • 34
9

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
caspian311
  • 161
  • 2
  • 3
  • 1
    This is really undersold as a solution to this problem. No it's not the most clear approach, but it's really effective. – josh Nov 01 '19 at 16:04
  • Bonus here is that it does NOT print pending and you can have bad code inside the example – Jason Aug 06 '21 at 19:08
8

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
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
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:

tejasbubane
  • 914
  • 1
  • 8
  • 11
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
Prabhakar
  • 6,458
  • 2
  • 40
  • 51