2017-09-15 110 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 

可以使用环境变量,从而不修改代码以只运行测试的一个子集。