2014-12-04 88 views
22

請指導如何使用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 

回答

35

可以使用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

6

下面是一個替代的解決方案以忽略(跳過)上述試驗方法(比如說, 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

1

有兩種方法可以在測試時跳過特定的代碼塊。

示例:使用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 
7

另一種方式來跳過測試:

# 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

2

掛起和跳過很好,但我一直用這個更大的描述/上下文塊,我需要忽略/跳過。

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

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