Class: Beaker::TestSuite::TestSuiteResult
- Inherits:
-
Object
- Object
- Beaker::TestSuite::TestSuiteResult
- Defined in:
- lib/beaker/test_suite.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 Beaker::TestCase to this TestSuiteResult instance, used in calculating TestSuiteResult data.
-
#elapsed_time ⇒ Object
The sum of all Beaker::TestCase runtimes in this TestSuiteResult.
-
#errored_tests ⇒ Object
How many errored Beaker::TestCase instances are in this TestSuiteResult.
-
#escape_invalid_xml_chars(string) ⇒ Object
Escape invalid XML UTF-8 codes from provided string, see www.w3.org/TR/xml/#charsets for valid character specification.
-
#failed? ⇒ Boolean
Did one or more Beaker::TestCase instances in this TestSuiteResult fail?.
-
#failed_tests ⇒ Object
How many failed Beaker::TestCase instances are in this TestSuiteResult.
-
#format_cdata(string) ⇒ Object
Remove color codes and invalid XML characters from provided string.
-
#initialize(options, name) ⇒ TestSuiteResult
constructor
Create a TestSuiteResult instance.
-
#is_valid_xml(int) ⇒ Boolean
Determine if the provided number falls in the range of accepted xml unicode values See www.w3.org/TR/xml/#charsets for valid for valid character specifications.
-
#passed_tests ⇒ Object
How many passed Beaker::TestCase instances are in this TestSuiteResult.
-
#pending_tests ⇒ Object
How many pending Beaker::TestCase instances are in this TestSuiteResult.
-
#print_test_result(test_case) ⇒ Object
A convenience method for printing the results of a Beaker::TestCase.
-
#skipped_tests ⇒ Object
How many skipped Beaker::TestCase instances are in this TestSuiteResult.
-
#strip_color_codes(text) ⇒ String
Remove color codes from provided string.
-
#success? ⇒ Boolean
Did all the Beaker::TestCase instances in this TestSuiteResult pass?.
-
#sum_failed ⇒ Object
How many Beaker::TestCase instances failed in this TestSuiteResult.
-
#summarize(summary_logger) ⇒ Object
Plain text summay of test suite.
-
#test_count ⇒ Object
How many Beaker::TestCase instances are in this TestSuiteResult.
-
#write_junit_xml(xml_file, stylesheet) ⇒ Object
Format and print the TestSuiteResult as JUnit XML.
Constructor Details
#initialize(options, name) ⇒ TestSuiteResult
Create a Beaker::TestSuite::TestSuiteResult instance.
21 22 23 24 25 26 27 28 29 |
# File 'lib/beaker/test_suite.rb', line 21 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.
15 16 17 |
# File 'lib/beaker/test_suite.rb', line 15 def start_time @start_time end |
#stop_time ⇒ Object
Returns the value of attribute stop_time.
15 16 17 |
# File 'lib/beaker/test_suite.rb', line 15 def stop_time @stop_time end |
#total_tests ⇒ Object
Returns the value of attribute total_tests.
15 16 17 |
# File 'lib/beaker/test_suite.rb', line 15 def total_tests @total_tests end |
Instance Method Details
#add_test_case(test_case) ⇒ Object
Add a Beaker::TestCase to this Beaker::TestSuite::TestSuiteResult instance, used in calculating Beaker::TestSuite::TestSuiteResult data.
33 34 35 |
# File 'lib/beaker/test_suite.rb', line 33 def add_test_case( test_case ) @test_cases << test_case end |
#elapsed_time ⇒ Object
The sum of all Beaker::TestCase runtimes in this Beaker::TestSuite::TestSuiteResult
83 84 85 |
# File 'lib/beaker/test_suite.rb', line 83 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::TestSuite::TestSuiteResult
48 49 50 |
# File 'lib/beaker/test_suite.rb', line 48 def errored_tests @test_cases.select { |c| c.test_status == :error }.length end |
#escape_invalid_xml_chars(string) ⇒ Object
Escape invalid XML UTF-8 codes from provided string, see www.w3.org/TR/xml/#charsets for valid character specification
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/beaker/test_suite.rb', line 174 def escape_invalid_xml_chars string escaped_string = "" string.chars.each do |i| char_as_codestring = i.unpack("U*").join if is_valid_xml(char_as_codestring.to_i) escaped_string << i else escaped_string << "\\#{char_as_codestring}" end end escaped_string end |
#failed? ⇒ Boolean
Did one or more Beaker::TestCase instances in this Beaker::TestSuite::TestSuiteResult fail?
78 79 80 |
# File 'lib/beaker/test_suite.rb', line 78 def failed? !success? end |
#failed_tests ⇒ Object
How many failed Beaker::TestCase instances are in this Beaker::TestSuite::TestSuiteResult
53 54 55 |
# File 'lib/beaker/test_suite.rb', line 53 def failed_tests @test_cases.select { |c| c.test_status == :fail }.length end |
#format_cdata(string) ⇒ Object
Remove color codes and invalid XML characters from provided string
189 190 191 |
# File 'lib/beaker/test_suite.rb', line 189 def format_cdata string escape_invalid_xml_chars(strip_color_codes(string)) end |
#is_valid_xml(int) ⇒ Boolean
Determine if the provided number falls in the range of accepted xml unicode values See www.w3.org/TR/xml/#charsets for valid for valid character specifications.
162 163 164 165 166 167 168 169 |
# File 'lib/beaker/test_suite.rb', line 162 def is_valid_xml(int) return ( int == 0x9 or int == 0xA or ( int >= 0x0020 and int <= 0xD7FF ) or ( int >= 0xE000 and int <= 0xFFFD ) or ( int >= 0x100000 and int <= 0x10FFFF ) ) end |
#passed_tests ⇒ Object
How many passed Beaker::TestCase instances are in this Beaker::TestSuite::TestSuiteResult
43 44 45 |
# File 'lib/beaker/test_suite.rb', line 43 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::TestSuite::TestSuiteResult
63 64 65 |
# File 'lib/beaker/test_suite.rb', line 63 def pending_tests @test_cases.select {|c| c.test_status == :pending}.length end |
#print_test_result(test_case) ⇒ Object
A convenience method for printing the results of a Beaker::TestCase
142 143 144 145 146 147 148 149 |
# File 'lib/beaker/test_suite.rb', line 142 def print_test_result(test_case) test_reported = if test_case.exception "reported: #{test_case.exception.inspect}" else test_case.test_status end @logger.notify " Test Case #{test_case.path} #{test_reported}" end |
#skipped_tests ⇒ Object
How many skipped Beaker::TestCase instances are in this Beaker::TestSuite::TestSuiteResult
58 59 60 |
# File 'lib/beaker/test_suite.rb', line 58 def skipped_tests @test_cases.select { |c| c.test_status == :skip }.length end |
#strip_color_codes(text) ⇒ String
Remove color codes from provided string. Color codes are of the format /(e[dd;ddm)+/.
154 155 156 |
# File 'lib/beaker/test_suite.rb', line 154 def strip_color_codes(text) text.gsub(/(\e|\^\[)\[(\d*;)*\d*m/, '') end |
#success? ⇒ Boolean
Did all the Beaker::TestCase instances in this Beaker::TestSuite::TestSuiteResult pass?
73 74 75 |
# File 'lib/beaker/test_suite.rb', line 73 def success? sum_failed == 0 end |
#sum_failed ⇒ Object
How many Beaker::TestCase instances failed in this Beaker::TestSuite::TestSuiteResult
68 69 70 |
# File 'lib/beaker/test_suite.rb', line 68 def sum_failed failed_tests + errored_tests end |
#summarize(summary_logger) ⇒ Object
Plain text summay of test suite
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 134 135 136 137 138 |
# File 'lib/beaker/test_suite.rb', line 89 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| print_test_result(test_case) end summary_logger.notify "Errored Tests Cases:" (grouped_summary[:error] || []).each do |test_case| print_test_result(test_case) end summary_logger.notify "Skipped Tests Cases:" (grouped_summary[:skip] || []).each do |test_case| print_test_result(test_case) end summary_logger.notify "Pending Tests Cases:" (grouped_summary[:pending] || []).each do |test_case| print_test_result(test_case) end summary_logger.notify("\n\n") end |
#test_count ⇒ Object
How many Beaker::TestCase instances are in this Beaker::TestSuite::TestSuiteResult
38 39 40 |
# File 'lib/beaker/test_suite.rb', line 38 def test_count @test_cases.length end |
#write_junit_xml(xml_file, stylesheet) ⇒ Object
Format and print the Beaker::TestSuite::TestSuiteResult as JUnit XML
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/beaker/test_suite.rb', line 196 def write_junit_xml(xml_file, stylesheet) begin #copy stylesheet into xml directory if not File.file?(File.join(File.dirname(xml_file), File.basename(stylesheet))) FileUtils.copy(stylesheet, File.join(File.dirname(xml_file), File.basename(stylesheet))) end suites = nil #check to see if an output file already exists, if it does add or replace test suite data if File.file?(xml_file) doc = Nokogiri::XML( File.open(xml_file, 'r') ) suites = doc.at_xpath('testsuites') #remove old data doc.search("//testsuite").each do |node| if node['name'] =~ /#{@name}/ node.unlink end end else #no existing file, create a new one doc = Nokogiri::XML::Document.new() doc.encoding = 'UTF-8' pi = Nokogiri::XML::ProcessingInstruction.new(doc, "xml-stylesheet", "type=\"text/xsl\" href=\"#{File.basename(stylesheet)}\"") pi.parent = doc suites = Nokogiri::XML::Node.new('testsuites', doc) suites.parent = doc end suite = Nokogiri::XML::Node.new('testsuite', doc) suite['name'] = @name suite['tests'] = test_count suite['errors'] = errored_tests suite['failures'] = failed_tests suite['skip'] = skipped_tests suite['pending'] = pending_tests suite['total'] = @total_tests suite['time'] = "%f" % (stop_time - start_time) properties = Nokogiri::XML::Node.new('properties', doc) @options.each_pair do | name, value | property = Nokogiri::XML::Node.new('property', doc) property['name'] = name property['value'] = value properties.add_child(property) end suite.add_child(properties) @test_cases.each do |test| item = Nokogiri::XML::Node.new('testcase', doc) item['classname'] = File.dirname(test.path) item['name'] = File.basename(test.path) item['time'] = "%f" % test.runtime # Did we fail? If so, report that. # We need to remove the escape character from colorized text, the # substitution of other entities is handled well by Rexml if test.test_status == :fail || test.test_status == :error then status = Nokogiri::XML::Node.new('failure', doc) status['type'] = test.test_status.to_s if test.exception then status['message'] = test.exception.to_s.gsub(/\e/, '') data = format_cdata(test.exception.backtrace.join('\n')) status.add_child(status.document.create_cdata(data)) end item.add_child(status) end if test.test_status == :skip status = Nokogiri::XML::Node.new('skip', doc) status['type'] = test.test_status.to_s item.add_child(status) end if test.test_status == :pending status = Nokogiri::XML::Node.new('pending', doc) status['type'] = test.test_status.to_s item.add_child(status) end if test.sublog then stdout = Nokogiri::XML::Node.new('system-out', doc) data = format_cdata(test.sublog) stdout.add_child(stdout.document.create_cdata(data)) item.add_child(stdout) end if test.last_result and test.last_result.stderr and not test.last_result.stderr.empty? then stderr = Nokogiri::XML::Node.new('system-err', doc) data = format_cdata(test.last_result.stderr) stderr.add_child(stderr.document.create_cdata(data)) item.add_child(stderr) end suite.add_child(item) end suites.add_child(suite) # junit/name.xml will be created in a directory relative to the CWD # -- JLS 2/12 File.open(xml_file, 'w') { |fh| fh.write(doc.to_xml) } rescue Exception => e @logger.error "failure in XML output:\n#{e.to_s}\n" + e.backtrace.join("\n") end end |