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.



107
108
109
110
111
# File 'lib/protest/test_case.rb', line 107

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.



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

def description=(value)
  @description = value
end

Class Method Details

.after(&block) ⇒ Object



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

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

.before(&block) ⇒ Object



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

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

.context(description, &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.



68
69
70
71
72
73
# File 'lib/protest/test_case.rb', line 68

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

.it ⇒ Object

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



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

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

.run(runner) ⇒ Object

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



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

def self.run(runner)
  tests.each {|test| runner.report(test) }
end

.scenario(name, &block) ⇒ Object



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

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.



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

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.



82
83
84
# File 'lib/protest/test_case.rb', line 82

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

.story(description, &block) ⇒ Object



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

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.



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

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.



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

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

.tests ⇒ Object

Tests added to this context.



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

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:



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

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.



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

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.



151
152
153
154
155
156
157
158
# File 'lib/protest/test_case.rb', line 151

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

#name ⇒ Object

Name of the test



167
168
169
# File 'lib/protest/test_case.rb', line 167

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:



162
163
164
# File 'lib/protest/test_case.rb', line 162

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.



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/protest/test_case.rb', line 120

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

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