【问题标题】:How do you generate html reports when running with parallel_tests?使用 parallel_tests 运行时如何生成 html 报告?
【发布时间】:2014-09-25 06:11:39
【问题描述】:

我正在使用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 文件。我也看到了这个问题here,但我没有使用黄瓜,所以这并不适用于我。我尝试将--format html --out tmp/report&lt;%= ENV['TEST_ENV_NUMBER'] %&gt;.html 放入我的.rspec_parallel 文件中,但没有任何效果。

【问题讨论】:

    标签: ruby rspec rake parallel-testing


    【解决方案1】:

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

    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
    

    【讨论】:

    • 你如何使用这段代码,我是新手,不知道如何使用这段代码得到结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2014-09-27
    • 1970-01-01
    • 2020-01-24
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多