Class: Test::Unit::UI::XML::TestRunner

Inherits:
Console::TestRunner
  • Object
show all
Defined in:
lib/test/unit/ui/xml/testrunner.rb

Overview

XML::TestRunner - generate xml output for test results

Example use:

$ ruby -rtest/unit/ui/xml/testrunner test/test_1.rb --runner=xml

By default, XML::TestRunner will output to stdout. You can set the environment variable $XMLTEST_OUTPUT to a filename to send the output to that file.

The summary file created by this testrunner is XML, but this module also includes a stylesheet (test/unit/ui/xml/xmltestrunner.xslt) which converts it to HTML. Copy the XSLT file into the same directory as the test results file, and open the results file with a browser.


(todo: use with rake)

Instance Method Summary collapse

Constructor Details

#initialize(suite, output_level) ⇒ TestRunner

Returns a new instance of TestRunner.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/test/unit/ui/xml/testrunner.rb', line 37

def initialize( suite, output_level )
  super( suite )
  if ENV['XMLTEST_OUTPUT']
    fn = ENV['XMLTEST_OUTPUT']
    puts "Writing to #{fn}"
    @io = File.open( fn, "w" )
    @using_stdout = false
  else
    puts "Writing to stdout (along with everyone else...)"
    @io = STDOUT
    @using_stdout = true
  end
  create_document()
end

Instance Method Details

#add_fault(fault) ⇒ Object

callbacks



97
98
99
100
101
102
103
# File 'lib/test/unit/ui/xml/testrunner.rb', line 97

def add_fault( fault )
  ##STDERR.puts "Fault:"
  @faults << fault
  e = REXML::Element.new( "fault" )
  e << REXML::CData.new( fault.long_display ) 
  @current_test << e
end

#create_documentObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/test/unit/ui/xml/testrunner.rb', line 52

def create_document()
  @doc = REXML::Document.new()
  @doc << REXML::XMLDecl.new()
  
  pi = REXML::Instruction.new(
         "xml-stylesheet",
         "type='text/xsl' href='xmltestrunner.xslt' "
  )
  @doc << pi

  e = REXML::Element.new( "testsuite" )
  e.attributes['rundate'] = Time.now
  @doc << e
end

#finished(elapsed_time) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/test/unit/ui/xml/testrunner.rb', line 110

def finished( elapsed_time )
  #STDERR.puts "Finished"
  res = REXML::Element.new( "result" )
  summ = REXML::Element.new( "summary" )
  summ.text = @result
  res << summ
  # @result is a Test::Unit::TestResults
  res.attributes['passed'] = @result.passed?
  res.attributes['testcount'] = @result.run_count
  res.attributes['assertcount'] = @result.assertion_count
  res.attributes['failures'] = @result.failure_count
  res.attributes['errors'] = @result.error_count
  @doc.root << res

  e = REXML::Element.new( "elapsed-time" )
  e.text = elapsed_time
  @doc.root << e
  @io.puts( @doc.to_s )
  
  unless @using_stdout
    puts @result
  end
end

#startObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/test/unit/ui/xml/testrunner.rb', line 71

def start
  @current_test = nil
  # setup_mediator
  @mediator = TestRunnerMediator.new( @suite )
  suite_name = @suite.to_s
  if @suite.kind_of?(Module)
    suite_name = @suite.name
  end
  @doc.root.attributes['name'] = suite_name
  # attach_to_mediator - define callbacks
  @mediator.add_listener( TestResult::FAULT, 
                          &method(:add_fault) )
  @mediator.add_listener( TestRunnerMediator::STARTED,
                          &method(:started) )
  @mediator.add_listener( TestRunnerMediator::FINISHED,
                          &method(:finished) )
  @mediator.add_listener( TestCase::STARTED, 
                          &method(:test_started) )
  @mediator.add_listener( TestCase::FINISHED, 
                          &method(:test_finished) )
  # return start_mediator
  return @mediator.run_suite
end

#started(result) ⇒ Object



105
106
107
108
# File 'lib/test/unit/ui/xml/testrunner.rb', line 105

def started( result )
  #STDERR.puts "Started"
  @result = result
end

#test_finished(name) ⇒ Object



143
144
145
146
147
# File 'lib/test/unit/ui/xml/testrunner.rb', line 143

def test_finished( name )
  #STDERR.puts "Test: #{name} finished"
  # find //test[@name='name']
  @current_test = nil
end

#test_started(name) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/test/unit/ui/xml/testrunner.rb', line 134

def test_started( name )
  #STDERR.puts "Test: #{name} started"
  e = REXML::Element.new( "test" )
  e.attributes['name'] = name
  #e.attributes['status'] = "failed"
  @doc.root << e
  @current_test = e
end

#to_sObject



67
68
69
# File 'lib/test/unit/ui/xml/testrunner.rb', line 67

def to_s
  @doc.to_s
end