You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.1 kB

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

No Description

Contributors (1)