如何忽略或跳过使用RSpec的测试方法?

问题描述:

请指导如何使用RSpec禁用以下测试方法之一。我使用Selenuim WebDriver + RSpec组合来运行测试。如何忽略或跳过使用RSpec的测试方法?

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 

可以使用pending()或更改itxit或包裹断言在未决的块为等待执行:

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 
+0

谢谢..它的工作原理! – 2014-12-04 08:41:35

下面是一个替代的解决方案以忽略(跳过)上述试验方法(比如说, Test01)。

describe 'Automation System' do 

    # some code here 

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

    it 'Test02' do 
    ###CODE###   
    end  
end 
+0

我喜欢这一款。在语义上,“跳过”“xit”和“pending”是不同的事情 – MegaTux 2016-10-07 15:20:17

有两种方法可以在测试时跳过特定的代码块。

示例:使用xit代替它。

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

将上面的代码块更改为下面的代码块。

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

第二种方法:通过调用块内部的挂起。 例如:

it "should redirects to the index page on success" do 
    pending        #this will be skipped 
    visit "/events" 
end 

有很多替代方案。主要标记为pendingskipped,它们之间存在细微的差异。来自文档

一个示例可以标记为跳过,其中未执行,或挂起执行,但失败不会导致整个套件发生故障。

参考,这里的文档:

另一种方式来跳过测试:

# 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 

来源:rspec 3.4 documentation

挂起和跳过很好,但我一直用这个更大的描述/上下文块,我需要忽略/跳过。

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

    it 'should do something else' do 
     ... 
    end 
    end 
end if false