Module: TestConsole::Runner

Includes:
Colors, Config
Included in:
TestConsole
Defined in:
lib/test_console/runner.rb

Constant Summary

Constants included from Colors

Colors::COLORS

Instance Method Summary collapse

Methods included from Colors

#color, #reset_color, #start_color

Instance Method Details

#abortObject

If there is a test suite running, the run is aborted Otherwise the console is killed



129
130
131
# File 'lib/test_console/runner.rb', line 129

def abort
  (@running) ? @abort = true : die
end

#rerun(type = nil) ⇒ Object

Reruns previous failures or errors Can either just run errors or failures

Examples:

rerun           # Rerun everything
rerun :errors   # Rerun errors
rerun :failures # Rerun failures


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
# File 'lib/test_console/runner.rb', line 55

def rerun type=nil
  return false unless @last_run_path

  auto_reload!
  @checked_views = false

  if File.directory? @last_run_path
    suite = make_suite_from_folder(@last_run_path, @last_run_filter)
  else
    suite = make_suite_from_file(@last_run_path, @last_run_filter)
  end

  suite = filter_tests(suite, @last_run_filter) if @last_run_filter

  to_rerun = []

  to_rerun += @last_run_failures unless type == :errors
  to_rerun += @last_run_errors unless type == :failures

  out 'All tests passed last time.  Nothing to rerun', :green and return true if to_rerun.empty?

  to_rerun = to_rerun.collect {|t| t.test_name}

  failed_suite = Test::Unit::TestSuite.new 'Previous test failures'
  suite.tests.each {|t| failed_suite.tests << t if to_rerun.include?(t.name)}

  run_suite failed_suite
end

#run(path, filter = nil) ⇒ Object

Checks that the specified path is valid. If so it creates a test suite from the path and runs it. If a filter is passed as a string or regex, individual test names are filtered by the expression.

Examples:

run './unit'                           # Run every test in the unit folder
run './unit/a_model_test.rb'           # Run the specific test file
run './unit', 'update'                 # Run every test in the unit folder that has update in the test name
run './unit/a_model_test.rb' /update/i # Run every test in specified file whose name contains a case-insensitve version of 'update'


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/test_console/runner.rb', line 16

def run path, filter=nil
  begin
    unless path && !path.empty?
      raise 'No path specified'
    end

    unless File.exists? path
      raise "Path #{path} doesn't exist"
    end

    auto_reload!
    @checked_views = false

    if File.directory? path
      suite = make_suite_from_folder(path, filter)
    else
      suite = make_suite_from_file(path, filter)
    end

    suite = filter_tests(suite, filter) if filter

    @last_run_path = path
    @last_run_filter = filter

    run_suite suite
  rescue Exception => e
    error e.message, e.backtrace
  end
end

#run_suite(suite) ⇒ Object

Runs a defined suite of tests Outputs the results



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/test_console/runner.rb', line 86

def run_suite(suite)
  @abort = false
  @running = true

  return false if stop_folders_changed?

  result = Test::Unit::TestResult.new
  started_at = Time.now

  suite.tests.each do |test|
    break if @abort
    test.run result do |type, name|
      case type
        when Test::Unit::TestCase::STARTED
          @num_failures = result.failure_count
          @num_errors = result.error_count
        when Test::Unit::TestCase::FINISHED
          if result.failure_count == @num_failures && result.error_count == @num_errors
            out name, success_color
          elsif result.error_count > @num_errors
            out name, error_color
          else
            out name, fail_color
          end
      end
    end
  end

  @last_run_failures = result.instance_variable_get(:@failures)
  @last_run_errors = result.instance_variable_get(:@errors)

  print_negatives @last_run_failures, fail_color
  print_negatives @last_run_errors, error_color

  print_result_summary result, Time.now - started_at

  @running = false
  @last_init_time ||= Time.now
  @last_run_time = Time.now
end