Class: Protest::TestCase
- Inherits:
-
Object
- Object
- Protest::TestCase
- 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
Defined Under Namespace
Classes: TestWrapper
Class Attribute Summary collapse
-
.description ⇒ Object
writeonly
Fancy name for your test case, reports can use this to give nice, descriptive output when running your tests.
Class Method Summary collapse
-
.after ⇒ Object
Add a teardown block to be run after each test in this context.
-
.before ⇒ Object
Add a setup block to be run before each test in this context.
-
.context(description, &block) ⇒ Object
(also: describe, story)
Define a new test context nested under the current one.
-
.global_setup(&block) ⇒ Object
(also: before_all, after_all)
Add a
setup
block that will be run once for the entire test case, before the first test is run. -
.global_teardown(&block) ⇒ Object
Add a
teardown
block that will be run once for the entire test case, after the last test is run. -
.it ⇒ Object
Add a test to be run in this context.
-
.run(runner) ⇒ Object
Run all tests in this context.
-
.scenario ⇒ Object
Add a test to be run in this context.
-
.setup(&block) ⇒ Object
Add a setup block to be run before each test in this context.
-
.should ⇒ Object
Add a test to be run in this context.
-
.teardown(&block) ⇒ Object
Add a teardown block to be run after each test in this context.
-
.test(name, &block) ⇒ Object
Add a test to be run in this context.
-
.tests ⇒ Object
Tests added to this context.
Instance Method Summary collapse
-
#assert(condition, message = "Expected condition to be satisfied") ⇒ Object
Ensure a condition is met.
-
#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.
-
#initialize(name, location, &block) ⇒ TestCase
constructor
Initialize a new instance of a single test.
-
#name ⇒ Object
Name of the test.
-
#pending(message = "Not Yet Implemented") ⇒ Object
Make the test be ignored as pending.
-
#run(report) ⇒ Object
Run a test in isolation.
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.
153 154 155 156 157 |
# File 'lib/protest/test_case.rb', line 153 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.
135 136 137 |
# File 'lib/protest/test_case.rb', line 135 def description=(value) @description = value end |
Class Method Details
.after ⇒ Object
Add a teardown block to be run after each test in this context. This method is aliased as after
for your comfort.
141 142 143 144 145 146 |
# File 'lib/protest/test_case.rb', line 141 def self.teardown(&block) define_method :teardown do instance_eval(&block) super() end end |
.before ⇒ Object
Add a setup block to be run before each test in this context. This method is aliased as before
for your comfort.
140 141 142 143 144 145 |
# File 'lib/protest/test_case.rb', line 140 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 127 128 129 130 |
# File 'lib/protest/test_case.rb', line 121 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.
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 |
.it ⇒ Object
Add a test to be run in this context. This method is aliased as it
and should
for your comfort.
146 147 148 |
# File 'lib/protest/test_case.rb', line 146 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 |
.scenario ⇒ Object
Add a test to be run in this context. This method is aliased as it
and should
for your comfort.
148 149 150 |
# File 'lib/protest/test_case.rb', line 148 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 |
.should ⇒ Object
Add a test to be run in this context. This method is aliased as it
and should
for your comfort.
147 148 149 |
# File 'lib/protest/test_case.rb', line 147 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 |
.tests ⇒ Object
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.
179 180 181 182 |
# File 'lib/protest/test_case.rb', line 179 def assert(condition, ="Expected condition to be satisfied") @report.add_assertion raise AssertionFailed, 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.
186 187 188 |
# File 'lib/protest/test_case.rb', line 186 def assert_block(="Expected condition to be satisified") #:nodoc: assert(yield, ) end |
#name ⇒ Object
Name of the test
197 198 199 |
# File 'lib/protest/test_case.rb', line 197 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.
192 193 194 |
# File 'lib/protest/test_case.rb', line 192 def pending(="Not Yet Implemented") raise Pending, , [@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.
166 167 168 169 170 171 172 173 174 |
# File 'lib/protest/test_case.rb', line 166 def run(report) @report = report pending if test.nil? setup instance_eval(&test) teardown @report = nil end |