【问题标题】:RSpec Mark Failed Tests as SkippedRSpec 将失败的测试标记为已跳过
【发布时间】:2018-12-20 10:30:44
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails-4 rspec rspec-rails rspec3


    【解决方案1】:

    应该相对简单。 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. replaces it with xit

    【讨论】:

    • 我估计这是一个 10 LOC 的 ruby​​ 脚本。 15 LOC 上衣
    【解决方案2】:

    我发现这个很棒的脚本可以满足我的需要: 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
    

    【讨论】:

    • 不错的发现!不知道为什么有人对此表示反对。也许是因为它是仅链接的答案。
    • 我会使用 xitxscenario 来保留信息,以防您想使用类似的脚本返回。如果 xscenario 不存在 - 将其定义为别名 relishapp.com/rspec/rspec-core/v/3-7/docs/configuration/…
    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多