class Danger::DangerJunitResults

Exposes test results summary with detailed failures, given a path to a JUnit report file.

@example Ensure all the tests are executed correctly

junit_results.parse("/tmp/junit-report.xml")
junit.report

@see valeriomazzeo/danger-junit_results @tags junit, tests

Attributes

executed_count[R]

Total number of tests executed.

@return [executed_count]

failed_count[R]

Total number of tests failed.

@return [failed_count]

failures[R]

An array of XML elements of the failed tests.

@return [Array<Nokogiri::XML::Element>]

skipped_count[R]

Total number of tests skipped.

@return [skipped_count]

total_count[R]

Total number of tests.

@return [total_count]

Public Class Methods

instance_name() click to toggle source
# File lib/junit_results/plugin.rb, line 84
def self.instance_name
  to_s.gsub("Danger", "").danger_underscore.split("/").last
end
new(arg) click to toggle source
Calls superclass method
# File lib/junit_results/plugin.rb, line 15
def initialize(arg)
    super
    @skipped_count = 0
    @executed_count = 0
    @failed_count = 0
end

Public Instance Methods

parse(file_path) click to toggle source

Parses tests. @return [success]

# File lib/junit_results/plugin.rb, line 50
def parse(file_path)
  require 'nokogiri'

  @doc = Nokogiri::XML(File.open(file_path))

  @total_count = @doc.xpath('//testsuite').map { |x| x.attr('tests').to_i }.inject(0){ |sum, x| sum + x }
  @skipped_count = @doc.xpath('//testsuite').map { |x| x.attr('skipped').to_i }.inject(0){ |sum, x| sum + x }
  @executed_count = @total_count - @skipped_count
  @failed_count = @doc.xpath('//testsuite').map { |x| x.attr('failures').to_i }.inject(0){ |sum, x| sum + x }

  @failures = @doc.xpath('//failure')

  return @failed_count <= 0
end
report() click to toggle source

Prints a detailed report of the tests failures. @return [success]

# File lib/junit_results/plugin.rb, line 68
def report
  tests_executed_string = @executed_count == 1 ? "test" : "tests"
  tests_failed_string = @failed_count == 1 ? "failure" : "failures"

  if @failed_count > 0
    fail("Executed #{@executed_count}(#{@total_count}) #{tests_executed_string}, with **#{@failed_count}** #{tests_failed_string} 🚨")
    @failures.each do |failure|
      fail("`[#{failure.content.split("/").last}] [#{failure.parent['name']}] #{failure['message']}`")
    end
  else
    message("Executed #{@executed_count}(#{@total_count}) #{tests_executed_string}, with #{@failed_count} #{tests_failed_string} 🎉")
  end

  return @failed_count <= 0
end