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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27288775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:38:37  来源:igfitidea点击:

How to ignore or skip a test method using RSpec?

rubyrspecselenium-webdriverrspec2

提问by Prashanth Sams

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

请指导如何使用 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

回答by Зелёный

You can use pending()or change itto xitor wrap assert in pending block for wait implementation:

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

回答by Mugur 'Bud' Chirica

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

来源:rspec 3.4 文档

回答by Prashanth Sams

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

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

describe 'Automation System' do

  # some code here

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

  it 'Test02' do
     ###CODE###         
  end    
end

回答by tejasbubane

There are a number of alternatives for this. Mainly marking it as pendingor skippedand there is a subtle difference between them. From the docs

为此,有许多替代方案。主要是将其标记为pendingskipped,它们之间存在细微差别。从文档

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:

请参阅此处的文档:

回答by caspian311

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

回答by Prabhakar Undurthi

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

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

Example : Using xit in place of it.

示例:使用 xit 代替它。

  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:

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

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