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.

Direct Known Subclasses

Rails::TestCase

Defined Under Namespace

Classes: TestWrapper

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.



149
150
151
152
153
# File 'lib/protest/test_case.rb', line 149

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.



131
132
133
# File 'lib/protest/test_case.rb', line 131

def description=(value)
  @description = value
end

Class Method Details

.afterObject

Add a teardown block to be run after each test in this context. This method is aliased as after for your comfort.



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

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

.beforeObject

Add a setup block to be run before each test in this context. This method is aliased as before for your comfort.



136
137
138
139
140
141
# File 'lib/protest/test_case.rb', line 136

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

.context(description, &block) ⇒ Object Also known as: describe, story

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.



121
122
123
124
125
126
# File 'lib/protest/test_case.rb', line 121

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

.global_setup(&block) ⇒ Object Also known as: before_all, after_all

Add a setup block that will be run once for the entire test case, before the first test is run.

Keep in mind that while setup blocks are evaluated on the context of the test, and thus you can share state between them, your tests will not be able to access instance variables set in a global_setup block.

This is usually not needed (and generally using it is a code smell, since you could make a test dependent on the state of other tests, which is a huge problem), but it comes in handy when you need to do expensive operations in your test setup/teardown and the tests won’t modify the state set on this operations. For example, creating large amount of records in a database or filesystem, when your tests will only read these records.



81
82
83
84
85
86
87
88
# File 'lib/protest/test_case.rb', line 81

def self.global_setup(&block)
  (class << self; self; end).class_eval do
    define_method :do_global_setup do
      super()
      instance_eval(&block)
    end
  end
end

.global_teardown(&block) ⇒ Object

Add a teardown block that will be run once for the entire test case, after the last test is run.

Keep in mind that while teardown blocks are evaluated on the context of the test, and thus you can share state between the tests and the teardown blocks, you will not be able to access instance variables set in a test from your global_teardown block.

See TestCase.global_setup for a discussion on why these methods are best avoided unless you really need them and use them carefully.



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

def self.global_teardown(&block)
  (class << self; self; end).class_eval do
    define_method :do_global_teardown do
      instance_eval(&block)
      super()
    end
  end
end

.itObject

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



142
143
144
# File 'lib/protest/test_case.rb', line 142

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

.run(runner) ⇒ Object

Run all tests in this context. Takes a Report instance in order to provide output.



36
37
38
39
40
41
42
43
44
45
# File 'lib/protest/test_case.rb', line 36

def self.run(runner)
  runner.report(TestWrapper.new(:setup, self), true)
  tests.each {|test| runner.report(test, false) }
  runner.report(TestWrapper.new(:teardown, self), true)
rescue Exception => e
  # If any exception bubbles up here, then it means it was during the
  # global setup/teardown blocks, so let's just skip the rest of this
  # context.
  return
end

.scenarioObject

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



144
145
146
# File 'lib/protest/test_case.rb', line 144

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

.setup(&block) ⇒ Object

Add a setup block to be run before each test in this context. This method is aliased as before for your comfort.



60
61
62
63
64
65
# File 'lib/protest/test_case.rb', line 60

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.



143
144
145
# File 'lib/protest/test_case.rb', line 143

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

.teardown(&block) ⇒ Object

Add a teardown block to be run after each test in this context. This method is aliased as after for your comfort.



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

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.



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

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

.testsObject

Tests added to this context.



48
49
50
# File 'lib/protest/test_case.rb', line 48

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:



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

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

#assert_block(message = "Expected condition to be satisified") ⇒ Object

Provided for Test::Unit compatibility, this lets you include Test::Unit::Assertions and everything works seamlessly.



182
183
184
# File 'lib/protest/test_case.rb', line 182

def assert_block(message="Expected condition to be satisified") #:nodoc:
  assert(yield, message)
end

#nameObject

Name of the test



193
194
195
# File 'lib/protest/test_case.rb', line 193

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:



188
189
190
# File 'lib/protest/test_case.rb', line 188

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.



162
163
164
165
166
167
168
169
170
# File 'lib/protest/test_case.rb', line 162

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

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