Class: Danger::DangerJunitResults

Inherits:
Plugin
  • Object
show all
Defined in:
lib/junit_results/plugin.rb

Overview

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

Examples:

Ensure all the tests are executed correctly


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

See Also:

  • valeriomazzeo/danger-junit_results

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ DangerJunitResults

Returns a new instance of DangerJunitResults.



15
16
17
18
19
20
21
22
# File 'lib/junit_results/plugin.rb', line 15

def initialize(arg)
    super
    @total_count = 0
    @skipped_count = 0
    @executed_count = 0
    @failed_count = 0
    @retried_count = 0
end

Instance Attribute Details

#executed_countexecuted_count (readonly)

Total number of tests executed.

Returns:



37
38
39
# File 'lib/junit_results/plugin.rb', line 37

def executed_count
  @executed_count
end

#failed_countfailed_count (readonly)

Total number of tests failed.

Returns:



42
43
44
# File 'lib/junit_results/plugin.rb', line 42

def failed_count
  @failed_count
end

#failuresArray<Nokogiri::XML::Element> (readonly)

An array of XML elements of the failed tests.

Returns:

  • (Array<Nokogiri::XML::Element>)


52
53
54
# File 'lib/junit_results/plugin.rb', line 52

def failures
  @failures
end

#retried_countretried_count (readonly)

Total number of tests retried.

Returns:



47
48
49
# File 'lib/junit_results/plugin.rb', line 47

def retried_count
  @retried_count
end

#skipped_countskipped_count (readonly)

Total number of tests skipped.

Returns:



32
33
34
# File 'lib/junit_results/plugin.rb', line 32

def skipped_count
  @skipped_count
end

#total_counttotal_count (readonly)

Total number of tests.

Returns:



27
28
29
# File 'lib/junit_results/plugin.rb', line 27

def total_count
  @total_count
end

Class Method Details

.instance_nameObject



94
95
96
# File 'lib/junit_results/plugin.rb', line 94

def self.instance_name
  to_s.gsub("Danger", "").danger_underscore.split("/").last
end

Instance Method Details

#parse(file_path) ⇒ success

Parses tests.

Returns:

  • (success)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/junit_results/plugin.rb', line 57

def parse(file_path)
  require 'nokogiri'

  @doc = Nokogiri::XML(File.open(file_path))
  groupedByName = @doc.xpath('//testcase').group_by { |x| "#{x.attr('classname')}.#{x.attr('name')}" }
  @retried_count = groupedByName.reduce(0) { |sum, (key, value)| sum + (value.count - 1) }
  @total_count = @doc.xpath('//testsuite').map { |x| x.attr('tests').to_i }.inject(0){ |sum, x| sum + x } - @retried_count
  @skipped_count = @doc.xpath('//testsuite').map { |x| x.attr('skipped').to_i }.inject(0){ |sum, x| sum + x }
  @executed_count = @total_count - @skipped_count
  @failures = groupedByName.map { |(key, value)| value.last().xpath('failure') }.select { |x| !x.empty?() }.flatten
  @failed_count = @failures.count

  return @failed_count <= 0
end

#reportsuccess

Prints a detailed report of the tests failures.

Returns:

  • (success)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/junit_results/plugin.rb', line 75

def report
  tests_executed_string = @executed_count == 1 ? "test" : "tests"
  tests_failed_string = @failed_count == 1 ? "failure" : "failures"
  tests_retried_string = @retried_count == 1 ? "retry" : "retries"

  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
  elsif @retried_count > 0
    message("Executed #{@executed_count}(#{@total_count}) #{tests_executed_string}, with #{@failed_count} #{tests_failed_string} and #{@retried_count} #{tests_retried_string} 🎉")
  else
    message("Executed #{@executed_count}(#{@total_count}) #{tests_executed_string}, with #{@failed_count} #{tests_failed_string} 🎉")
  end

  return @failed_count <= 0
end