Class: Protest::TestCase

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

Overview

A TestCase defines a suite of related tests. You can further categorize your tests by declaring nested contexts inside the class. See TestCase.context.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, location, &block) ⇒ TestCase

Initialize a new instance of a single test. This test can be run in isolation by calling TestCase#run.



110
111
112
113
114
# File 'lib/protest/test_case.rb', line 110

def initialize(name, location, &block)
  @test = block
  @location = location
  @name = name
end

Class Attribute Details

.description=(value) ⇒ Object

Fancy name for your test case, reports can use this to give nice, descriptive output when running your tests.



73
74
75
# File 'lib/protest/test_case.rb', line 73

def description=(value)
  @description = value
end

.locationObject

Fancy name for your test case, reports can use this to give nice, descriptive output when running your tests.



73
74
75
# File 'lib/protest/test_case.rb', line 73

def location
  @location
end

Class Method Details

.after(&block) ⇒ Object



94
95
96
97
# File 'lib/protest/test_case.rb', line 94

def after(&block)
  warn "[DEPRECATED] `after` alias is deprecated. Use `teardown` instead."
  teardown(&block)
end

.before(&block) ⇒ Object



89
90
91
92
# File 'lib/protest/test_case.rb', line 89

def before(&block)
  warn "[DEPRECATED] `before` alias is deprecated. Use `setup` instead."
  setup(&block)
end

.context(description, location = caller.at(0), &block) ⇒ Object Also known as: describe

Define a new test context nested under the current one. All setup and teardown blocks defined on the current context will be inherited by the new context. This method is aliased as describe for your comfort.



62
63
64
65
66
67
68
# File 'lib/protest/test_case.rb', line 62

def self.context(description, location = caller.at(0), &block)
  subclass = Class.new(self)
  subclass.class_eval(&block) if block
  subclass.description = description
  subclass.location = location
  const_set(sanitize_description(description), subclass)
end

.filenameObject



103
104
105
# File 'lib/protest/test_case.rb', line 103

def filename
  location.match(/:/).pre_match
end

.itObject

Add a test to be run in this context. This method is aliased as it and should for your comfort.



76
77
78
# File 'lib/protest/test_case.rb', line 76

def self.test(name, &block)
  tests << new(name, caller.at(0), &block)
end

.line_numberObject



99
100
101
# File 'lib/protest/test_case.rb', line 99

def line_number
  Integer(location.match(/:/).post_match[/^\d+/])
end

.scenario(name, &block) ⇒ Object



84
85
86
87
# File 'lib/protest/test_case.rb', line 84

def scenario(name, &block)
  warn "[DEPRECATED] `scenario` alias is deprecated. Use `test`, `it` or `should` instead."
  test(name, &block)
end

.setup(&block) ⇒ Object

Add a setup block to be run before each test in this context.



44
45
46
47
48
49
# File 'lib/protest/test_case.rb', line 44

def self.setup(&block)
  define_method :setup do
    super()
    instance_eval(&block)
  end
end

.shouldObject

Add a test to be run in this context. This method is aliased as it and should for your comfort.



77
78
79
# File 'lib/protest/test_case.rb', line 77

def self.test(name, &block)
  tests << new(name, caller.at(0), &block)
end

.story(description, &block) ⇒ Object



79
80
81
82
# File 'lib/protest/test_case.rb', line 79

def story(description, &block)
  warn "[DEPRECATED] `story` alias is deprecated. Use `describe` or `context` instead."
  context(description, &block)
end

.teardown(&block) ⇒ Object

Add a teardown block to be run after each test in this context.



52
53
54
55
56
57
# File 'lib/protest/test_case.rb', line 52

def self.teardown(&block)
  define_method :teardown do
    instance_eval(&block)
    super()
  end
end

.test(name, &block) ⇒ Object

Add a test to be run in this context. This method is aliased as it and should for your comfort.



39
40
41
# File 'lib/protest/test_case.rb', line 39

def self.test(name, &block)
  tests << new(name, caller.at(0), &block)
end

.testsObject

Tests added to this context.



33
34
35
# File 'lib/protest/test_case.rb', line 33

def self.tests
  @tests ||= []
end

Instance Method Details

#assert(condition, message = "Expected condition to be satisfied") ⇒ Object

Ensure a condition is met. This will raise AssertionFailed if the condition isn’t met. You can override the default failure message by passing it as an argument.

Raises:



139
140
141
142
# File 'lib/protest/test_case.rb', line 139

def assert(condition, message="Expected condition to be satisfied")
  @report.on_assertion
  raise AssertionFailed, message unless condition
end

#assert_equal(expected, actual, message = nil) ⇒ Object

Passes if expected == actual. You can override the default failure message by passing it as an argument.



146
147
148
# File 'lib/protest/test_case.rb', line 146

def assert_equal(expected, actual, message=nil)
  assert expected == actual, message || "#{expected.inspect} expected but was #{actual.inspect}"
end

#assert_raise(exception_class = Exception, message = nil) ⇒ Object

Passes if the code block raises the specified exception. If no exception is specified, passes if any exception is raised, otherwise it fails. You can override the default failure message by passing it as an argument.



154
155
156
157
158
159
160
161
# File 'lib/protest/test_case.rb', line 154

def assert_raise(exception_class=Exception, message=nil)
  begin
    yield
  rescue exception_class => e
  ensure
    assert e, message || "Expected #{exception_class.name} to be raised"
  end
end

#line_numberObject



174
175
176
# File 'lib/protest/test_case.rb', line 174

def line_number
  Integer(@location.match(/:/).post_match[/^\d+/])
end

#nameObject

Name of the test



170
171
172
# File 'lib/protest/test_case.rb', line 170

def name
  @name
end

#pending(message = "Not Yet Implemented") ⇒ Object

Make the test be ignored as pending. You can override the default message that will be sent to the report by passing it as an argument.

Raises:



165
166
167
# File 'lib/protest/test_case.rb', line 165

def pending(message="Not Yet Implemented")
  raise Pending, message, [@location, *caller].uniq
end

#run(report) ⇒ Object

Run a test in isolation. Any setup and teardown blocks defined for this test case will be run as expected.

You need to provide a Runner instance to handle errors/pending tests/etc.

If the test’s block is nil, then the test will be marked as pending and nothing will be run.



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/protest/test_case.rb', line 123

def run(report)
  @report = report
  pending if test.nil?

  begin
    setup
    instance_eval(&test)
  ensure
    teardown
    @report = nil
  end
end