Class: MicroTest::TestWrapper

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/micro_test/test_wrapper.rb

Overview

A wrapper class for individual tests. Exists for the purpose of isolating the test method so it can run in its own thread.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_class, desc) { ... } ⇒ TestWrapper

Constructor.

Parameters:

  • test_class (MicroTest::Test)

    The test class that defines the test being wrapped.

  • desc (String)

    The test description.

Yields:

  • The block that defines the test code.



15
16
17
18
19
20
21
# File 'lib/micro_test/test_wrapper.rb', line 15

def initialize(test_class, desc, &block)
  super() # inits MonitorMixin
  @test_class = test_class
  @desc = desc
  create_method(:test, &block)
  reset
end

Instance Attribute Details

#assertsObject (readonly)

Returns the value of attribute asserts.



9
10
11
# File 'lib/micro_test/test_wrapper.rb', line 9

def asserts
  @asserts
end

#descObject (readonly)

Returns the value of attribute desc.



9
10
11
# File 'lib/micro_test/test_wrapper.rb', line 9

def desc
  @desc
end

#test_classObject (readonly)

Returns the value of attribute test_class.



9
10
11
# File 'lib/micro_test/test_wrapper.rb', line 9

def test_class
  @test_class
end

Instance Method Details

#afterObject



33
# File 'lib/micro_test/test_wrapper.rb', line 33

def after; end

#assert(value) ⇒ Object

A basic assert method to be used within tests.

Examples:

class SimpleTest < MicroTest::Test
  test "common sense" do
    assert 1 > 0
  end
end

Parameters:

  • value (Object)

    The value to assert.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/micro_test/test_wrapper.rb', line 62

def assert(value)
  @asserts << assert_info(caller).merge(:value => value)

  if !value
    binding.pry(:quiet => true) if @options[:pry]

    # I don't really like the coupling to the runner here
    MicroTest::Runner.exit = true if @options[:fail_fast]
  end

  value
end

#beforeObject

callback stubs



32
# File 'lib/micro_test/test_wrapper.rb', line 32

def before; end

#create_method(name) { ... } ⇒ Object

Creates a method on this instance.

Parameters:

  • name (Symbol)

    The name of the method.

Yields:

  • The block of code that will serve as the method’s implementation.



26
27
28
29
# File 'lib/micro_test/test_wrapper.rb', line 26

def create_method(name, &block)
  eigen = class << self; self; end
  eigen.send(:define_method, name, &block)
end

#durationObject

Rounded duration.



102
103
104
105
# File 'lib/micro_test/test_wrapper.rb', line 102

def duration
  return nil if @duration.nil?
  (@duration * 10**4).ceil.to_f / 10**4
end

#failed_assertsObject

Returns a list of all failed asserts.



89
90
91
92
# File 'lib/micro_test/test_wrapper.rb', line 89

def failed_asserts
  return [] if passed?
  @asserts.select { |a| !a[:value] }
end

#finished?Boolean

Indicates if this test has finished running.

Returns:

  • (Boolean)


77
78
79
# File 'lib/micro_test/test_wrapper.rb', line 77

def finished?
  !@duration.nil?
end

#invoke(formatter, options = {}) ⇒ Object

Runs the test code.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/micro_test/test_wrapper.rb', line 38

def invoke(formatter, options={})
  reset
  @formatter = formatter
  @options = options
  @formatter.before_test(self)
  start = Time.now
  before
  test
  @invoked = true
  after
  @duration = Time.now - start
  @formatter.after_test(self)
end

#passed?Boolean

Indicates if this test passed.

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/micro_test/test_wrapper.rb', line 82

def passed?
  return true if !@invoked || @asserts.empty?
  return false if @asserts.empty?
  @asserts.map{ |a| !!a[:value] }.uniq == [true]
end

#resetObject

Resets this test in preparation for a clean test run.



95
96
97
98
99
# File 'lib/micro_test/test_wrapper.rb', line 95

def reset
  @invoked = false
  @asserts = []
  @duration = nil
end