3

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

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

4

2 回答 2

4

应该比较直接。RSpec 列出了这样的失败规范

rspec ./spec/models/user.rb:67 # User does this thing
rspec ./spec/models/post.rb:13 # Post does another thing
rspec ./spec/models/rating.rb:123 # Rating does something else entirely

文件名和行号指向测试的开始行,带有it ... do.

写一个脚本

  1. 从失败输出中提取文件名和行号
  2. 打开这些文件,转到指定的行
  3. 替换itxit.
于 2018-07-12T09:12:04.913 回答
4

我发现这个很棒的脚本可以满足我的需要: 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
于 2018-07-12T10:03:58.233 回答