📅  最后修改于: 2020-12-06 10:56:56             🧑  作者: Mango
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
您将看到此输出-
运行选项: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标签使子集测试非常容易!