Rspec和watir-webdriver;基于断言运行/跳过测试?

时间:2016-09-26 19:18:15

标签: rspec filtering watir-webdriver

我正在寻找一种方法来根据断言特定元素的存在来包含或排除特定的it块。
背景:我有一个冒烟测试,它会查看元素各部分的功能。我希望为其他功能添加更多测试,但如果页面上有特定部分,则仅
我的想法的伪代码:

describe 'Smoking sections' do
    it 'runs test 1' do
        # does stuff
    end
    it 'runs test 2' do
        # does more stuff
    end
    # if foo_section.present? == true do
        # run additional tests using `it` blocks
    # else
        # p "Section not present"
    # end
    it 'continues doing more tests like normal' do
        # does additional tests
    end
end

这种过滤是否可行?

1 个答案:

答案 0 :(得分:1)

RSpec提供了许多approaches for skipping tests。在这种情况下,您希望在示例中使用skip方法。通过使用before hook来检查该部分的存在,这是最容易实现的。

require 'rspec/autorun'

RSpec.describe 'Smoking sections' do
  it 'runs test 1' do
    # does stuff
  end

  it 'runs test 2' do
    # does more stuff
  end

  describe 'additional foo section tests' do
    before(:all) do
      skip('Section not present') unless foo_section.present?
    end

    it 'runs additional foo test' do
      # runs foo test
    end    
  end

  it 'continues doing more tests like normal' do
    # does additional tests
  end
end

虽然您可能需要考虑设计您的烟雾测试,以便所有测试都应该运行。如果你有可跳过的测试,它可能会失败。