Rspec 和 watir-webdriver; running/skipping 基于断言的测试?

Rspec and watir-webdriver; running/skipping tests based on assertion?

我正在寻找一种方法来根据断言特定元素的存在来包含或排除特定 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

这种过滤可行吗?

RSpec提供了approaches for skipping tests个数。在这种情况下,您想在示例中使用 skip 方法。这最容易通过使用 before 挂钩来检查该部分的存在来实现。

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

尽管您可能想考虑设计冒烟测试,使所有测试都应该 运行。如果你有可跳过的测试,它可能会破坏目的。