Class: CI::Reporter::TestSuite

Inherits:
Struct
  • Object
show all
Defined in:
lib/ci/reporter/test_suite.rb

Overview

Basic structure representing the running of a test suite. Used to time tests and store results.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ TestSuite

Returns a new instance of TestSuite.



58
59
60
61
# File 'lib/ci/reporter/test_suite.rb', line 58

def initialize(name)
  super(name.to_s) # RSpec passes a "description" object instead of a string
  @testcases = []
end

Instance Attribute Details

#assertionsObject

Returns the value of attribute assertions

Returns:

  • (Object)

    the current value of assertions



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def assertions
  @assertions
end

#errorsObject

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def errors
  @errors
end

#failuresObject

Returns the value of attribute failures

Returns:

  • (Object)

    the current value of failures



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def failures
  @failures
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def name
  @name
end

#skippedObject

Returns the value of attribute skipped

Returns:

  • (Object)

    the current value of skipped



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def skipped
  @skipped
end

#stderrObject

Returns the value of attribute stderr.



57
58
59
# File 'lib/ci/reporter/test_suite.rb', line 57

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



57
58
59
# File 'lib/ci/reporter/test_suite.rb', line 57

def stdout
  @stdout
end

#testcasesObject

Returns the value of attribute testcases.



56
57
58
# File 'lib/ci/reporter/test_suite.rb', line 56

def testcases
  @testcases
end

#testsObject

Returns the value of attribute tests

Returns:

  • (Object)

    the current value of tests



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def tests
  @tests
end

#timeObject

Returns the value of attribute time

Returns:

  • (Object)

    the current value of time



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def time
  @time
end

#timestampObject

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



55
56
57
# File 'lib/ci/reporter/test_suite.rb', line 55

def timestamp
  @timestamp
end

Instance Method Details

#create_builderObject

Creates the xml builder instance used to create the report xml document.



85
86
87
88
89
# File 'lib/ci/reporter/test_suite.rb', line 85

def create_builder
  require 'builder'
  # :escape_attrs is obsolete in a newer version, but should do no harm
  Builder::XmlMarkup.new(:indent => 2, :escape_attrs => true)
end

#finishObject

Finishes timing the test suite.



73
74
75
76
77
78
79
80
81
82
# File 'lib/ci/reporter/test_suite.rb', line 73

def finish
  self.tests = testcases.size
  self.time = Time.now - @start
  self.timestamp = @start.iso8601
  self.failures = testcases.inject(0) {|sum,tc| sum += tc.failures.select{|f| f.failure? }.size }
  self.errors = testcases.inject(0) {|sum,tc| sum += tc.failures.select{|f| f.error? }.size }
  self.skipped = testcases.inject(0) {|sum,tc| sum += (tc.skipped? ? 1 : 0) }
  self.stdout = @capture_out.finish if @capture_out
  self.stderr = @capture_err.finish if @capture_err
end

#startObject

Starts timing the test suite.



64
65
66
67
68
69
70
# File 'lib/ci/reporter/test_suite.rb', line 64

def start
  @start = Time.now
  unless ENV['CI_CAPTURE'] == "off"
    @capture_out = OutputCapture.wrap($stdout) {|io| $stdout = io }
    @capture_err = OutputCapture.wrap($stderr) {|io| $stderr = io }
  end
end

#to_xmlObject

Creates an xml string containing the test suite results.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ci/reporter/test_suite.rb', line 92

def to_xml
  builder = create_builder
  # more recent version of Builder doesn't need the escaping
  def builder.trunc!(txt)
    txt.sub(/\n.*/m, '...')
  end
  builder.instruct!
  attrs = {}
  each_pair {|k,v| attrs[k] = builder.trunc!(v.to_s) unless v.nil? || v.to_s.empty? }
  builder.testsuite(attrs) do
    @testcases.each do |tc|
      tc.to_xml(builder)
    end
    builder.tag! "system-out" do
      builder.text!(self.stdout || '' )
    end
    builder.tag! "system-err" do
      builder.text!(self.stderr || '' )
    end
  end
end