【问题标题】:Rspec and watir-webdriver; running/skipping tests based on assertion?Rspec 和 watir-webdriver;基于断言运行/跳过测试?
【发布时间】:2016-09-26 19:18:15
【问题描述】:

我正在寻找一种基于断言特定元素的存在来包含或排除特定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 filtering watir-webdriver


    【解决方案1】:

    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
    

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

    【讨论】:

    • 谢谢!这就是我想要做的。我同意不应该跳过测试——我目前的目标是继续添加到我当前的冒烟测试中,直到我有更多时间来重构并将其分解为更小的规格(即更简洁)的测试。
    • 我确实有一个问题:当我设置我的规范时,它通过了我现有的所有 itfirstthen它通过了可跳过的。这和预期的一样吗?我的印象是它会自上而下运行,包括可跳过的部分
    • 这是意料之中的。 execution order is defined 作为示例从上到下,然后从上到下嵌套组。鉴于可跳过的测试位于嵌套组中,它们最后运行。如果您需要将测试按特定顺序排列,则可以更改嵌套。
    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2019-09-28
    • 2018-09-07
    • 1970-01-01
    相关资源
    最近更新 更多