From b907469f9dfa34ad3115fce446da5c05b1ddcccf Mon Sep 17 00:00:00 2001 From: Jens Bissinger Date: Sat, 18 May 2013 11:39:21 +0200 Subject: [PATCH 1/4] Move lib/yarjuf.rb to lib/j_unit.rb for rspec auto loading without requiring yarjuf gem and keep a simple pointer in lib/yarjuf.rb. --- lib/j_unit.rb | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/yarjuf.rb | 115 +------------------------------------------------- 2 files changed, 115 insertions(+), 114 deletions(-) create mode 100644 lib/j_unit.rb diff --git a/lib/j_unit.rb b/lib/j_unit.rb new file mode 100644 index 0000000..2041a83 --- /dev/null +++ b/lib/j_unit.rb @@ -0,0 +1,114 @@ +require 'time' +require 'builder' +require 'rspec/core/formatters/base_formatter' + +#An RSpec formatter for generating results in JUnit format +class JUnit < RSpec::Core::Formatters::BaseFormatter + + #rspec formatter methods we care about + + def initialize(output) + super output + @test_suite_results = {} + @builder = Builder::XmlMarkup.new :indent => 2 + end + + def example_passed(example) + add_to_test_suite_results example + end + + def example_failed(example) + add_to_test_suite_results example + end + + def example_pending(example) + add_to_test_suite_results example + end + + def dump_summary(duration, example_count, failure_count, pending_count) + build_results duration, example_count, failure_count, pending_count + output.puts @builder.target! + end + + protected + + def add_to_test_suite_results(example) + suite_name = JUnit.root_group_name_for example + @test_suite_results[suite_name] = [] unless @test_suite_results.keys.include? suite_name + @test_suite_results[suite_name] << example + end + + def failure_details_for(example) + exception = example.metadata[:execution_result][:exception] + exception.nil? ? "" : "#{exception.message}\n#{format_backtrace(exception.backtrace, example).join("\n")}" + end + + #utility methods + + def self.count_in_suite_of_type(suite, test_case_result_type) + suite.select {|example| example.metadata[:execution_result][:status] == test_case_result_type}.size + end + + def self.root_group_name_for(example) + group_hierarchy = [] + current_example_group = example.metadata[:example_group] + until current_example_group.nil? do + group_hierarchy.unshift current_example_group + current_example_group = current_example_group[:example_group] + end + group_hierarchy.first[:description] + end + + #methods to build the xml for test suites and individual tests + + def build_results(duration, example_count, failure_count, pending_count) + @builder.instruct! :xml, :version => "1.0", :encoding => "UTF-8" + @builder.testsuites :errors => 0, :failures => failure_count, :skipped => pending_count, :tests => example_count, :time => duration, :timestamp => Time.now.iso8601 do + build_all_suites + end + end + + def build_all_suites + @test_suite_results.each do |suite_name, tests| + build_test_suite suite_name, tests + end + end + + def build_test_suite(suite_name, tests) + failure_count = JUnit.count_in_suite_of_type tests, "failed" + skipped_count = JUnit.count_in_suite_of_type tests, "pending" + + @builder.testsuite :name => suite_name, :tests => tests.size, :errors => 0, :failures => failure_count, :skipped => skipped_count do + @builder.properties + build_all_tests tests + end + end + + def build_all_tests(tests) + tests.each do |test| + build_test test + end + end + + def build_test(test) + test_name = test.metadata[:full_description] + execution_time = test.metadata[:execution_result][:run_time] + test_status = test.metadata[:execution_result][:status] + + @builder.testcase :name => test_name, :time => execution_time do + case test_status + when "pending" then @builder.skipped + when "failed" then build_failed_test test + end + end + end + + def build_failed_test(test) + failure_message = "failed #{test.metadata[:full_description]}" + + @builder.failure :message => failure_message, :type => "failed" do + @builder.cdata! failure_details_for test + end + end +end + diff --git a/lib/yarjuf.rb b/lib/yarjuf.rb index 2041a83..f911f24 100644 --- a/lib/yarjuf.rb +++ b/lib/yarjuf.rb @@ -1,114 +1 @@ -require 'time' -require 'builder' -require 'rspec/core/formatters/base_formatter' - -#An RSpec formatter for generating results in JUnit format -class JUnit < RSpec::Core::Formatters::BaseFormatter - - #rspec formatter methods we care about - - def initialize(output) - super output - @test_suite_results = {} - @builder = Builder::XmlMarkup.new :indent => 2 - end - - def example_passed(example) - add_to_test_suite_results example - end - - def example_failed(example) - add_to_test_suite_results example - end - - def example_pending(example) - add_to_test_suite_results example - end - - def dump_summary(duration, example_count, failure_count, pending_count) - build_results duration, example_count, failure_count, pending_count - output.puts @builder.target! - end - - protected - - def add_to_test_suite_results(example) - suite_name = JUnit.root_group_name_for example - @test_suite_results[suite_name] = [] unless @test_suite_results.keys.include? suite_name - @test_suite_results[suite_name] << example - end - - def failure_details_for(example) - exception = example.metadata[:execution_result][:exception] - exception.nil? ? "" : "#{exception.message}\n#{format_backtrace(exception.backtrace, example).join("\n")}" - end - - #utility methods - - def self.count_in_suite_of_type(suite, test_case_result_type) - suite.select {|example| example.metadata[:execution_result][:status] == test_case_result_type}.size - end - - def self.root_group_name_for(example) - group_hierarchy = [] - current_example_group = example.metadata[:example_group] - until current_example_group.nil? do - group_hierarchy.unshift current_example_group - current_example_group = current_example_group[:example_group] - end - group_hierarchy.first[:description] - end - - #methods to build the xml for test suites and individual tests - - def build_results(duration, example_count, failure_count, pending_count) - @builder.instruct! :xml, :version => "1.0", :encoding => "UTF-8" - @builder.testsuites :errors => 0, :failures => failure_count, :skipped => pending_count, :tests => example_count, :time => duration, :timestamp => Time.now.iso8601 do - build_all_suites - end - end - - def build_all_suites - @test_suite_results.each do |suite_name, tests| - build_test_suite suite_name, tests - end - end - - def build_test_suite(suite_name, tests) - failure_count = JUnit.count_in_suite_of_type tests, "failed" - skipped_count = JUnit.count_in_suite_of_type tests, "pending" - - @builder.testsuite :name => suite_name, :tests => tests.size, :errors => 0, :failures => failure_count, :skipped => skipped_count do - @builder.properties - build_all_tests tests - end - end - - def build_all_tests(tests) - tests.each do |test| - build_test test - end - end - - def build_test(test) - test_name = test.metadata[:full_description] - execution_time = test.metadata[:execution_result][:run_time] - test_status = test.metadata[:execution_result][:status] - - @builder.testcase :name => test_name, :time => execution_time do - case test_status - when "pending" then @builder.skipped - when "failed" then build_failed_test test - end - end - end - - def build_failed_test(test) - failure_message = "failed #{test.metadata[:full_description]}" - - @builder.failure :message => failure_message, :type => "failed" do - @builder.cdata! failure_details_for test - end - end -end - +require 'j_unit' From 46b682e97c58ee5f550c8685fb7695c553a0f1a4 Mon Sep 17 00:00:00 2001 From: Jens Bissinger Date: Sun, 19 May 2013 11:52:45 +0200 Subject: [PATCH 2/4] Fix Gemfile source --- Gemfile | 2 +- Gemfile.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 1b6623f..cbfc93f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source :rubygems +source 'https://rubygems.org' gem "cucumber" gem "rspec" diff --git a/Gemfile.lock b/Gemfile.lock index 4859911..b87e911 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,5 +1,5 @@ GEM - remote: http://rubygems.org/ + remote: https://rubygems.org/ specs: aruba (0.5.1) childprocess (~> 0.3.6) From 5e9fc0b81e94b5f11f66ddcf1ada5c80ccf2b988 Mon Sep 17 00:00:00 2001 From: Jens Bissinger Date: Sun, 19 May 2013 11:52:52 +0200 Subject: [PATCH 3/4] Update dependencies --- Gemfile.lock | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b87e911..58648d0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -13,7 +13,7 @@ GEM diff-lcs (>= 1.1.3) gherkin (~> 2.11.0) json (>= 1.4.6) - diff-lcs (1.1.3) + diff-lcs (1.2.4) ffi (1.2.0) gherkin (2.11.5) json (>= 1.4.6) @@ -27,14 +27,14 @@ GEM sexp_processor (~> 3.0) ripper_ruby_parser (0.0.8) sexp_processor (~> 3.0) - rspec (2.12.0) - rspec-core (~> 2.12.0) - rspec-expectations (~> 2.12.0) - rspec-mocks (~> 2.12.0) - rspec-core (2.12.1) - rspec-expectations (2.12.0) - diff-lcs (~> 1.1.3) - rspec-mocks (2.12.0) + rspec (2.13.0) + rspec-core (~> 2.13.0) + rspec-expectations (~> 2.13.0) + rspec-mocks (~> 2.13.0) + rspec-core (2.13.1) + rspec-expectations (2.13.0) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.13.1) ruby2ruby (1.2.5) ruby_parser (~> 2.0) sexp_processor (~> 3.0) From cd7f75a395f37667037bd0d5c59ce4c7c7a05945 Mon Sep 17 00:00:00 2001 From: Jens Bissinger Date: Sat, 25 May 2013 00:46:58 +0200 Subject: [PATCH 4/4] Fix loading j_unit file (e.g. for running cucumber features) --- lib/yarjuf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yarjuf.rb b/lib/yarjuf.rb index f911f24..f928052 100644 --- a/lib/yarjuf.rb +++ b/lib/yarjuf.rb @@ -1 +1 @@ -require 'j_unit' +require File.join(File.dirname(__FILE__), 'j_unit')