在第一次失败时停止执行特定的 rspec 测试(不是测试套件) [英] Stop execution of particular rspec test (not test suite) on first failure

查看:37
本文介绍了在第一次失败时停止执行特定的 rspec 测试(不是测试套件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有某种编程方式可以强制 rspec 测试在第一次失败时停止?

Is there some programmatic way to force rspec test to stop on first failure?

例如,

  1. 我有一个包含几个 rspec 测试的测试套件,并且我一次运行了它们.
  2. 每个 rspec 测试都位于单独的 spec.rb 文件中

如果这些测试中的任何一个出现故障,我想在失败的特定测试步骤上进一步停止执行该特定测试,但不停止执行我正在运行的整个测试套件.

If some failure occurs in any of these tests, I would like to stop the execution of that specific test further on that particular test step that failed but not to stop execution of whole test suite that I'm running.

我知道有以下方法:

RSpec.configure do |c|
  c.fail_fast = true
end

但是在我在测试套件中运行的任何规范文件中定义它会导致套件的所有执行失败.

But defining it in any of the spec files that I ran in my test suite causes all execution of suite to fail.

有什么办法可以处理这个案子吗?

Any way this case can be handled?

提前致谢

推荐答案

我知道这是一个老问题,但我在寻求自己实现相同行为时偶然发现了它.经过多次无果的搜索,我想出了一些实现起来相当简单的东西,它实际上提供了所需的行为!

I know this is an old question, but I stumbled across it while looking to achieve this same behavior myself. After much fruitless searching, I came up with something that is fairly straightforward to implement which actually provides the desired behavior!

只需在每个规范文件中添加以下内容即可在发生异常时跳过后续示例(在该特定文件中):

Just add the following to each spec file to skip subsequent examples (in that specific file) when an exception occurs:

  before :all do
    $continue = true
  end

  around :each do |example|
    if $continue
      $continue = false
      example.run
      $continue = true unless example.exception
    else
      example.skip
    end
  end

这里的逻辑应该是不言自明的.继续运行示例直到发生异常,然后跳过该规范的其余示例.每个钩子周围的钩子很好地促进了这个逻辑,因为它可以控制每个示例的执行.请参阅 RSpec 文档,了解有关around"钩子.

The logic here should be self-explanatory. Continue running examples until an exception occurs, then skip the rest of the examples for that spec. The around each hook facilitates this logic nicely since it can control the execution of each example. See the RSpec documentation for more info on the "around" hook.

这篇关于在第一次失败时停止执行特定的 rspec 测试(不是测试套件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆