Class: FasTest::TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/fas_test.rb

Overview

Used to discover tests and actually run them. Normally this class will be used by the command line client.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose = false) ⇒ TestRunner

Returns a new instance of TestRunner.



9
10
11
12
13
# File 'lib/fas_test.rb', line 9

def initialize(verbose = false)
  @verbose = verbose
  @assertion_count = 0
  @test_results = {}
end

Instance Attribute Details

#test_resultsObject (readonly)

Returns the value of attribute test_results.



7
8
9
# File 'lib/fas_test.rb', line 7

def test_results
  @test_results
end

Instance Method Details

#class_inherits_parent?(child_class, parent_class) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/fas_test.rb', line 152

def class_inherits_parent?(child_class, parent_class)
  child_class.ancestors.any? { |ancestor| ancestor == parent_class }
end

#find_test_classesObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/fas_test.rb', line 130

def find_test_classes
  classes = []
  Module.constants.each do |constant|
    constant_str = constant.to_s
    if constant_str.end_with? "Tests"
      classes << eval(constant_str)
    end
  end
  return classes
end

#get_all_test_method_names(test_class) ⇒ Object



126
127
128
# File 'lib/fas_test.rb', line 126

def get_all_test_method_names(test_class)
  test_class.instance_methods.find_all { |m| m.to_s.start_with?('test__') }
end

#get_constant_type(construct) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/fas_test.rb', line 141

def get_constant_type(construct)
  construct_class = construct.class
  if construct_class == Class
    return ConstantTypes::CLASS
  elsif construct_class == Module
    return ConstantTypes::MODULE
  else
    return ConstantTypes::OTHER
  end
end

#increment_assert_countObject



43
44
45
# File 'lib/fas_test.rb', line 43

def increment_assert_count
  @assertion_count += 1
end

#init_test_instance(test_class) ⇒ Object



37
38
39
40
41
# File 'lib/fas_test.rb', line 37

def init_test_instance(test_class)
  test_instance = test_class.new
  test_instance.runner = self
  return test_instance
end

#load_test_files(path) ⇒ Object



122
123
124
# File 'lib/fas_test.rb', line 122

def load_test_files(path)
  Dir[File.join(path, '/**/*_tests.rb')].each { |file_name| load file_name }
end

#pretty_print_stack_trace(exception) ⇒ Object



109
110
111
112
113
# File 'lib/fas_test.rb', line 109

def pretty_print_stack_trace(exception)
  if @verbose
    exception.backtrace.each { |trace| puts "    #{trace}"}
  end
end


156
157
158
159
160
# File 'lib/fas_test.rb', line 156

def print_summary
  pass_count = @test_results.values.find_all { |r| r.status == TestStatuses::PASS }.length
  fail_count = @test_results.length - pass_count
  puts "#{pass_count} passed; #{fail_count} failed; #{@assertion_count} assertions"
end

#record_test_result(test_instance, test_method_name, status) ⇒ Object



80
81
82
83
84
# File 'lib/fas_test.rb', line 80

def record_test_result(test_instance, test_method_name, status)
  test_class = test_instance.class
  key = "#{test_class.name}::#{test_method_name}"
  @test_results[key] = TestResult.new(test_class, test_method_name, status)
end

#run_test(test_instance, test_method_name) ⇒ Object



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
# File 'lib/fas_test.rb', line 47

def run_test(test_instance, test_method_name)
  begin
    status = TestStatuses::PASS
    setup_succeeded = try_test_setup(test_instance)
    test_instance.needs_teardown = true
    if setup_succeeded
      test_instance.send(test_method_name)
      if @verbose
        puts "Pass: #{test_instance.class.name}::#{test_method_name}"
      end
    end
  rescue AssertionException => ex
    status = TestStatuses::FAIL
    try_test_teardown(test_instance)
    puts "Fail: #{test_instance.class.name}::#{test_method_name}"
    puts "  -> #{ex.message}"
  rescue Exception => ex
    status = TestStatuses::CRASH
    try_test_teardown(test_instance)
    puts "Crash: #{test_instance.class.name}::#{test_method_name}"
    puts "  -> #{ex.class.name}: #{ex.message}"
    pretty_print_stack_trace(ex)
  rescue Object => obj
    status = TestStatuses::CRASH
    try_test_teardown(test_instance)
    puts "Crash: #{test_instance.class.name}::#{test_method_name}"
    puts "  -> Raised non-exception: #{obj.to_s}"
  ensure
    try_test_teardown(test_instance)
    record_test_result(test_instance, test_method_name, status)
  end
end

#run_tests_in_class(test_class) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fas_test.rb', line 15

def run_tests_in_class(test_class)
  test_instance = init_test_instance(test_class)
  begin
    setup_failed = false
    begin
      test_instance.class_setup
    rescue Object => ex
      puts "Superfail: Crashed when running #{test_class.name}::class_setup"
      puts "  -> #{ex.class.name}: #{ex.message}"
      pretty_print_stack_trace(ex)
      setup_failed = true
    end
    unless setup_failed
      get_all_test_method_names(test_class).each do |test_method_name|
        run_test(test_instance, test_method_name)
      end
    end
  ensure
    test_instance.class_teardown
  end
end

#run_tests_in_folder(path) ⇒ Object



115
116
117
118
119
120
# File 'lib/fas_test.rb', line 115

def run_tests_in_folder(path)
  load_test_files(path)
  find_test_classes.each do |test_class|
    run_tests_in_class(test_class)
  end
end

#try_test_setup(test_instance) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fas_test.rb', line 86

def try_test_setup(test_instance)
  setup_succeeded = true
  begin
    test_instance.test_setup
  rescue Exception => ex
    setup_succeeded = false
    puts "Superfail: #{test_instance.class.name}::test_setup"
    puts "  -> #{ex.class.name}: #{ex.message}"
    pretty_print_stack_trace(ex)
  end
  return setup_succeeded
end

#try_test_teardown(test_instance) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/fas_test.rb', line 99

def try_test_teardown(test_instance)
  if test_instance.needs_teardown
    begin
      test_instance.test_teardown
    ensure
      test_instance.needs_teardown = false
    end
  end
end