Class: Petitest::TestRunner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test:, test_group:, test_method_name:) ⇒ TestRunner

Returns a new instance of TestRunner.

Parameters:



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/petitest/test_runner.rb', line 24

def initialize(
  test:,
  test_group:,
  test_method_name:
)
  @duration = nil
  @processed = false
  @test = test
  @test_group = test_group
  @test_method_name = test_method_name
end

Instance Attribute Details

#errorStandardError?

Returns:

  • (StandardError, nil)


4
5
6
# File 'lib/petitest/test_runner.rb', line 4

def error
  @error
end

#finished_atTime?

Returns:

  • (Time, nil)


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

def finished_at
  @finished_at
end

#started_atTime?

Returns:

  • (Time, nil)


10
11
12
# File 'lib/petitest/test_runner.rb', line 10

def started_at
  @started_at
end

#testPetitest::Test (readonly)

Returns:



13
14
15
# File 'lib/petitest/test_runner.rb', line 13

def test
  @test
end

#test_groupPetitest::TestGroup (readonly)

Returns:



16
17
18
# File 'lib/petitest/test_runner.rb', line 16

def test_group
  @test_group
end

#test_method_nameSymbol (readonly)

Returns:

  • (Symbol)


19
20
21
# File 'lib/petitest/test_runner.rb', line 19

def test_method_name
  @test_method_name
end

Instance Method Details

#backtraceArray<String>?

Returns:

  • (Array<String>, nil)


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

def backtrace
  if error
    error.backtrace
  end
end

#descriptionString

Returns:

  • (String)


44
45
46
# File 'lib/petitest/test_runner.rb', line 44

def description
  test.class.description_by_method_name[test_method_name] || "##{test_method_name}"
end

#error_class_nameString?

Returns:

  • (String, nil)


57
58
59
60
61
# File 'lib/petitest/test_runner.rb', line 57

def error_class_name
  if error
    error.class.to_s
  end
end

#error_messageString?

Returns:

  • (String, nil)


64
65
66
67
68
# File 'lib/petitest/test_runner.rb', line 64

def error_message
  if error
    error.to_s
  end
end

#failed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/petitest/test_runner.rb', line 71

def failed?
  processed? && !skipped? && !error.nil?
end

#filtered_backtraceArray<String>?

Returns:

  • (Array<String>, nil)


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/petitest/test_runner.rb', line 76

def filtered_backtrace
  @filtered_backtrace ||= begin
    path = ::File.expand_path("../test_group.rb", __FILE__)
    backtrace.reverse_each.each_with_object([]) do |line, lines|
      if line.start_with?(path)
        break lines
      end
      unless ::Petitest.configuration.backtrace_filters.any? { |filter| filter === line }
        lines << line
      end
    end.reverse
  end
end

#full_descriptionString

Returns:

  • (String)


49
50
51
52
53
54
# File 'lib/petitest/test_runner.rb', line 49

def full_description
  [
    test_group.full_description,
    description,
  ].join(" ")
end

#inspectObject

Note:

Override



91
92
93
# File 'lib/petitest/test_runner.rb', line 91

def inspect
  "#<#{self.class}>"
end

#passed?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/petitest/test_runner.rb', line 96

def passed?
  processed? && !skipped? && !failed?
end

#processed?Boolean

Returns:

  • (Boolean)


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

def processed?
  @processed
end

#runObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/petitest/test_runner.rb', line 105

def run
  self.started_at = ::Time.now
  test.send(test_method_name)
  true
rescue => error
  self.error = error
  false
ensure
  @finished_at = ::Time.now
  @processed = true
end

#skipped?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/petitest/test_runner.rb', line 118

def skipped?
  processed? && error.is_a?(::Petitest::AssertionSkipError)
end

#test_methodPetitest::TestMethod



123
124
125
126
127
128
129
130
131
132
# File 'lib/petitest/test_runner.rb', line 123

def test_method
  @test_method ||= begin
    source_location = test.public_method(test_method_name).source_location
    ::Petitest::TestMethod.new(
      line_number: source_location[0],
      method_name: test_method_name,
      path: source_location[1],
    )
  end
end