RSpec - 标记



RSpec 标记提供了一种简单的方法来运行规范文件中的特定测试。默认情况下,RSpec 会运行它运行的所有规范文件中全部测试,但你可能只需要运行其中一个子集。让我们说你有一些运行非常快速的测试,并且你刚刚对应用程序代码做出了更改,只想运行快速测试,此代码将演示如何使用 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

你将看到以下输出 −

运行选项:包含 {: 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 标记使运行测试子集变得非常容易!

广告
© . All rights reserved.