使用 `--only-failures` 选项
--only-failures
选项过滤运行的示例,以便只执行上次运行时失败的示例。要使用此选项,您首先必须配置 config.example_status_persistence_file_path
,RSpec 将使用它来存储每次运行时每个示例的状态。
还有一个 `--next-failure` 选项,它是 `--only-failures --fail-fast --order defined` 的简写。它允许您反复只关注一个当前失败的示例,然后转到下一个失败,等等。
这两个选项中的任何一个都可以与另一个目录或文件名组合使用;RSpec 将只运行从加载的示例集中加载的失败。
背景
给定一个名为“spec/spec_helper.rb”的文件,其中包含
RSpec.configure do |c|
c.example_status_persistence_file_path = "examples.txt"
end
以及一个名为“.rspec”的文件,其中包含
--require spec_helper
--order random
--format documentation
以及一个名为“spec/array_spec.rb”的文件,其中包含
RSpec.describe 'Array' do
it "checks for inclusion of 1" do
expect([1, 2]).to include(1)
end
it "checks for inclusion of 2" do
expect([1, 2]).to include(2)
end
it "checks for inclusion of 3" do
expect([1, 2]).to include(3) # failure
end
end
以及一个名为“spec/string_spec.rb”的文件,其中包含
RSpec.describe 'String' do
it "checks for inclusion of 'foo'" do
expect("food").to include('foo')
end
it "checks for inclusion of 'bar'" do
expect("food").to include('bar') # failure
end
it "checks for inclusion of 'baz'" do
expect("bazzy").to include('baz')
end
it "checks for inclusion of 'foobar'" do
expect("food").to include('foobar') # failure
end
end
以及一个名为“spec/passing_spec.rb”的文件,其中包含
puts "Loading passing_spec.rb"
RSpec.describe "A passing spec" do
it "passes" do
expect(1).to eq(1)
end
end
以及我已经运行过一次 `rspec`,结果是“8 个示例,3 个失败”。
运行 `rspec --only-failures` 仅加载包含失败的规范文件,并仅运行失败
当我运行 `rspec --only-failures`
那么“rspec –only-failures” 的输出应该包含“3 个示例,3 个失败”
以及“rspec –only-failures” 的输出不应该包含“加载 passing_spec.rb”。
将 `--only-failures` 与文件名组合使用
当我运行 `rspec spec/array_spec.rb --only-failures`
那么输出应该包含“1 个示例,1 个失败”
当我运行 `rspec spec/string_spec.rb --only-failures`
那么输出应该包含“2 个示例,2 个失败”。
使用 `--next-failure` 反复运行单个失败
当我运行 `rspec --next-failure`
那么输出应该包含“1 个示例,1 个失败”
以及输出应该包含“检查是否包含 3”
当我通过将“spec/array_spec.rb” 中的“to include(3)” 替换为“not_to include(3)” 来修复它
以及我运行 `rspec --next-failure`
那么输出应该包含“2 个示例,1 个失败”
以及输出应该包含“检查是否包含 3”
以及输出应该包含“检查是否包含 'bar’”
当我通过将“spec/string_spec.rb” 中的“to include('bar')” 替换为“not_to include('bar')” 来修复它
以及我运行 `rspec --next-failure`
那么输出应该包含“2 个示例,1 个失败”
以及输出应该包含“检查是否包含 'bar’”
以及输出应该包含“检查是否包含 'foobar’”
当我通过将“spec/string_spec.rb” 中的“to include('foobar')” 替换为“not_to include('foobar')” 来修复它
以及我运行 `rspec --next-failure`
那么输出应该包含“1 个示例,0 个失败”
以及输出应该包含“检查是否包含 'foobar’”
当我运行 `rspec --next-failure`
那么输出应该包含“所有示例都被过滤掉了”。
运行 `rspec --only-failures` 而不配置 `example_status_persistence_file_path` 会出现错误
当我运行 `rspec spec/passing_spec.rb --only-failures`
那么它应该通过“0 个示例,0 个失败” 来通过。
在不配置 `example_status_persistence_file_path` 的情况下使用 `--only-failures` 时,会显示清晰的错误
给定我没有配置 `example_status_persistence_file_path`
当我运行 `rspec --only-failures`
那么它应该失败,并显示“要使用 `--only-failures`,您必须首先设置 `config.example_status_persistence_file_path`”。