2017-09-15 39 views
0

我試圖以這種方式構建我的測試,以便我可以自己運行某些上下文塊,但還需要在個別塊中進一步實施嵌套標籤。事情是這樣的:在Rspec中運行嵌套標籤

context 'outer context', :outer_tag do 
    it 'inner it', :tag1 do 
     expect(1).to eq(1) 
    end 

    it 'inner it 2', :tag2 do 
     expect(2).to eq(2) 
    end 
end 

,我想運行的線沿線的東西:

rspec的--tag外標籤--tag TAG1

的希望,它將只運行測試

在帶有以下標籤的上下文中:使用以下標籤標記的外標籤:tag1

有沒有辦法獲得此行爲?目前,這似乎是作爲'或'來操作,當我想我正在尋找它作爲'和'的更多操作。

謝謝!

回答

1

你可以做這樣的事情:

RSpec.describe 'Something' do 
    context 'outer context', :outer_tag do 
    it 'inner one', :tag1, outer_tag: 'tag1' do 
     expect(1).to eq(1) 
    end 

    it 'inner two', :tag2, outer_tag: 'tag2' do 
     expect(2).to eq(2) 
    end 
    end 

    context 'another context', :different_tag do 
    it 'inner three', :tag2, different_tag: 'tag2' do 
     expect(3).to eq(3) 
    end 
    end 
end 

,然後運行:

rspec example.rb --tag 'outer_tag' 
# => Something 
#  outer context 
#  inner one 
#  inner two 
rspec example.rb --tag 'outer_tag:tag2' 
# => Something 
#  outer context 
#  inner two 
rspec example.rb --tag tag2 
# => Something 
#  outer context 
#  inner two 
#  another context 
#  inner three 

這將啓動,當你需要多層次,雖然得到怪異:

context 'third context', :final_tag do 
    context 'inside third', :inner_third, final_tag: 'inner_third' do 
    it 'inner four', :inner_four, inner_third: 'inner_four', final_tag: 'inner_third:inner_four' do 
     expect(4).to eq(4) 
    end 
    end 
end 

rspec example.rb --tag 'final_tag:inner_third:inner_four' 
rspec example.rb --tag 'inner_third:inner_four' 
rspec example.rb --tag inner_four 
# All run 
# => Something 
#  third context 
#  inside third 
#   inner four 

作品,但是非常冗長。

而且由於RSpec的方式處理命令行(哈希)上的標籤,它會導致一些意想不到的東西,試圖把它們混合起來:

rspec example.rb --tag outer_tag --tag ~'outer_tag:tag2' 
# Run options: exclude {:outer_tag=>"tag2"} 
# => Something 
#  outer context 
#  inner one 
#  another context    # <- nothing in this context was expected to run 
#  inner three (FAILED - 1) 

這樣的工作,但:

rspec example.rb --tag outer_tag --tag ~tag2 
# => Something 
#  outer context 
#  inner one 
1

您還可以使用RSpec'exclusion filter來運行您所有的:outer_tag測試,但具有:tag1規範的測試除外。只有

RSpec.configure do |config| 
    config.filter_run :outer_tag 
    config.filter_run_excluding :tag1 
end 

:tag2測試運行:

> rspec -fd tag_spec.rb 
Run options: 
    include {:outer_tag=>true} 
    exclude {:tag1=>true} 

outer context 
    inner it 2 

Finished in 0.00326 seconds (files took 1.07 seconds to load) 
1 example, 0 failures 

可以使用環境變量,從而不修改代碼以只運行測試的一個子集。