仅使用 rspec 打印失败示例的数量

  • 本文关键字:失败 rspec 打印 ruby rspec
  • 更新时间 :
  • 英文 :


有没有办法rspec只打印失败示例的数量而没有任何其他信息?

我希望输出是这样的:

Finished in 2.35 seconds (files took 7.33 seconds to load)
1207 examples, 40 failures, 15 pending

谢谢

开箱即用 RSpec 没有与所需输出匹配的格式化程序。但是,可以编写自己的格式化程序。

这是 RSpec 3+ 的外观

RSpec::Support.require_rspec_core "formatters/base_text_formatter"
class SimpleFormatter < RSpec::Core::Formatters::BaseTextFormatter
  RSpec::Core::Formatters.register self, :example_passed, :example_pending, :example_failed, :dump_pending, :dump_failures, :dump_summary
  def example_passed(message)
    # Do nothing
  end
  def example_pending(message)
    # Do nothing
  end
  def example_failed(message)
    # Do nothing
  end
  def dump_pending(message)
    # Do nothing
  end
  def dump_failures(message)
  end
  def dump_summary(message)
    colorizer = ::RSpec::Core::Formatters::ConsoleCodes
    output.puts "nFinished in #{message.formatted_duration} " 
       "(files took #{message.formatted_load_time} to load)n" 
       "#{message.colorized_totals_line(colorizer)}n"
  end
end
# Example from RSpec Core - https://github.com/rspec/rspec-core/blob/bc1482605763cc16efa55c98b8da64a89c8ff5f9/lib/rspec/core/formatters/base_text_formatter.rb

然后从命令行运行rspec spec/ -r ./path/to/formatter -f SimpleFormatter .

为避免不断键入-r-f选项,您可以将它们放在.rspec

--require ./path/to/formatter
--format SimpleFormatter

现在,只需运行rspec spec/就会自动使用我们刚刚创建的格式化程序。

最新更新