Class: MiniTest::Reporters::JUnitReporter

Inherits:
Object
  • Object
show all
Includes:
MiniTest::Reporter
Defined in:
lib/minitest/reporters/junit_reporter.rb

Overview

A reporter for writing JUnit test reports Intended for easy integration with CI servers - tested on JetBrains TeamCity

Inspired by ci_reporter (see https://github.com/nicksieger/ci_reporter) Also inspired by Marc Seeger's attempt at producing a JUnitReporter (see https://github.com/rb2k/minitest-reporters/commit/e13d95b5f884453a9c77f62bc5cba3fa1df30ef5) Also inspired by minitest-ci (see https://github.com/bhenderson/minitest-ci)

Instance Method Summary collapse

Methods included from MiniTest::Reporter

#after_suite, #after_test, #before_suite, #before_suites, #before_test, #error, #failure, #filter_backtrace, #output, #pass, #print, #puts, #runner, #skip, #verbose?

Constructor Details

#initialize(reports_dir = "test/reports", empty = true) ⇒ JUnitReporter

Returns a new instance of JUnitReporter.



15
16
17
18
19
20
21
22
23
# File 'lib/minitest/reporters/junit_reporter.rb', line 15

def initialize(reports_dir = "test/reports", empty = true)
  @reports_path = File.absolute_path(reports_dir)

  if empty
    puts "Emptying #{@reports_path}"
    FileUtils.remove_dir(@reports_path) if File.exists?(@reports_path)
    FileUtils.mkdir_p(@reports_path)
  end
end

Instance Method Details

#after_suites(suites, type)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minitest/reporters/junit_reporter.rb', line 25

def after_suites(suites, type)
  puts "Writing XML reports to #{@reports_path}"
  runner.test_results.each do |suite, tests|
    suite_result = analyze_suite(suite, tests)

    xml = Builder::XmlMarkup.new(:indent => 2)
    xml.instruct!
    xml.testsuite(:name => suite, :skipped => suite_result[:skip_count], :failures => suite_result[:failure_count],
                  :errors => suite_result[:error_count], :tests => suite_result[:test_count],
                  :assertions => suite_result[:assertion_count], :time => suite_result[:time]) do
      tests.each do |test, test_runner|
        xml.testcase(:name => test_runner.test.to_s, :classname => suite, :assertions => test_runner.assertions,
                     :time => test_runner.time) do
          xml << xml_message_for(test_runner) if test_runner.result != :pass
        end
      end
    end
    File.open(filename_for(suite), "w") { |file| file << xml.target! }
  end
end