Class: GUnit::TestRunner

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gunit/test_runner.rb

Overview

A TestRunner object discovers TestCase classes The TestRunner object calls suite() on all TestCase classes Each TestCase class returns a TestSuite object with instances of itself (TestCases) each with a method to be executed The TestRunner object calls run() on all TestSuite objects, collecting TestResponses Each TestSuite object calls run() on all of its TestCase objects, yielding TestResponses Each TestCase object executes its method, returning a TestResponse The TestRunner displays the TestResponses as they are yielded After all tests have run, the TestRunner displays a summery of results

Constant Summary collapse

PASS_CHAR =
'.'
FAIL_CHAR =
'F'
TODO_CHAR =
'*'
EXCEPTION_CHAR =
'E'
DEFAULT_PATTERNS =
['test/**/*_test.rb', 'test/**/test_*.rb']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TestRunner

Returns a new instance of TestRunner.



35
36
37
38
# File 'lib/gunit/test_runner.rb', line 35

def initialize(*args)
  STDOUT.sync = true
  reset
end

Instance Attribute Details

#autorun=(value) ⇒ Object (writeonly)

Sets the attribute autorun

Parameters:

  • value

    the value to set the attribute autorun to.



32
33
34
# File 'lib/gunit/test_runner.rb', line 32

def autorun=(value)
  @autorun = value
end

#ioObject

TestSuites and/or TestCases



31
32
33
# File 'lib/gunit/test_runner.rb', line 31

def io
  @io
end

#patternsObject

TestSuites and/or TestCases



31
32
33
# File 'lib/gunit/test_runner.rb', line 31

def patterns
  @patterns
end

#responsesObject (readonly)

Returns the value of attribute responses.



33
34
35
# File 'lib/gunit/test_runner.rb', line 33

def responses
  @responses
end

#silentObject

TestSuites and/or TestCases



31
32
33
# File 'lib/gunit/test_runner.rb', line 31

def silent
  @silent
end

#testsObject



55
56
57
# File 'lib/gunit/test_runner.rb', line 55

def tests
  @tests ||= []
end

Instance Method Details

#autorun?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/gunit/test_runner.rb', line 59

def autorun?
  ! @autorun.nil? && @autorun
end

#discoverObject



49
50
51
52
53
# File 'lib/gunit/test_runner.rb', line 49

def discover
  files = self.patterns.inject([]){|memo, pattern| memo + Dir.glob(pattern) }.compact
  files.each {|file| Kernel.require file }
  self.tests = TestCase.subclasses.inject([]){|memo, test_case| memo << test_case.suite }.compact
end

#exceptionsObject



99
100
101
# File 'lib/gunit/test_runner.rb', line 99

def exceptions
  @responses.find_all{|r| r.is_a? ExceptionResponse }
end

#failsObject



95
96
97
# File 'lib/gunit/test_runner.rb', line 95

def fails
  @responses.find_all{|r| r.is_a? FailResponse }
end

#passesObject



91
92
93
# File 'lib/gunit/test_runner.rb', line 91

def passes
  @responses.find_all{|r| r.is_a? PassResponse }
end

#resetObject



40
41
42
43
44
45
46
47
# File 'lib/gunit/test_runner.rb', line 40

def reset
  @io         = STDOUT
  @silent     = false
  @responses  = []
  @tests      = []
  @patterns   = DEFAULT_PATTERNS
  @autorun    = true
end

#runObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gunit/test_runner.rb', line 67

def run
  self.tests.each do |test|
    case
    when test.is_a?(TestSuite)
      test.run do |response|
        @responses << response
        @io.print self.class.response_character(response) unless self.silent
      end
    when test.is_a?(TestCase)
      response = test.run
      @responses << response
      @io.print self.class.response_character(response) unless self.silent
    end
  end
  
  unless self.silent
    @io.puts ""
    @responses.each do |response|
      @io.puts "#{response.message} (#{response.file_name}:#{response.line_number})" unless response.is_a?(GUnit::PassResponse)
    end
    @io.puts "#{@responses.length} verifications: #{passes.length} passed, #{fails.length} failed, #{exceptions.length} exceptions, #{to_dos.length} to-dos"
  end
end

#run!Object



63
64
65
# File 'lib/gunit/test_runner.rb', line 63

def run!
  self.run if self.autorun?
end

#to_dosObject



103
104
105
# File 'lib/gunit/test_runner.rb', line 103

def to_dos
  @responses.find_all{|r| r.is_a? ToDoResponse }
end