简体   繁体   English

黄瓜后钩

[英]The After hook in cucumber

Folks, 伙计们,

I am having some trouble working with the After hook. 使用After挂钩时遇到一些麻烦。 I have organized my tests in folders like this: 我在这样的文件夹中组织了测试:

features/Accounts/accounts_api.feature
features/Accounts/step_definition/account_steps.rb

features/labs/create_lab.feature
features/labs/step_definition/labs_steps.rb

Now I have an After hook present in the step definition of the Accounts feature, I want that hook to run after every scenario of the "Accounts" feature, but I do not want it to run after every scenario of the "labs" feature. 现在,在“帐户”功能的步骤定义中有一个After挂钩,我希望该挂钩在“帐户”功能的每个方案之后运行,但是我不希望它在“实验室”功能的每个方案之后运行。 I tried this: 我尝试了这个:

cucumber --tags @newlabs

the above should run all the scenarios present in the labs feature tagged as newlabs but what I am seeing is that once the scenario tagged as @newlabs runs the @after hook present in the step definition of Accounts starts to run. 上面的代码应该运行标记为newlabs的实验室功能中存在的所有场景,但是我看到的是,一旦标记为@newlabs的场景运行了@after步骤定义中存在的@after钩子,就开始运行。 I am thinking why is this happening, am I using the hook in the wrong way or is my overall understanding of hooks wrong? 我在想为什么会这样,是我以错误的方式使用了钩子,还是我对钩子的整体理解是错误的?

Thanks a lot for taking the time to respond, this helps a lot. 非常感谢您抽出宝贵的时间来答复,这很有帮助。

Hooks don't care what step definition script they're located in and will run for every scenario. 挂钩并不关心它们位于哪个步骤定义脚本中,并且可以在每种情况下运行。 Or, more specifically, your after hook will run after every scenario that runs, for every feature, regardless of the tags you pass in to Cucumber. 或者,更具体地说,无论传递给Cucumber的标签如何,对于每个功能,您的after钩子都将在每个场景运行之后运行。

If you want a little more control over that, check out the Cucumber wiki page on hooks and look in the section called 'Tagged hooks'. 如果要对此进行更多控制,请查看关于钩子Cucumber Wiki页面,并查看“标记的钩子”部分。

Possibly you define After hook in wrong place. 可能是您在错误的位置定义了After hook。 Note that After hook (as well as other hooks) must be defined in the .rb , not in the .feature file. 请注意, After钩子(以及其他钩子)必须在.rb定义,而不是在.feature文件中定义。 Common place for hooks is features/support/hooks.rb . 挂钩的常见位置是features/support/hooks.rb You will define your hook this way: 您将通过以下方式定义钩子:

# features/support/hooks.rb
After('@newlabs') do # will run after each scenario tagged with @newlabs 
   # your teardown ruby code 
end

# features/Accounts/accounts_api.feature
@newlabs # tag all scenarious of this feature with @newlabs tag
Feature: your feature

Scenario: your scenario
  Given: ...
  When: ...
  Then: ...

In cucumber output you won't see that After hook is executed (unless you output something to STDOUT from the hook definition) - hooks will run implicitly. 在黄瓜输出中,您将不会看到执行钩子After (除非您从钩子定义中向STDOUT )- 钩子将隐式运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM