RSpec标签提供了在spec文件中运行特定测试的简便方法.默认情况下,RSpec将在其运行的spec文件中运行所有测试,但您可能只需要运行它们的子集.假设您有一些运行速度非常快的测试,并且您刚刚对应用程序代码进行了更改,并且您只想运行快速测试,此代码将演示如何使用RSpec标记执行此操作.
describe "How to run specific Examples with Tags" do it 'is a slow test', :slow = > true do sleep 10 puts 'This test is slow!' end it 'is a fast test', :fast = > true do puts 'This test is fast!' end end
现在,将上面的代码保存在新文件名为tag_spec.rb.从命令行运行以下命令:rspec --tag slow tag_spec.rb
您将看到此输出 :
运行选项:include {:slow => true}
This test is slow! . Finished in 10 seconds (files took 0.11601 seconds to load) 1 example, 0 failures
然后,运行此命令:rspec --tag fast tag_spec.rb
你会看到这个输出 :
Run options: include {:fast = >true} This test is fast! . Finished in 0.001 seconds (files took 0.11201 seconds to load) 1 example, 0 failures
如您所见,RSpec标签使一部分测试变得非常容易!