Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lib/j_unit.rb to be able to omit the require 'yarjuf' statements #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source 'https://rubygems.org'

gem "cucumber"
gem "rspec"
Expand Down
20 changes: 10 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
aruba (0.5.1)
childprocess (~> 0.3.6)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
114 changes: 114 additions & 0 deletions lib/j_unit.rb
Original file line number Diff line number Diff line change
@@ -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

115 changes: 1 addition & 114 deletions lib/yarjuf.rb
Original file line number Diff line number Diff line change
@@ -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 File.join(File.dirname(__FILE__), 'j_unit')