Class: Beaker::TestSuiteResult
- Inherits:
-
Object
- Object
- Beaker::TestSuiteResult
- Defined in:
- lib/beaker/test_suite_result.rb
Overview
Holds the output of a test suite, formats in plain text or xml
Instance Attribute Summary collapse
-
#start_time ⇒ Object
Returns the value of attribute start_time.
-
#stop_time ⇒ Object
Returns the value of attribute stop_time.
-
#total_tests ⇒ Object
Returns the value of attribute total_tests.
Instance Method Summary collapse
-
#add_test_case(test_case) ⇒ Object
Add a TestCase to this TestSuiteResult instance, used in calculating TestSuiteResult data.
-
#elapsed_time ⇒ Object
The sum of all TestCase runtimes in this TestSuiteResult.
-
#errored_tests ⇒ Object
How many errored TestCase instances are in this TestSuiteResult.
-
#failed? ⇒ Boolean
Did one or more TestCase instances in this TestSuiteResult fail?.
-
#failed_tests ⇒ Object
How many failed TestCase instances are in this TestSuiteResult.
-
#initialize(options, name) ⇒ TestSuiteResult
constructor
Create a TestSuiteResult instance.
-
#passed_tests ⇒ Object
How many passed TestCase instances are in this TestSuiteResult.
-
#pending_tests ⇒ Object
How many pending TestCase instances are in this TestSuiteResult.
-
#persist_test_results(filepath) ⇒ Object
Saves failure and error cases as a JSON file for only-failures processing.
-
#print_test_result(test_case) ⇒ Object
A convenience method for printing the results of a TestCase.
-
#skipped_tests ⇒ Object
How many skipped TestCase instances are in this TestSuiteResult.
-
#success? ⇒ Boolean
Did all the TestCase instances in this TestSuiteResult pass?.
-
#sum_failed ⇒ Object
How many TestCase instances failed in this TestSuiteResult.
-
#summarize(summary_logger) ⇒ Object
Plain text summay of test suite.
-
#test_count ⇒ Object
How many TestCase instances are in this TestSuiteResult.
-
#write_junit_xml(xml_file, file_to_link = nil, time_sort = false) ⇒ Object
private
Writes Junit XML of this TestSuiteResult.
Constructor Details
#initialize(options, name) ⇒ TestSuiteResult
Create a Beaker::TestSuiteResult instance.
16 17 18 19 20 21 22 23 24 |
# File 'lib/beaker/test_suite_result.rb', line 16 def initialize( , name ) @options = @logger = [:logger] @name = name @test_cases = [] #Set some defaults, just in case you attempt to print without including them start_time = Time.at(0) stop_time = Time.at(1) end |
Instance Attribute Details
#start_time ⇒ Object
Returns the value of attribute start_time.
10 11 12 |
# File 'lib/beaker/test_suite_result.rb', line 10 def start_time @start_time end |
#stop_time ⇒ Object
Returns the value of attribute stop_time.
10 11 12 |
# File 'lib/beaker/test_suite_result.rb', line 10 def stop_time @stop_time end |
#total_tests ⇒ Object
Returns the value of attribute total_tests.
10 11 12 |
# File 'lib/beaker/test_suite_result.rb', line 10 def total_tests @total_tests end |
Instance Method Details
#add_test_case(test_case) ⇒ Object
Add a Beaker::TestCase to this Beaker::TestSuiteResult instance, used in calculating Beaker::TestSuiteResult data.
28 29 30 |
# File 'lib/beaker/test_suite_result.rb', line 28 def add_test_case( test_case ) @test_cases << test_case end |
#elapsed_time ⇒ Object
The sum of all Beaker::TestCase runtimes in this Beaker::TestSuiteResult
78 79 80 |
# File 'lib/beaker/test_suite_result.rb', line 78 def elapsed_time @test_cases.inject(0.0) {|r, t| r + t.runtime.to_f } end |
#errored_tests ⇒ Object
How many errored Beaker::TestCase instances are in this Beaker::TestSuiteResult
43 44 45 |
# File 'lib/beaker/test_suite_result.rb', line 43 def errored_tests @test_cases.select { |c| c.test_status == :error }.length end |
#failed? ⇒ Boolean
Did one or more Beaker::TestCase instances in this Beaker::TestSuiteResult fail?
73 74 75 |
# File 'lib/beaker/test_suite_result.rb', line 73 def failed? !success? end |
#failed_tests ⇒ Object
How many failed Beaker::TestCase instances are in this Beaker::TestSuiteResult
48 49 50 |
# File 'lib/beaker/test_suite_result.rb', line 48 def failed_tests @test_cases.select { |c| c.test_status == :fail }.length end |
#passed_tests ⇒ Object
How many passed Beaker::TestCase instances are in this Beaker::TestSuiteResult
38 39 40 |
# File 'lib/beaker/test_suite_result.rb', line 38 def passed_tests @test_cases.select { |c| c.test_status == :pass }.length end |
#pending_tests ⇒ Object
How many pending Beaker::TestCase instances are in this Beaker::TestSuiteResult
58 59 60 |
# File 'lib/beaker/test_suite_result.rb', line 58 def pending_tests @test_cases.select {|c| c.test_status == :pending}.length end |
#persist_test_results(filepath) ⇒ Object
Saves failure and error cases as a JSON file for only-failures processing
157 158 159 160 161 162 |
# File 'lib/beaker/test_suite_result.rb', line 157 def persist_test_results(filepath) return if filepath.empty? results = @test_cases.select { |c| [:fail, :error].include? c.test_status }.map(&:path) File.open(filepath, 'w') { |file| file.puts JSON.dump(results) } end |
#print_test_result(test_case) ⇒ Object
A convenience method for printing the results of a Beaker::TestCase
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/beaker/test_suite_result.rb', line 137 def print_test_result(test_case) if test_case.exception test_file_trace = "" test_case.exception.backtrace.each do |line| if line.include?(test_case.path) test_file_trace = "\r\n Test line: #{line}" break end end if test_case.exception.backtrace && test_case.path test_reported = "reported: #{test_case.exception.inspect}#{test_file_trace}" else test_case.test_status end " Test Case #{test_case.path} #{test_reported}" end |
#skipped_tests ⇒ Object
How many skipped Beaker::TestCase instances are in this Beaker::TestSuiteResult
53 54 55 |
# File 'lib/beaker/test_suite_result.rb', line 53 def skipped_tests @test_cases.select { |c| c.test_status == :skip }.length end |
#success? ⇒ Boolean
Did all the Beaker::TestCase instances in this Beaker::TestSuiteResult pass?
68 69 70 |
# File 'lib/beaker/test_suite_result.rb', line 68 def success? sum_failed == 0 end |
#sum_failed ⇒ Object
How many Beaker::TestCase instances failed in this Beaker::TestSuiteResult
63 64 65 |
# File 'lib/beaker/test_suite_result.rb', line 63 def sum_failed failed_tests + errored_tests end |
#summarize(summary_logger) ⇒ Object
Plain text summay of test suite
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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/beaker/test_suite_result.rb', line 84 def summarize(summary_logger) summary_logger.notify <<-HEREDOC Test Suite: #{@name} @ #{start_time} - Host Configuration Summary - HEREDOC average_test_time = elapsed_time / test_count summary_logger.notify %Q[ - Test Case Summary for suite '#{@name}' - Total Suite Time: %.2f seconds Average Test Time: %.2f seconds Attempted: #{test_count} Passed: #{passed_tests} Failed: #{failed_tests} Errored: #{errored_tests} Skipped: #{skipped_tests} Pending: #{pending_tests} Total: #{@total_tests} - Specific Test Case Status - ] % [elapsed_time, average_test_time] grouped_summary = @test_cases.group_by{|test_case| test_case.test_status } summary_logger.notify "Failed Tests Cases:" (grouped_summary[:fail] || []).each do |test_case| summary_logger.notify print_test_result(test_case) end summary_logger.notify "Errored Tests Cases:" (grouped_summary[:error] || []).each do |test_case| summary_logger.notify print_test_result(test_case) end summary_logger.notify "Skipped Tests Cases:" (grouped_summary[:skip] || []).each do |test_case| summary_logger.notify print_test_result(test_case) end summary_logger.notify "Pending Tests Cases:" (grouped_summary[:pending] || []).each do |test_case| summary_logger.notify print_test_result(test_case) end summary_logger.notify("\n\n") end |
#test_count ⇒ Object
How many Beaker::TestCase instances are in this Beaker::TestSuiteResult
33 34 35 |
# File 'lib/beaker/test_suite_result.rb', line 33 def test_count @test_cases.length end |
#write_junit_xml(xml_file, file_to_link = nil, time_sort = false) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Writes Junit XML of this Beaker::TestSuiteResult
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/beaker/test_suite_result.rb', line 177 def write_junit_xml(xml_file, file_to_link = nil, time_sort = false) stylesheet = File.join(@options[:project_root], @options[:xml_stylesheet]) begin LoggerJunit.write_xml(xml_file, stylesheet) do |doc, suites| = suites.add_element(REXML::Element.new('meta_test_info')) unless file_to_link.nil? time_sort ? .add_attribute('page_active', 'performance') : .add_attribute('page_active', 'execution') .add_attribute('link_url', file_to_link) else .add_attribute('page_active', 'no-links') .add_attribute('link_url', '') end suite = suites.add_element(REXML::Element.new('testsuite')) suite.add_attributes( [ ['name' , @name], ['tests', test_count], ['errors', errored_tests], ['failures', failed_tests], ['skipped', skipped_tests], ['pending', pending_tests], ['total', @total_tests], ['time', "%f" % (stop_time - start_time)] ]) properties = suite.add_element(REXML::Element.new('properties')) @options.each_pair do |name,value| property = properties.add_element(REXML::Element.new('property')) property.add_attributes([['name', name], ['value', value.to_s || '']]) end test_cases_to_report = @test_cases test_cases_to_report = @test_cases.sort { |x,y| y.runtime <=> x.runtime } if time_sort test_cases_to_report.each do |test| item = suite.add_element(REXML::Element.new('testcase')) item.add_attributes( [ ['classname', File.dirname(test.path)], ['name', File.basename(test.path)], ['time', "%f" % test.runtime] ]) test.exports.each do |export| export.keys.each do |key| item.add_attribute(key.to_s.tr(" ", "_"), export[key]) end end #Report failures if test.test_status == :fail || test.test_status == :error status = item.add_element(REXML::Element.new('failure')) status.add_attribute('type', test.test_status.to_s) if test.exception status.add_attribute('message', test.exception.to_s.gsub(/\e/,'')) data = LoggerJunit.format_cdata(test.exception.backtrace.join('\n')) REXML::CData.new(data, true, status) end end if test.test_status == :skip status = item.add_element(REXML::Element.new('skipped')) status.add_attribute('type', test.test_status.to_s) end if test.test_status == :pending status = item.add_element(REXML::Element.new('pending')) status.add_attribute('type', test.test_status.to_s) end if test.sublog stdout = item.add_element(REXML::Element.new('system-out')) data = LoggerJunit.format_cdata(test.sublog) REXML::CData.new(data, true, stdout) end if test.last_result and test.last_result.stderr and not test.last_result.stderr.empty? stderr = item.add_element('system-err') data = LoggerJunit.format_cdata(test.last_result.stderr) REXML::CData.new(data, true, stderr) end end end rescue Exception => e @logger.error "failure in XML output: \n#{e.to_s}" + e.backtrace.join("\n") end end |