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.



158
159
160
161
162
# File 'lib/protest/test_case.rb', line 158

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.



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

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.



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

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.



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

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.



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

def self.context(description, &block)
  subclass = Class.new(self)
  (class << subclass; self; end).class_eval do
    def do_global_setup; end
    def do_global_teardown; end
  end
  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.



86
87
88
89
90
91
92
93
# File 'lib/protest/test_case.rb', line 86

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.



114
115
116
117
118
119
120
121
# File 'lib/protest/test_case.rb', line 114

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.



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

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

.run(runner, sub_test_cases) ⇒ Object

Run all tests in this context, then run any test cases under this test case. Takes a Report instance in order to provide output. global_setup will be run before the first test is run; global_teardown will be run after the final test is run (including any tests in child test cases).



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

def self.run(runner, sub_test_cases)
  runner.report(TestWrapper.new(:setup, self), true)
  tests.each {|test| runner.report(test, false) }
  sub_test_cases.each {|test_case| runner.run_test_case(test_case) }
  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.
  #warn "#{e.class}: #{e.message}"
  #warn e.backtrace.join("\n")
  return
end

.scenarioObject

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



153
154
155
# File 'lib/protest/test_case.rb', line 153

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.



65
66
67
68
69
70
# File 'lib/protest/test_case.rb', line 65

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.



152
153
154
# File 'lib/protest/test_case.rb', line 152

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.



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

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.



59
60
61
# File 'lib/protest/test_case.rb', line 59

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

.testsObject

Tests added to this context.



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

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:



184
185
186
187
# File 'lib/protest/test_case.rb', line 184

def assert(condition, message="Expected condition to be satisfied")
  @report.add_assertion
  raise AssertionFailed, (Proc === message ? message.call : 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.



191
192
193
# File 'lib/protest/test_case.rb', line 191

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

#context_descriptionObject



206
207
208
# File 'lib/protest/test_case.rb', line 206

def context_description
  self.class.description
end

#nameObject

Name of the test



202
203
204
# File 'lib/protest/test_case.rb', line 202

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:



197
198
199
# File 'lib/protest/test_case.rb', line 197

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.



171
172
173
174
175
176
177
178
179
# File 'lib/protest/test_case.rb', line 171

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

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