Class: AnySpec::TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/any-spec/test-runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_executable, test_specification_file) ⇒ TestRunner

Returns a new instance of TestRunner.

Raises:

  • (Exception)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/any-spec/test-runner.rb', line 13

def initialize( target_executable, test_specification_file )
  # Verify that the target executable exists and is in the current PATH
  @target_executable = target_executable.strip
  # Load the test specification file
  @test_specification_file = File.expand_path(test_specification_file)
  raise Exception, "The test specification file you supplied does not exist." unless(File.exist? @test_specification_file)
  test_spec = YAML::load_file(@test_specification_file)
  @specification_root = test_spec["specification_root"]
  @specification_extension = test_spec["specification_extension"]
  # Find and load test-case file paths
  @test_case_paths = Dir[File.join(File.split(@test_specification_file)[0], @specification_root, '**',  "*" + @specification_extension)].sort
  # Instantiate the test cases
  @test_cases = @test_case_paths.map do |test_case|
    AnySpec::TestCase.new(test_case, @target_executable)
  end
end

Instance Attribute Details

#reportObject

Returns the value of attribute report.



6
7
8
# File 'lib/any-spec/test-runner.rb', line 6

def report
  @report
end

#specification_extensionObject

Returns the value of attribute specification_extension.



6
7
8
# File 'lib/any-spec/test-runner.rb', line 6

def specification_extension
  @specification_extension
end

#specification_rootObject

Returns the value of attribute specification_root.



6
7
8
# File 'lib/any-spec/test-runner.rb', line 6

def specification_root
  @specification_root
end

#target_executableObject

Returns the value of attribute target_executable.



6
7
8
# File 'lib/any-spec/test-runner.rb', line 6

def target_executable
  @target_executable
end

#test_case_pathsObject

Returns the value of attribute test_case_paths.



6
7
8
# File 'lib/any-spec/test-runner.rb', line 6

def test_case_paths
  @test_case_paths
end

#test_casesObject

Returns the value of attribute test_cases.



6
7
8
# File 'lib/any-spec/test-runner.rb', line 6

def test_cases
  @test_cases
end

Instance Method Details

#message(string, color = :white) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/any-spec/test-runner.rb', line 58

def message(string, color = :white)
  if(@silence)
    @report << string
  else
    case color
      when :white then print( "\e[37m" )
      when :red then print( "\e[31m" )
      when :green then print( "\e[32m" )
      when :grey then print( "\e[90m")
    end
    print string + "\e[0m"
    $stdout.flush
  end
end

#run_tests(silence = false) ⇒ Object



30
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
# File 'lib/any-spec/test-runner.rb', line 30

def run_tests(silence = false)
  @report = ""
  @silence = silence
  message "\nStarted\n"
  start_time = Time.now
  assertions = 0
  failed_tests = []
  @test_cases.each do |test_case|
    result = test_case.run
    message ".", :green if(result)
    message "F", :red if(!result)
    assertions += test_case.assertions
    failed_tests << test_case if(!result)
  end
  message "\n\n"
  message "Failures:\n\n" unless failed_tests.empty?
  failed_tests.each_index do |x|
    test_case = failed_tests[x]
    message "  #{x + 1}) " + test_case.path.gsub(File.split(@test_specification_file)[0], "") + "\n"
    message test_case.message + "\n\n"
  end
  message "\n\nFinished in #{(Time.now - start_time).to_f} seconds.\n"
  pass_rate = format("%.2f",(100.0 - ((failed_tests.length.to_f) / @test_cases.length) * 100))
  result_color = failed_tests.count == 0 ? :green : :red
  message "#{@test_cases.length} tests, #{assertions} assertions, #{failed_tests.count} failures, #{pass_rate}% pass rate\n", result_color
  return @test_cases
end