Class: Danger::DangerJunit
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerJunit
- Defined in:
- lib/junit/plugin.rb
Overview
Report, or inspect any JUnit XML formatted test suite report.
Testing frameworks have standardized on the JUnit XML format for reporting results, this means that projects using Rspec, Jasmine, Mocha, XCTest and more - can all use the same Danger error reporting. Perfect.
You can see some examples on [this page from Circle CI](circleci.com/docs/test-metadata/) and on this [project’s README](github.com/orta/danger-junit.git) about how you can add JUnit XML output for your testing projects.
Instance Attribute Summary collapse
-
#errors ⇒ Array<Ox::Element>
An array of XML elements that represent passed tests.
-
#failures ⇒ Array<Ox::Element>
An array of XML elements that represent failed tests.
-
#headers ⇒ Array<Symbol>
An array of symbols that become the columns of your tests, if ‘nil`, the default, it will be all of the attributes.
-
#passes ⇒ Array<Ox::Element>
An array of XML elements that represent passed tests.
-
#show_skipped_tests ⇒ Bool
An attribute to make the plugin show a warning on skipped tests.
-
#skipped ⇒ Array<Ox::Element>
An array of XML elements that represent skipped tests.
-
#tests ⇒ Array<Ox::Element>
All the tests for introspection.
Instance Method Summary collapse
-
#parse(file) ⇒ void
Parses an XML file, which fills all the attributes will ‘raise` for errors.
-
#report ⇒ void
Causes a build fail if there are test failures, and outputs a markdown table of the results.
Instance Attribute Details
#errors ⇒ Array<Ox::Element>
An array of XML elements that represent passed tests.
65 66 67 |
# File 'lib/junit/plugin.rb', line 65 def errors @errors end |
#failures ⇒ Array<Ox::Element>
An array of XML elements that represent failed tests.
60 61 62 |
# File 'lib/junit/plugin.rb', line 60 def failures @failures end |
#headers ⇒ Array<Symbol>
An array of symbols that become the columns of your tests, if ‘nil`, the default, it will be all of the attributes.
81 82 83 |
# File 'lib/junit/plugin.rb', line 81 def headers @headers end |
#passes ⇒ Array<Ox::Element>
An array of XML elements that represent passed tests.
55 56 57 |
# File 'lib/junit/plugin.rb', line 55 def passes @passes end |
#show_skipped_tests ⇒ Bool
An attribute to make the plugin show a warning on skipped tests.
75 76 77 |
# File 'lib/junit/plugin.rb', line 75 def show_skipped_tests @show_skipped_tests end |
#skipped ⇒ Array<Ox::Element>
An array of XML elements that represent skipped tests.
70 71 72 |
# File 'lib/junit/plugin.rb', line 70 def skipped @skipped end |
#tests ⇒ Array<Ox::Element>
All the tests for introspection
50 51 52 |
# File 'lib/junit/plugin.rb', line 50 def tests @tests end |
Instance Method Details
#parse(file) ⇒ void
This method returns an undefined value.
Parses an XML file, which fills all the attributes will ‘raise` for errors
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/junit/plugin.rb', line 86 def parse(file) require 'ox' raise "No Junit file was found at #{file}" unless File.exist? file xml_string = File.read file @doc = Ox.parse xml_string suite_root = @doc.nodes.first.value == 'testsuites' ? @doc.nodes.first : @doc @tests = suite_root.nodes.map(&:nodes).flatten.select { |node| node.value == 'testcase' } failed_suites = suite_root.nodes.select { |suite| suite[:failures].to_i > 0 || suite[:errors].to_i > 0 } failed_tests = failed_suites.map(&:nodes).flatten.select { |node| node.value == 'testcase' } @failures = failed_tests.select { |test| test.nodes.count > 0 }.select { |test| test.nodes.first.value == 'failure' } @errors = failed_tests.select { |test| test.nodes.count > 0 }.select { |test| test.nodes.first.value == 'error' } @skipped = tests.select { |test| test.nodes.count > 0 }.select { |test| test.nodes.first.value == 'skipped' } @passes = tests - @failures - @errors - @skipped end |
#report ⇒ void
This method returns an undefined value.
Causes a build fail if there are test failures, and outputs a markdown table of the results.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/junit/plugin.rb', line 110 def report warn("Skipped #{skipped.count} tests.") if show_skipped_tests && skipped.count > 0 unless failures.empty? && errors.empty? fail("Tests have failed, see below for more information.", sticky: false) = "### Tests: \n\n" tests = (failures + errors) keys = headers || tests.first.attributes.keys attributes = keys.map(&:to_s).map(&:capitalize) # Create the headers << attributes.join(' | ') + "|\n" << attributes.map { |_| '---' }.join(' | ') + "|\n" # Map out the keys to the tests tests.each do |test| row_values = keys.map { |key| test.attributes[key] }.map { |v| auto_link(v) } << row_values.join(' | ') + "|\n" end markdown end end |