class Protest::TestCase
A TestCase
defines a suite of related tests. You can further categorize your tests by declaring nested contexts inside the class. See TestCase.context
.
Attributes
Fancy name for your test case, reports can use this to give nice, descriptive output when running your tests.
Fancy name for your test case, reports can use this to give nice, descriptive output when running your tests.
Public Class Methods
# File lib/protest/test_case.rb, line 94 def after(&block) warn "[DEPRECATED] `after` alias is deprecated. Use `teardown` instead." teardown(&block) end
# File lib/protest/test_case.rb, line 89 def before(&block) warn "[DEPRECATED] `before` alias is deprecated. Use `setup` instead." setup(&block) end
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.
# 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
# File lib/protest/test_case.rb, line 103 def filename location.match(/:/).pre_match end
# File lib/protest/test_case.rb, line 99 def line_number Integer(location.match(/:/).post_match[/^\d+/]) end
Initialize a new instance of a single test. This test can be run in isolation by calling TestCase#run
.
# File lib/protest/test_case.rb, line 110 def initialize(name, location, &block) @test = block @location = location @name = name end
# 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
Add a setup block to be run before each test in this context.
# File lib/protest/test_case.rb, line 44 def self.setup(&block) define_method :setup do super() instance_eval(&block) end end
# 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
Add a teardown block to be run after each test in this context.
# File lib/protest/test_case.rb, line 52 def self.teardown(&block) define_method :teardown do instance_eval(&block) super() end end
Add a test to be run in this context. This method is aliased as it
and should
for your comfort.
# File lib/protest/test_case.rb, line 39 def self.test(name, &block) tests << new(name, caller.at(0), &block) end
Tests added to this context.
# File lib/protest/test_case.rb, line 33 def self.tests @tests ||= [] end
Private Class Methods
# File lib/protest/test_case.rb, line 200 def self.inherited(child) Protest.test_cases << child end
# File lib/protest/test_case.rb, line 190 def self.sanitize_description(description) "Test#{description.gsub(/\W+/, ' ').strip.gsub(/(^| )(\w)/) { $2.upcase }}".to_sym end
Public Instance Methods
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.
# 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
Passes if expected == actual. You can override the default failure message by passing it as an argument.
# 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
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.
# 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
# File lib/protest/test_case.rb, line 174 def line_number Integer(@location.match(/:/).post_match[/^\d+/]) end
Name of the test
# File lib/protest/test_case.rb, line 170 def name @name end
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.
# File lib/protest/test_case.rb, line 165 def pending(message="Not Yet Implemented") raise Pending, message, [@location, *caller].uniq end
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.
# 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
Private Instance Methods
# File lib/protest/test_case.rb, line 186 def test @test end