Permalink
Browse files

- add initial version of Test::Unit support

git-svn-id: http://svn.caldersphere.net/svn/main/ci_reporter@54 b03c2d0b-2f10-0410-a2f9-fc8001506dfa
  • Loading branch information...
1 parent b8a4915 commit dd18124faaa4610c730194df4cbff193c0032c78 @nicksieger nicksieger committed Jan 4, 2007
@@ -3,7 +3,7 @@
module CI
module Reporter
class ReportManager
- def initialize(prefix="test")
+ def initialize(prefix)
@basedir = ENV['CI_REPORTS'] || File.expand_path("#{Dir.getwd}/#{prefix.downcase}/reports")
@basename = "#{@basedir}/#{prefix.upcase}"
FileUtils.mkdir_p(@basedir)
@@ -1,21 +1,96 @@
+require 'test/unit'
+require 'test/unit/ui/console/testrunner'
+
module CI
module Reporter
class TestUnitFailure
+ def initialize(fault)
+ @fault = fault
+ end
def failure?
-
+ @fault.kind_of? Test::Unit::Failure
end
-
+
def error?
-
+ @fault.kind_of? Test::Unit::Error
end
-
+
def exception
-
+ @fault.exception
end
end
- class TestUnit
-
+ 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 = nil
+ @unknown_count = 0
+ end
+
+ def test_started(name)
+ test_name, suite_name = extract_names(name)
+ unless @suite && @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)
+ finish_test(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)
+ @suite = TestSuite.new(suite_name)
+ @suite.start
+ end
+
+ def finish_suite
+ if @suite
+ @suite.finish
+ @report_manager.write_report(@suite)
+ end
+ end
+
+ def start_test(test_name)
+ tc = TestCase.new(test_name)
+ tc.start
+ @suite.testcases << tc
+ end
+
+ def finish_test(failure = nil)
+ tc = @suite.testcases.last
+ tc.finish
+ tc.failure = TestUnitFailure.new(failure) if failure
+ end
end
end
end
@@ -0,0 +1,75 @@
+require File.dirname(__FILE__) + "/../../spec_helper.rb"
+
+context "The TestUnit reporter" do
+ setup do
+ @report_mgr = mock("report manager")
+ @testunit = CI::Reporter::TestUnit.new(nil, @report_mgr)
+ end
+
+ specify "should build suites based on adjacent tests with the same class name" do
+ @suite = nil
+ @report_mgr.should_receive(:write_report).once.and_return {|suite| @suite = suite }
+
+ @testunit.started(mock("result"))
+ @testunit.test_started("test_one(TestCaseClass)")
+ @testunit.test_finished("test_one(TestCaseClass)")
+ @testunit.test_started("test_two(TestCaseClass)")
+ @testunit.test_finished("test_two(TestCaseClass)")
+ @testunit.finished(10)
+
+ @suite.name.should == "TestCaseClass"
+ @suite.testcases.length.should == 2
+ @suite.testcases.first.name.should == "test_one"
+ @suite.testcases.first.should_not_be_failure
+ @suite.testcases.first.should_not_be_error
+ @suite.testcases.last.name.should == "test_two"
+ @suite.testcases.last.should_not_be_failure
+ @suite.testcases.last.should_not_be_error
+ end
+
+ specify "should build two suites when encountering different class names" do
+ @suites = []
+ @report_mgr.should_receive(:write_report).twice.and_return {|suite| @suites << suite }
+
+ @testunit.started(mock("result"))
+ @testunit.test_started("test_one(TestCaseClass)")
+ @testunit.test_finished("test_one(TestCaseClass)")
+ @testunit.test_started("test_two(AnotherTestCaseClass)")
+ @testunit.test_finished("test_two(AnotherTestCaseClass)")
+ @testunit.finished(10)
+
+ @suites.first.name.should == "TestCaseClass"
+ @suites.first.testcases.length.should == 1
+ @suites.first.testcases.first.name.should == "test_one"
+ @suites.last.name.should == "AnotherTestCaseClass"
+ @suites.last.testcases.length.should == 1
+ @suites.last.testcases.first.name.should == "test_two"
+ end
+
+ specify "should add failures to testcases when encountering a fault" do
+ begin
+ raise StandardError, "error"
+ rescue => e
+ @error = Test::Unit::Error.new("test_two(TestCaseClass)", e)
+ end
+
+ @suite = nil
+ @report_mgr.should_receive(:write_report).once.and_return {|suite| @suite = suite }
+
+ @testunit.started(mock("result"))
+ @testunit.test_started("test_one(TestCaseClass)")
+ @testunit.test_finished("test_one(TestCaseClass)")
+ @testunit.test_started("test_two(TestCaseClass)")
+ @testunit.fault(@error)
+ @testunit.finished(10)
+
+ @suite.name.should == "TestCaseClass"
+ @suite.testcases.length.should == 2
+ @suite.testcases.first.name.should == "test_one"
+ @suite.testcases.first.should_not_be_failure
+ @suite.testcases.first.should_not_be_error
+ @suite.testcases.last.name.should == "test_two"
+ @suite.testcases.last.should_not_be_failure
+ @suite.testcases.last.should_be_error
+ end
+end
View
@@ -3,7 +3,7 @@
require 'spec'
$: << File.dirname(__FILE__) + "/../lib"
require 'ci/reporter/core'
-# require 'ci/reporter/test_unit'
+require 'ci/reporter/test_unit'
require 'ci/reporter/rspec'
REPORTS_DIR = File.dirname(__FILE__) + "/reports"
@@ -1,7 +1,12 @@
namespace :ci do
task :setup_rspec do
- rm_rf "spec/reports"
- ENV['RSPECOPTS'] = ["--require", "#{File.dirname(__FILE__)}/rspec.rb",
+ rm_rf ENV["CI_REPORTS"] || "spec/reports"
+ ENV["RSPECOPTS"] = ["--require", "#{File.dirname(__FILE__)}/rspec_loader.rb",
"--format", "CI::Reporter::RSpec"].join(" ")
end
+
+ task :setup_testunit do
+ rm_rf ENV["CI_REPORTS"] || "test/reports"
+ ENV["TESTOPTS"] = "#{File.dirname(__FILE__)}/test_unit_loader.rb"
+ end
end
File renamed without changes.
@@ -0,0 +1,8 @@
+$: << File.dirname(__FILE__) + "/../lib"
+require 'ci/reporter/test_unit'
+
+class Test::Unit::UI::Console::TestRunner
+ def create_mediator(suite) # swap in our custom mediator
+ return CI::Reporter::TestUnit.new(suite)
+ end
+end

0 comments on commit dd18124

Please sign in to comment.