Class: Test::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/test/cli.rb,
lib/test/runner.rb

Overview

The Test::Runner class handles the execution of tests.

Constant Summary collapse

DEFAULT_FORMAT =

Default report is in the old “dot-progress” format.

'dotprogress'
OPEN_ERRORS =

Exceptions that are not caught by test runner.

[NoMemoryError, SignalException, Interrupt, SystemExit]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Runner

New Runner.

Parameters:

  • suite (Array)

    A list of compliant tests/testcases.



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/test/runner.rb', line 132

def initialize(options={}, &block)
  @suite     = options[:suite]   || self.class.suite
  @files     = options[:files]   || self.class.files
  @format    = options[:format]  || self.class.format
  @tags      = options[:tags]    || self.class.tags
  @units     = options[:units]   || self.class.units
  @match     = options[:match]   || self.class.match
  @verbose   = options[:verbose] || self.class.verbose
  @hard      = options[:hard]    || self.class.hard

  block.call(self) if block
end

Instance Attribute Details

#filesObject (readonly)

Test files to load.



87
88
89
# File 'lib/test/runner.rb', line 87

def files
  @files
end

#formatObject

Reporter format name, or name fragment, used to look up reporter class.



90
91
92
# File 'lib/test/runner.rb', line 90

def format
  @format
end

#matchObject (readonly)

Matching text used to filter which tests are run.



98
99
100
# File 'lib/test/runner.rb', line 98

def match
  @match
end

#observersObject (readonly)

Array of observers, typically this just contains the recorder and reporter instances.



153
154
155
# File 'lib/test/runner.rb', line 153

def observers
  @observers
end

#recorderObject (readonly)

Record pass, fail, error, pending and omitted tests.



149
150
151
# File 'lib/test/runner.rb', line 149

def recorder
  @recorder
end

#reporterObject (readonly)

The reporter to use for ouput.



146
147
148
# File 'lib/test/runner.rb', line 146

def reporter
  @reporter
end

#suiteObject (readonly)

Test suite to run. This is a list of compliant test units and test cases.



84
85
86
# File 'lib/test/runner.rb', line 84

def suite
  @suite
end

#tagsObject (readonly)

Selected tags used to filter which tests are run.



101
102
103
# File 'lib/test/runner.rb', line 101

def tags
  @tags
end

#unitsObject (readonly)

List of units with which to filter tests. It is an array of strings which are matched against module, class and method names.



105
106
107
# File 'lib/test/runner.rb', line 105

def units
  @units
end

Class Method Details

.cli(*argv) ⇒ Object

Test runner command line interface.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/test/cli.rb', line 11

def self.cli(*argv)
  runner = new

  Test::Config.load

  cli_options(runner, argv)

  begin
    # Add standard location if it exists.
    $LOAD_PATH.unshift(File.expand_path('lib')) if File.directory?('lib')

    success = runner.run
    exit -1 unless success
  rescue => error
    raise error if $DEBUG
    $stderr.puts('ERROR: ' + error.to_s)
  end
end

.cli_options(runner, argv) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/test/cli.rb', line 31

def self.cli_options(runner, argv)
  require 'optparse'

  config = Test.config.dup
  config_loaded = false

  common  = config.delete('common')
  default = config.delete('default')

  common.call(runner) if common

  OptionParser.new do |opt|
    opt.banner = "Usage: #{$0} [options] [files ...]"

    unless config.empty?
      opt.separator "PRESET OPTIONS:"
      config.each do |name, block|
        opt.on("--#{name}") do
          block.call(runner)
        end
      end
    end

    opt.separator "CONFIG OPTIONS:"

    opt.on '-f', '--format NAME', 'report format' do |name|
      runner.format = name
    end
    opt.on '-y', '--tapy', 'shortcut for -f tapy' do
      runner.format = 'tapy'
    end
    opt.on '-j', '--tapj', 'shortcut for -f tapj' do
      runner.format = 'tapj'
    end

    opt.on '-t', '--tag TAG', 'select tests by tag' do |tag|
      runner.tags << tag
    end
    opt.on '-u', '--unit TAG', 'select tests by software unit' do |unit|
      runner.units << unit
    end
    opt.on '-m', '--match TEXT', 'select tests by description' do |text|
      runner.match << text 
    end

    opt.on '-I', '--loadpath PATH',  'add to $LOAD_PATH' do |paths|
      paths.split(/[:;]/).reverse_each do |path|
        $LOAD_PATH.unshift path
      end
    end
    opt.on '-r', '--require FILE', 'require file' do |file|
      require file
    end
    opt.on '-v' , '--verbose', 'provide extra detailed report' do
      runner.verbose = true
    end
    #opt.on('--log DIRECTORY', 'log directory'){ |dir|
    #  options[:log] = dir
    #}
    opt.on_tail("--[no-]ansi" , 'turn on/off ANSI colors'){ |v| $ansi = v }
    opt.on_tail("--debug" , 'turn on debugging mode'){ $DEBUG = true }
    #opt.on_tail("--about" , 'display information about lemon'){
    #  puts "Ruby Test v#{VERSION}"
    #  puts "#{COPYRIGHT}"
    #  exit
    #}
    opt.on_tail('-h', '--help', 'display this help message'){
      puts opt
      exit
    }
  end.parse!(argv)

  default.call(runner) if default && !config_loaded

  runner.files.replace(argv) unless argv.empty?
end

.filesObject

Default list of test files to load.



32
33
34
# File 'lib/test/runner.rb', line 32

def self.files
  @files ||= []
end

.formatObject



37
38
39
# File 'lib/test/runner.rb', line 37

def self.format
  @format || DEFAULT_FORMAT
end

.format=(format) ⇒ Object



42
43
44
# File 'lib/test/runner.rb', line 42

def self.format=(format)
  @format = format
end

.hardObject



72
73
74
# File 'lib/test/runner.rb', line 72

def self.hard
  @hard
end

.hard=(boolean) ⇒ Object



77
78
79
# File 'lib/test/runner.rb', line 77

def self.hard=(boolean)
  @hard = !!boolean
end

.matchObject

Default description match for filtering tests.



57
58
59
# File 'lib/test/runner.rb', line 57

def self.match
  @match ||= []
end

.suiteObject

Default test suite ($TEST_SUITE).



27
28
29
# File 'lib/test/runner.rb', line 27

def self.suite
  $TEST_SUITE
end

.tagsObject

Default selection of tags for filtering tests.



62
63
64
# File 'lib/test/runner.rb', line 62

def self.tags
  @tags ||= []
end

.unitsObject

Default selection of units for filtering tests.



67
68
69
# File 'lib/test/runner.rb', line 67

def self.units
  @unit ||= []
end

.verboseObject



47
48
49
# File 'lib/test/runner.rb', line 47

def self.verbose
  @verbose
end

.verbose=(boolean) ⇒ Object



52
53
54
# File 'lib/test/runner.rb', line 52

def self.verbose=(boolean)
  @verbose = !!boolean
end

Instance Method Details

#hard=(boolean) ⇒ Object



123
124
125
# File 'lib/test/runner.rb', line 123

def hard=(boolean)
  @hard = !!boolean
end

#hard?Boolean

Use “hard” test mode?

Returns:

  • (Boolean)


118
119
120
# File 'lib/test/runner.rb', line 118

def hard?
  @hard
end

#runBoolean

Run test suite.

Returns:

  • (Boolean)

    That the tests ran without error or failure.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/test/runner.rb', line 160

def run
  files_resolved.each do |file|
    require file
  end

  @reporter  = reporter_load(format)
  @recorder  = Recorder.new
  @observers = [@reporter, @recorder]

  #cd_tmp do
    observers.each{ |o| o.begin_suite(suite) }
    run_thru(suite)
    observers.each{ |o| o.end_suite(suite) }
  #end

  recorder.success?
end

#verbose=(boolean) ⇒ Object



113
114
115
# File 'lib/test/runner.rb', line 113

def verbose=(boolean)
  @verbose = !!boolean
end

#verbose?Boolean

Show extra details in reports.

Returns:

  • (Boolean)


108
109
110
# File 'lib/test/runner.rb', line 108

def verbose?
  @verbose
end