2016-09-26 40 views
0

我正在寻找一种方法来基于声明特定元素的存在来包含或排除特定的it块。
背景:我有一个烟雾测试,查看元素部分的功能。我希望为其他功能添加更多测试,但只有(如果页面上存在特定部分,则为)。我的想法
伪代码:Rspec和watir-webdriver;基于断言运行/跳过测试?

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

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 

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

+0

谢谢!这正是我期待的。我同意测试不应该被跳过 - 我目前的目标是继续添加到我目前的烟雾测试中,直到时间更倾向于重构并将其分解为更小的规格(即更简洁)的测试。 – kmancusi

+0

我确实有一个问题:当我设置好规格时,它首先通过我现有的所有“块”块,然后通过可跳过的块。这是否如预期的那样?我的印象是它会自上而下运行,包括可跳过的部分 – kmancusi

+0

这是预期的。 [执行顺序被定义](https://relishapp.com/rspec/rspec-core/docs/command-line/order)作为例子从上到下,然后从上到下嵌套组。鉴于可跳过的测试是在一个嵌套组中,他们跑到最后。如果您需要将测试按特定顺序进行,您可以更改嵌套。 –