Class: MicroTest::Test

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

Overview

Superclass for all test classes.

Examples:

Create a subclass with a test.

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

Class Method Summary collapse

Class Method Details

.after(what = nil) { ... } ⇒ Object

Defines a teardown method that will run after each individual test.

Parameters:

  • what (Symbol) (defaults to: nil)

    Deprecated but maintained for backwards compatibility.

Yields:

  • A block of code that will serve as the teardown method.



60
61
62
# File 'lib/micro_test/test.rb', line 60

def after(what=nil, &block)
  @after = block
end

.before(what = nil) { ... } ⇒ Object

Defines a setup method that will run before each individual test.

Parameters:

  • what (Symbol) (defaults to: nil)

    Deprecated but maintained for backwards compatibility.

Yields:

  • A block of code that will serve as the setup method.



53
54
55
# File 'lib/micro_test/test.rb', line 53

def before(what=nil, &block)
  @before = block
end

.filesHash

Note:

Primarily used in the context of MicroTest::Test.

All files that define subclasses of this class.

Examples:

Files are stored in a Hash with the following format.

{
  "path/to/file1.rb" => ["line 1", "line 2", "line 3", ...],
  "path/to/file2.rb" => ["line 1", "line 2", "line 3", ...]
}

Returns:

  • (Hash)


33
34
35
# File 'lib/micro_test/test.rb', line 33

def files
  @files ||= {}
end

.inherited(subclass) ⇒ Object

A callback provided by Ruby that is invoked whenever a subclass is created.



44
45
46
47
48
# File 'lib/micro_test/test.rb', line 44

def inherited(subclass)
  file_path = caller[0][0, caller[0].index(/:[0-9]+:/)]
  files[file_path] = File.open(file_path).readlines
  subclasses << subclass
end

.resetObject

Resets the state in preparation for a new test run.



38
39
40
41
# File 'lib/micro_test/test.rb', line 38

def reset
  tests.each { |test| test.reset }
  subclasses.each { |subclass| subclass.reset }
end

.subclassesArray<MicroTest::Test>

All subclasses of this class.

Returns:



15
16
17
# File 'lib/micro_test/test.rb', line 15

def subclasses
  @subclasses ||= []
end

.test(desc) { ... } ⇒ Object

Defines a test. Allows subclasses to define tests in their class definition.

Examples:

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

Parameters:

  • desc (String)

    A description for the test.

Yields:

  • A block that defines the test code.



76
77
78
79
80
81
# File 'lib/micro_test/test.rb', line 76

def test(desc, &block)
  wrapper = MicroTest::TestWrapper.new(self, desc, &block)
  wrapper.create_method(:before, &@before) if @before
  wrapper.create_method(:after, &@after) if @after
  tests << wrapper
end

.testsArray<MicroTest::TestWrapper>

All individual tests defined in this class.

Returns:



21
22
23
# File 'lib/micro_test/test.rb', line 21

def tests
  @tests ||= []
end