Hi Dylan,
I had modified the code .. and am not sure where to copy and paste
it ...
Iam including the whole source code for the modified ci reporter -
To use this remove the require for ci_reporter and require the
modified file.
The usage remains the same ....
set the env variable ENV['CI_REPORTS'] = the folder where the reports
need to be stored.
The file also changes the report file slightly to include multiple
test classes that were run.
require 'delegate'
require 'stringio'
require 'fileutils'
require 'test/unit'
require 'test/unit/ui/console/testrunner'
require 'ClassAttr'
module YNOT
module YNOTE
class OutputCapture < DelegateClass(IO)
# Start capturing IO, using the given block to assign self to
the proper IO global.
def initialize(io, &assign)
super
@delegate_io = io
@captured_io = StringIO.new
@assign_block = assign
@assign_block.call self
end
# Finalize the capture and reset to the original IO object.
def finish
@assign_block.call @delegate_io
@captured_io.string
end
# setup tee methods
%w(<< print printf putc puts write).each do |m|
module_eval(<<-EOS, __FILE__, __LINE__)
def #{m}(*args, &block)
@delegate_io.send(:#{m}, *args, &block)
@captured_io.send(:#{m}, *args, &block)
end
EOS
end
end
class ReportManager
@@current_suites = Array.new()
class_attr_accessor(:current_suites)
def initialize(prefix)
@basedir = ENV['CI_REPORTS'] || File.expand_path("#{Dir.getwd}/
#{prefix.downcase}/reports")
@basename = "#{@basedir}/#{prefix.upcase}"
FileUtils.mkdir_p(@basedir)
end
def write_report(suite)
File.open("#{@basename}-#{suite.name.gsub(/[^a-zA-Z0-9]+/,
'-')}.xml", "w") do |f|
f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
f << suite.to_xml
end
end
# will do runtime reporting
def write_newreport_allsuites()
File.open("#{@basename}-#{"MYREPORTFILE".gsub(/[^a-zA-Z0-9]+/,
'-')}.xml", "w") do |f|
f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tests>\n"
ReportManager.current_suites.each { |st|
f << st.to_xml
}
f << "</tests>"
end
end
end
class Failure
def
self.new(fault)
fault.kind_of?(Test::Unit::Failure) ? TestUnitFailure.new
(fault) : TestUnitError.new(fault)
end
end
# Wrapper around a <code>Test::Unit</code> error to be used by the
test suite to interpret results.
class TestUnitError
def initialize(fault)
@fault = fault
end
def failure?() false end
def error?() true end
def name() @
fault.exception.class.name end
def message() @fault.exception.message end
def location() @fault.exception.backtrace.join("\n") end
end
# Wrapper around a <code>Test::Unit</code> failure to be used by
the test suite to interpret results.
class TestUnitFailure
def initialize(fault)
@fault = fault
end
def failure?() true end
def error?() false end
def name() Test::Unit::AssertionFailedError.name end
def message() @fault.message end
def location() @fault.location.join("\n") end
end
# Replacement Mediator that adds listeners to capture the results
of the <code>Test::Unit</code> runs.
class TestUnit < Test::Unit::UI::TestRunnerMediator
def initialize(suite, report_mgr = nil)
super(suite)
@report_manager = report_mgr || ReportManager.new("test")
add_listener(Test::Unit::UI::TestRunnerMediator::STARTED,
&method(:started))
add_listener(Test::Unit::TestCase::STARTED, &method
(:test_started))
add_listener(Test::Unit::TestCase::FINISHED, &method
(:test_finished))
add_listener(Test::Unit::TestResult::FAULT, &method(:fault))
add_listener(Test::Unit::UI::TestRunnerMediator::FINISHED,
&method(:finished))
end
def started(result)
@suite_result = result
@last_assertion_count = 0
@current_suite = nil
@unknown_count = 0
@result_assertion_count = 0
end
def test_started(name)
test_name, suite_name = extract_names(name)
unless @current_suite && @
current_suite.name == suite_name
finish_suite
start_suite(suite_name)
end
start_test(test_name)
end
def test_finished(name)
finish_test
end
def fault(fault)
tc = @current_suite.testcases.last
tc.failures << Failure.new(fault)
end
def finished(elapsed_time)
finish_suite
end
private
def extract_names(name)
match = name.match(/(.*)\(([^)]*)\)/)
if match
[match[1], match[2]]
else
@unknown_count += 1
[name, "unknown-#{@unknown_count}"]
end
end
def start_suite(suite_name)
@current_suite = TestSuite.new(suite_name)
@current_suite.start
ReportManager.current_suites << @current_suite
=begin
@current_suite = TestSuite.new(suite_name)
@current_suite.start
=end
end
def finish_suite
if @current_suite
@current_suite.finish
@current_suite.assertions = @suite_result.assertion_count -
@last_assertion_count
@last_assertion_count = @suite_result.assertion_count
##### remove this after you make the changes
######@report_manager.write_report(@current_suite) #old
report -- creates a xmlfile for each class
################################
#@report_manager.write_newreport(@current_suites)
@report_manager.write_newreport_allsuites()
end
end
def start_test(test_name)
tc = TestCase.new(test_name)
tc.start
@current_suite.testcases << tc
#=begin --- This is written for returning results in runtime.. after
each testcase is run... when running only name will be present
@current_suite.assertions = @suite_result.assertion_count -
@last_assertion_count
@last_assertion_count = @suite_result.assertion_count
#######@report_manager.write_report(@current_suite) #old
report -- creates a xmlfile for each class
@report_manager.write_newreport_allsuites()
#@report_manager.write_newreport_testcaselevel
(@current_suites)
#@report_manager.write_newreport(@current_suites)
#=end
end
def finish_test
tc = @current_suite.testcases.last
tc.finish
tc.assertions = @suite_result.assertion_count -
@result_assertion_count
@result_assertion_count = @suite_result.assertion_count
@report_manager.write_newreport_allsuites()
end
end
end
end
module Test #:nodoc:all
module Unit
module UI
module Console
class TestRunner
def create_mediator(suite)
# swap in our custom mediator
return YNOT::YNOTE::TestUnit.new(suite)
end
end
end
end
end
end
If this doesnt work ... i could mail you the whole file.
Thanks,
Tony