Class: Danger::DangerJunitResults
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerJunitResults
- Defined in:
- lib/junit_results/plugin.rb
Overview
Exposes test results summary with detailed failures, given a path to a JUnit report file.
Instance Attribute Summary collapse
-
#executed_count ⇒ executed_count
readonly
Total number of tests executed.
-
#failed_count ⇒ failed_count
readonly
Total number of tests failed.
-
#failures ⇒ Array<Nokogiri::XML::Element>
readonly
An array of XML elements of the failed tests.
-
#retried_count ⇒ retried_count
readonly
Total number of tests retried.
-
#skipped_count ⇒ skipped_count
readonly
Total number of tests skipped.
-
#total_count ⇒ total_count
readonly
Total number of tests.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(arg) ⇒ DangerJunitResults
constructor
A new instance of DangerJunitResults.
-
#parse(file_path) ⇒ success
Parses tests.
-
#report ⇒ success
Prints a detailed report of the tests failures.
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_count ⇒ executed_count (readonly)
Total number of tests executed.
37 38 39 |
# File 'lib/junit_results/plugin.rb', line 37 def executed_count @executed_count end |
#failed_count ⇒ failed_count (readonly)
Total number of tests failed.
42 43 44 |
# File 'lib/junit_results/plugin.rb', line 42 def failed_count @failed_count end |
#failures ⇒ Array<Nokogiri::XML::Element> (readonly)
An array of XML elements of the failed tests.
52 53 54 |
# File 'lib/junit_results/plugin.rb', line 52 def failures @failures end |
#retried_count ⇒ retried_count (readonly)
Total number of tests retried.
47 48 49 |
# File 'lib/junit_results/plugin.rb', line 47 def retried_count @retried_count end |
#skipped_count ⇒ skipped_count (readonly)
Total number of tests skipped.
32 33 34 |
# File 'lib/junit_results/plugin.rb', line 32 def skipped_count @skipped_count end |
#total_count ⇒ total_count (readonly)
Total number of tests.
27 28 29 |
# File 'lib/junit_results/plugin.rb', line 27 def total_count @total_count end |
Class Method Details
.instance_name ⇒ Object
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.
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 |
#report ⇒ success
Prints a detailed report of the tests failures.
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 ("Executed #{@executed_count}(#{@total_count}) #{tests_executed_string}, with #{@failed_count} #{tests_failed_string} and #{@retried_count} #{tests_retried_string} 🎉") else ("Executed #{@executed_count}(#{@total_count}) #{tests_executed_string}, with #{@failed_count} #{tests_failed_string} 🎉") end return @failed_count <= 0 end |