I am trying to send all the failed test case error to an email whenever my rake test(Minitest) fails, Right now it shows the error and failed test case in the terminal now.
I have no clue how to capture those failed test cases errors
and failed cases
related info in some rails variable to and send those errors in the email.
I just want to get the error in my rails programmatically each time my test cases fail just like it shows in the terminal when I run rake test
.
Explored Simplecov Github doc too, but didn't find anything
I am using these 3 gems to generate coverage report including Minitest gem too
group :test do
gem 'simplecov'
gem 'simplecov-cobertura'
gem 'minitest'
end
Like this failure case in terminal
Any help would be highly appreciated.
There are multiple ways to achieve this, I will describe one of the ways. Most of the big testing libs have a concept of custom reporters or hooks around their execution lifecycle, you would want to essentially use that to trigger your email in case tests fail. In case of minitest, you should follow these examples from the minitest doc.
You should create a minitest plugin and let the minitest plugin load a custom reporter which keeps tracks of failures and sends them over an email once the test suite is finished. Your custom reporter might look something like
# 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
If you want to see what more you can do, have a look at some of the inbuilt reporters.