1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
Feature: After Hooks
After hooks can be used to clean up any state you've altered during your
scenario, or to check the status of the scenario and act accordingly.
You can ask a scenario whether it has failed, for example.
Mind you, even if it hasn't failed yet, you can still make the scenario
fail if your After hook throws an error.
Background:
Given the standard step definitions
Scenario Outline: Retrieve the status of a scenario as a symbol
Given a file named "features/support/debug_hook.rb" with:
"""
After do |scenario|
puts scenario.status.inspect
end
"""
And a file named "features/result.feature" with:
"""
Feature:
Scenario:
Given this step <result>
"""
When I run `cucumber -f progress`
Then the output should contain "<status symbol>"
Examples:
| result | status symbol |
| passes | :passed |
| fails | :failed |
| is pending | :pending |
Scenario: Check the failed status of a scenario in a hook
Given a file named "features/support/debug_hook.rb" with:
"""
After do |scenario|
if scenario.failed?
puts "eek"
end
end
"""
And a file named "features/fail.feature" with:
"""
Feature:
Scenario:
Given this step fails
"""
When I run `cucumber -f progress`
Then the output should contain:
"""
eek
"""
Scenario: Make a scenario fail from an After hook
Given a file named "features/support/bad_hook.rb" with:
"""
After do
fail 'yikes'
end
"""
And a file named "features/pass.feature" with:
"""
Feature:
Scenario:
Given this step passes
"""
When I run `cucumber -f pretty`
Then it should fail with:
"""
Scenario: # features/pass.feature:2
Given this step passes # features/step_definitions/steps.rb:1
yikes (RuntimeError)
./features/support/bad_hook.rb:2:in `After'
"""
Scenario: After hooks are executed in reverse order of definition
Given a file named "features/support/hooks.rb" with:
"""
After do
puts "First"
end
After do
puts "Second"
end
"""
And a file named "features/pass.feature" with:
"""
Feature:
Scenario:
Given this step passes
"""
When I run `cucumber -f progress`
Then the output should contain:
"""
Second
First
"""
|