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'
PASS_COLOR =
"\e[0;32m"
FAIL_COLOR =
"\e[0;31m"
EXCEPTION_COLOR =
"\e[0;31m"
TODO_COLOR =
"\e[0;33m"
DEFAULT_COLOR =
"\e[0m"
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.



40
41
42
43
# File 'lib/gunit/test_runner.rb', line 40

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.



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

def autorun=(value)
  @autorun = value
end

#ioObject

TestSuites and/or TestCases



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

def io
  @io
end

#patternsObject

TestSuites and/or TestCases



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

def patterns
  @patterns
end

#responsesObject (readonly)

Returns the value of attribute responses.



38
39
40
# File 'lib/gunit/test_runner.rb', line 38

def responses
  @responses
end

#silentObject

TestSuites and/or TestCases



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

def silent
  @silent
end

#testsObject



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

def tests
  @tests ||= []
end

Instance Method Details

#autorun?Boolean

Returns:

  • (Boolean)


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

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

#discoverObject



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

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



105
106
107
# File 'lib/gunit/test_runner.rb', line 105

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

#failsObject



101
102
103
# File 'lib/gunit/test_runner.rb', line 101

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

#passesObject



97
98
99
# File 'lib/gunit/test_runner.rb', line 97

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

#resetObject



45
46
47
48
49
50
51
52
# File 'lib/gunit/test_runner.rb', line 45

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

#runObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gunit/test_runner.rb', line 72

def run
  @io.puts test_case_classes.map{|klass| klass.to_s }.join(', ') unless self.silent
  self.test_cases.each do |test_case|
    response = test_case.run
    @responses << response
    unless self.silent
      @io.print self.class.response_color(response)
      @io.print self.class.response_character(response)
      @io.print DEFAULT_COLOR
    end
  end
  
  unless self.silent
    @io.puts ""
    @responses.each do |response|
      unless response.is_a?(GUnit::PassResponse)
        @io.print self.class.response_color(response)
        @io.print "#{response.message} (#{response.file_name}:#{response.line_number})\n"
        @io.print DEFAULT_COLOR
      end
    end
    @io.puts "#{@responses.length} verifications: #{passes.length} passed, #{fails.length} failed, #{exceptions.length} exceptions, #{to_dos.length} to-dos"
  end
end

#run!Object



68
69
70
# File 'lib/gunit/test_runner.rb', line 68

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

#to_dosObject



109
110
111
# File 'lib/gunit/test_runner.rb', line 109

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