|
| 1 | +# Cucumber Hooks |
| 2 | + |
| 3 | +Cucumber proposes several hooks to let you specify some code to be executed at |
| 4 | +different stages of test execution, like before or after the execution of a |
| 5 | +scenario. |
| 6 | + |
| 7 | +Hooks are part of your support code. |
| 8 | + |
| 9 | +They are executed in the following order: |
| 10 | + |
| 11 | +- [AfterConfiguration](#afterconfiguration-and-installplugin) |
| 12 | +- [InstallPlugin](#afterconfiguration-and-installplugin) |
| 13 | +- [BeforeAll](#beforeall-and-afterall) |
| 14 | + - Per scenario: |
| 15 | + - [Around](#around) |
| 16 | + - [Before](#before-and-after) |
| 17 | + - Per step: |
| 18 | + - [AfterStep](#afterstep) |
| 19 | + - [After](#before-and-after) |
| 20 | +- [AfterAll](#beforeall-and-afterall) |
| 21 | + |
| 22 | +You can define as many hooks as you want. If you have several hooks of the same |
| 23 | +types - for example, several `BeforeAll` hooks - they will be all executed once. |
| 24 | + |
| 25 | +Multiple hooks of the same type are executed in the order that they were defined. |
| 26 | +If you wish to control this order, use manual requires in `env.rb` - This file is |
| 27 | +loaded first - or migrate them all to one `hooks.rb` file. |
| 28 | + |
| 29 | +## AfterConfiguration and InstallPlugin |
| 30 | + |
| 31 | +[`AfterConfiguration`](#afterconfiguration) and [`InstallPlugin`](#installplugin) |
| 32 | +hooks are dedicated to plugins and are meant to extend Cucumber. For example, |
| 33 | +[`AfterConfiguration`](#afterconfiguration) allows you to dynamically change the |
| 34 | +configuration before the execution of the tests, and [`InstallPlugin`](#installplugin) |
| 35 | +allows to have some code that would have deeper impact on the execution. |
| 36 | + |
| 37 | +### AfterConfiguration |
| 38 | + |
| 39 | +**Note:** this is a legacy hook. You may consider using [`InstallPlugin`](#installplugin) instead. |
| 40 | + |
| 41 | +```ruby |
| 42 | +AfterConfiguration do |configuration| |
| 43 | + # configuration is an instance of Cucumber::Configuration defined in |
| 44 | + # lib/cucumber/configuration.rb. |
| 45 | +end |
| 46 | +``` |
| 47 | + |
| 48 | +### InstallPlugin |
| 49 | + |
| 50 | +In addition to the configuration, `InstallPlugin` also has access to some of Cucumber |
| 51 | +internals through a `RegistryWrapper`, defined in |
| 52 | +[lib/cucumber/glue/registry_wrapper.rb](../../../../lib/cucumber/glue/registry_wrapper.rb). |
| 53 | + |
| 54 | +```ruby |
| 55 | +InstallPlugin do |configuration, registry| |
| 56 | + # configuration is an instance of Cucumber::Configuration defined in |
| 57 | + # lib/cucumber/configuration.rb |
| 58 | + # |
| 59 | + # registry is an instance of Cucumber::Glue::RegistryWrapper defined in |
| 60 | + # lib/cucumber/glue/registry_wrapper.rb |
| 61 | +end |
| 62 | +``` |
| 63 | + |
| 64 | +You can see an example in the [Cucumber Wire plugin](https://github.com/cucumber/cucumber-ruby-wire). |
| 65 | + |
| 66 | +## BeforeAll and AfterAll |
| 67 | + |
| 68 | +`BeforeAll` is executed once before the execution of the first scenario. `AfterAll` |
| 69 | +is executed once after the execution of the last scenario. |
| 70 | + |
| 71 | +These two types of hooks have no parameters. Their purpose is to set-up and/or clean-up |
| 72 | +your environment not related to Cucumber, like a database or a browser. |
| 73 | + |
| 74 | +```ruby |
| 75 | +BeforeAll do |
| 76 | + # snip |
| 77 | +end |
| 78 | + |
| 79 | +AfterAll do |
| 80 | + # snip |
| 81 | +end |
| 82 | +``` |
| 83 | + |
| 84 | +## Around |
| 85 | + |
| 86 | +**Note:** `Around` is a legacy hook and its usage is discouraged in favor of |
| 87 | +[`Before` and `After`](#before-and-after) hooks. |
| 88 | + |
| 89 | +`Around` is a special hook which allows you to have a block syntax. Its original |
| 90 | +purpose was to support some databases with only block syntax for transactions. |
| 91 | + |
| 92 | +```ruby |
| 93 | +Around do |scenario, block| |
| 94 | + SomeDatabase::begin_transaction do # this is just for illustration |
| 95 | + block.call |
| 96 | + end |
| 97 | +end |
| 98 | +``` |
| 99 | + |
| 100 | +## Before and After |
| 101 | + |
| 102 | +`Before` is executed before each test case. `After` is executed after each test case. |
| 103 | +They both have the test case being executed as a parameter. Within the `After` hook, |
| 104 | +the status of the test case is also available. |
| 105 | + |
| 106 | +```ruby |
| 107 | +Before do |test_case| |
| 108 | + log test_case.name |
| 109 | +end |
| 110 | + |
| 111 | +After do |test_case| |
| 112 | + log test_case.failed? |
| 113 | + log test_case.status |
| 114 | +end |
| 115 | +``` |
| 116 | + |
| 117 | +## AfterStep |
| 118 | + |
| 119 | +`AfterStep` is executed after each step of a test. If steps are not executed due |
| 120 | +to a previous failure, `AfterStep` won't be executed either. |
| 121 | + |
| 122 | +```ruby |
| 123 | +AfterStep do |result, test_step| |
| 124 | + log test_step.inspect # test_step is a Cucumber::Core::Test::Step |
| 125 | + log result.inspect # result is a Cucumber::Core::Test::Result |
| 126 | +end |
| 127 | +``` |
0 commit comments