6

我正在使用parallel_tests框架并行运行一堆 rspec 测试。在并行化测试之前,我将测试结果输出到 html 文件中,如下所示:

rspec --format html --out tmp/index.html <pattern>

现在它看起来更像这样:

parallel:spec --format html --out tmp/index.html <pattern>

但是,现在测试是并行运行的,每个测试都生成自己的 html 文件,并且由于它们都使用相同的路径(tmp/index.html),最后完成的测试会覆盖输出 html 文件,我只留下一份测试报告。如何生成包含所有测试的汇总结果的单个 html 文件(这将是理想的)?如果这是不可能的,我怎样才能将每个测试输出到它自己的输出 html 文件中,这样它们就不会相互覆盖?

我尝试在 parallel_test 项目中使用内置记录器(ParallelTests::RSpec::RuntimeLogger、ParallelTests::RSpec::SummaryLogger 和 ParallelTests::RSpec::FailuresLogger)但这些都只生成简单的文本文件而不是好的像 rspec 这样的 html 文件。我也在这里看到了这个问题,但我没有使用黄瓜,所以这并不适用于我。我尝试放入--format html --out tmp/report<%= ENV['TEST_ENV_NUMBER'] %>.html我的.rspec_parallel文件,但这没有任何效果。

4

1 回答 1

5

我必须编写自己的格式化程序,这是代码以防其他人遇到此问题:

require 'fileutils'
RSpec::Support.require_rspec_core "formatters"
RSpec::Support.require_rspec_core "formatters/helpers"
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
RSpec::Support.require_rspec_core "formatters/html_printer"
RSpec::Support.require_rspec_core "formatters/html_formatter"

# Overrides functionality from base class to generate separate html files for each test suite
# https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/formatters/html_formatter.rb
class ParallelFormatter < RSpec::Core::Formatters::HtmlFormatter

  RSpec::Core::Formatters.register self, :start, :example_group_started, :start_dump,
                                         :example_started, :example_passed, :example_failed,
                                         :example_pending, :dump_summary

  # TEST_ENV_NUMBER will be empty for the first one, then start at 2 (continues up by 1 from there)
  def initialize(param=nil)
    output_dir = ENV['OUTPUT_DIR']
    FileUtils.mkpath(output_dir) unless File.directory?(output_dir)
    raise "Invalid output directory: #{output_dir}" unless File.directory?(output_dir)

    id = (ENV['TEST_ENV_NUMBER'].empty?) ? 1 : ENV['TEST_ENV_NUMBER'] # defaults to 1
    output_file = File.join(output_dir, "result#{id}.html")
    opened_file = File.open(output_file, 'w+')
    super(opened_file)
  end

end
于 2014-11-26T19:55:52.143 回答