【问题标题】:How to ignore or skip a test method using RSpec?如何使用 RSpec 忽略或跳过测试方法?
【发布时间】:2015-02-02 00:32:40
【问题描述】:

请指导如何使用 RSpec 禁用以下测试方法之一。我正在使用 Selenuim WebDriver + 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

【问题讨论】:

标签: ruby rspec selenium-webdriver rspec2


【解决方案1】:

对此有多种选择。主要标记为pendingskipped,它们之间有细微的差别。来自文档

一个示例可以标记为已跳过,即未执行,也可以标记为待处理,但失败不会导致整个套件失败。

请参阅此处的文档:

【讨论】:

    【解决方案2】:

    Pending 和 skip 很好,但我一直将它用于需要忽略/跳过的较大的描述/上下文块。

    describe Foo do
      describe '#bar' do
        it 'should do something' do
          ...
        end
    
        it 'should do something else' do
          ...
        end
      end
    end if false
    

    【讨论】:

    • 作为这个问题的解决方案,这确实被低估了。不,这不是最明确的方法,但确实有效。
    • 这里的好处是它不会打印待处理,并且您可以在示例中包含错误代码
    【解决方案3】:

    另一种跳过测试的方法:

    # 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

    【讨论】:

      【解决方案4】:

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

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

      第二种方式:通过在块内调用pending。 示例:

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

      【讨论】:

        【解决方案5】:

        这是从示例脚本中忽略(跳过)上述测试方法(例如,Test01)的替代解决方案。

        describe 'Automation System' do
        
          # some code here
        
          it 'Test01' do
             skip "is skipped" do
             ###CODE###
             end
          end
        
          it 'Test02' do
             ###CODE###         
          end    
        end
        

        【讨论】:

        • 我喜欢这个。从语义上讲,“skip”、“xit”和“pending”是不同的东西
        【解决方案6】:

        您可以使用pending() 或将it 更改为xit 或将断言包装在待处理块中以实现等待:

        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
        

        【讨论】:

        • 另外,您也可以将scenario 更改为xscenario :)
        • 原来xcontext & xdescribe 也有效:)
        猜你喜欢
        • 2015-12-04
        • 2017-01-26
        • 2016-02-02
        • 2018-11-26
        • 2015-03-12
        • 1970-01-01
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多