如何在运行parallel_tests时生成html报告?



我正在使用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文件中,但没有任何效果。

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

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

最新更新