RSpec 将失败的测试标记为已跳过

Waz*_*ery 3 rspec rspec-rails ruby-on-rails-4 rspec3

我们有一个用 RSpec 编写的单元测试套件。我们有一些失败的测试,实际上数量很多。

我正在寻找的是一个脚本或魔术命令,将所有失败的测试标记为已跳过,这样我就不必一一检查它们并将它们标记为已跳过。

Waz*_*ery 5

我发现这个很棒的脚本完全满足我的需要: https://gist.github.com/mcoms/77954d191bde31d4677872d2ab3d0cd5

把内容复制到这里,以防原来的要点被删除:

# frozen_string_literal: true

class CustomFormatter
  RSpec::Core::Formatters.register self, :example_failed

  def initialize(output)
    @output = output
  end

  def example_failed(notification)
    tf = Tempfile.new
    File.open(notification.example.metadata[:file_path]) do |f|
      counter = 1
      while (line = f.gets)
        if counter == notification.example.metadata[:line_number]
          line.sub!('it', 'skip')
          line.sub!('scenario', 'skip')
          @output << line
        end
        tf.write line
        counter += 1
      end
    end
    tf.close
    FileUtils.mv tf.path, notification.example.metadata[:file_path]
  end
end
Run Code Online (Sandbox Code Playgroud)