# (c) Copyright 2006-2007 Nick Sieger # See the file LICENSE.txt included with the distribution for # software license details. require File.dirname(__FILE__) + "/../../spec_helper.rb" require 'rexml/document' describe "A TestSuite" do before(:each) do @suite = CI::Reporter::TestSuite.new("example suite") end it "should collect timings when start and finish are invoked in sequence" do @suite.start @suite.finish @suite.time.should > 0 end it "should aggregate tests" do @suite.start @suite.testcases << CI::Reporter::TestCase.new("example test") @suite.finish @suite.tests.should == 1 end it "should indicate number of failures and errors" do failure = mock("failure") failure.stub!(:failure?).and_return true failure.stub!(:error?).and_return false error = mock("error") error.stub!(:failure?).and_return false error.stub!(:error?).and_return true @suite.start @suite.testcases << CI::Reporter::TestCase.new("example test") @suite.testcases << CI::Reporter::TestCase.new("failure test") @suite.testcases.last.failure = failure @suite.testcases << CI::Reporter::TestCase.new("error test") @suite.testcases.last.failure = error @suite.finish @suite.tests.should == 3 @suite.failures.should == 1 @suite.errors.should == 1 end end describe "TestSuite xml" do before(:each) do @suite = CI::Reporter::TestSuite.new("example suite") @suite.assertions = 11 begin raise StandardError, "an exception occurred" rescue => e @exception = e end end it "should contain Ant/JUnit-formatted description of entire suite" do failure = mock("failure") failure.stub!(:failure?).and_return true failure.stub!(:error?).and_return false failure.stub!(:name).and_return "failure" failure.stub!(:message).and_return "There was a failure" failure.stub!(:location).and_return @exception.backtrace.join("\n") error = mock("error") error.stub!(:failure?).and_return false error.stub!(:error?).and_return true error.stub!(:name).and_return "error" error.stub!(:message).and_return "There was a error" error.stub!(:location).and_return @exception.backtrace.join("\n") @suite.start @suite.testcases << CI::Reporter::TestCase.new("example test") @suite.testcases << CI::Reporter::TestCase.new("failure test") @suite.testcases.last.failure = failure @suite.testcases << CI::Reporter::TestCase.new("error test") @suite.testcases.last.failure = error @suite.finish xml = @suite.to_xml doc = REXML::Document.new(xml) testsuite = doc.root.elements.to_a("/testsuite") testsuite.length.should == 1 testsuite = testsuite.first testsuite.attributes["name"].should == "example suite" testsuite.attributes["assertions"].should == "11" testcases = testsuite.elements.to_a("testcase") testcases.length.should == 3 end it "should contain full exception type and message in location element" do failure = mock("failure") failure.stub!(:failure?).and_return true failure.stub!(:error?).and_return false failure.stub!(:name).and_return "failure" failure.stub!(:message).and_return "There was a failure" failure.stub!(:location).and_return @exception.backtrace.join("\n") @suite.start @suite.testcases << CI::Reporter::TestCase.new("example test") @suite.testcases << CI::Reporter::TestCase.new("failure test") @suite.testcases.last.failure = failure @suite.finish xml = @suite.to_xml doc = REXML::Document.new(xml) elem = doc.root.elements.to_a("/testsuite/testcase[@name='failure test']/failure").first location = elem.texts.join location.should =~ Regexp.new(failure.message) location.should =~ Regexp.new(failure.name) end it "should filter attributes properly for invalid characters" do failure = mock("failure") failure.stub!(:failure?).and_return true failure.stub!(:error?).and_return false failure.stub!(:name).and_return "failure" failure.stub!(:message).and_return "There was a \nReason: blah" failure.stub!(:location).and_return @exception.backtrace.join("\n") @suite.start @suite.testcases << CI::Reporter::TestCase.new("failure test") @suite.testcases.last.failure = failure @suite.finish xml = @suite.to_xml xml.should =~ %r/message="There was a <failure>\.\.\."/ end end describe "A TestCase" do before(:each) do @tc = CI::Reporter::TestCase.new("example test") end it "should collect timings when start and finish are invoked in sequence" do @tc.start @tc.finish @tc.time.should > 0 end end