在Rails代码中使用SimpleCov在MiniTest中获取所有错误和失败的测试用例

问题描述 投票:1回答:1

每当我的瑞克测试(Minitest)失败时,我都会尝试将所有失败的测试用例错误发送给电子邮件,现在,它现在在终端中显示错误和失败的测试用例。

我不知道如何在一些rails变量中捕获与failed test cases errorsfailed cases相关的信息并在电子邮件中发送这些错误。

我只想在每次测试用例失败时都以编程方式获取错误,就像运行rake test时在终端上显示的那样。

也浏览过Simplecov Github文档,但未找到任何内容

我也使用这3颗宝石来生成覆盖率报告,包括Minitest宝石

group :test do
 gem 'simplecov'
 gem 'simplecov-cobertura'
 gem 'minitest'
end

https://github.com/colszowka/simplecov

Like this failure case in terminal

任何帮助将不胜感激。

enter image description here

ruby-on-rails testing code-coverage minitest simplecov
1个回答
0
投票

有多种方法可以实现,我将描述其中一种方法。大多数大型测试库在其执行生命周期中都有自定义报告程序或挂钩的概念,如果测试失败,您可能希望使用它来触发电子邮件。如果是最小测试,则应遵循最小测试文档中的these examples

您应该创建一个minitest插件,并让minitest插件加载自定义报告程序,该报告程序会记录失败情况,并在测试套件完成后通过电子邮件将其发送给他们。您的自定义记者可能看起来像

# minitest/email_reporter_plugin.rb

module Minitest
  class CustomEmailReporter < AbstractReporter
    attr_accessor :failures

    def initialize options
      self.failures = []
    end

    def record result
      self.failures << result if !(result.passed? || result.skipped?)
    end

    def report
      if !self.failures.empty?
          MyAwesomeEmailService.send_email(prepare_email_content)
      end
    end

    def prepare_email_content
       # Use the data in self.failures to prepare an email here and return it
    end
  end

  # code from above...
end

如果您想了解更多功能,请看一下inbuilt reporters

© www.soinside.com 2019 - 2024. All rights reserved.