cucumber java hooks_cucumber的hooks

Cucumber 的钩子允许在测试周期的不同阶段运行代码块。Before 和 After 钩子分别在每个场景开始前和结束后执行,可以根据需要进行细致控制。Around 钩子则包裹整个场景的执行,提供更灵活的控制。此外,可以通过标签来实现特定场景的钩子,用于特定情况,例如在持续集成中快速反馈失败。
摘要由CSDN通过智能技术生成

Cucumber provides a number of hooks which allow us to run blocks at various points in the Cucumber test cycle. You can put them in your support/env.rb file or any other file under the support directory, for example in a file called support/hooks.rb. There is no association between where the hook is defined and which scenario/step it is run for, but you can use tagged hooks (see below) if you want more fine grained control.

All defined hooks are run whenever the relevant event occurs.

Scenario hooks

Before hooks will be run before the first step of each scenario. They will run in the same order of which they are registered.

Before do

# Do something before each scenario.

end

Before do |scenario|

# The +scenario+ argument is optional, but if you use it, you can get the title,

# description, or name (title + description) of the scenario that is about to be

# executed.

Rails.logger.debug "Starting scenario: #{scenario.title}"

end

After hooks will be run after the last step of each scenario, even when there are failing, undefined, pending or skipped steps. They will run in the opposite order of which they are registered.

After do |scenario|

# Do something after each scenario.

# The +scenario+ argument is optional, but

# if you use it, you can inspect status with

# the #failed?, #passed? and #exception methods.

if(scenario.failed?)

subject = "[Project X] #{scenario.exception.message}"

send_failure_email(subject)

end

end

Here is another example, where we exit at the first failure (could be useful in some rare cases like Continuous Integration when fast feedback is important).

After do |s|

# Tell Cucumber to quit after this scenario is done - if it failed.

Cucumber.wants_to_quit = true if s.failed?

end

Around hooks will run “around” a scenario. This can be used to wrap the execution of a scenario in a block. The Around hook receives a scenario object and a block (Proc) object. The scenario will be executed when you invoke block.call.

The following example will cause scenarios tagged with @fast to fail if the execution takes longer than 0.5 seconds:

Around('@fast') do |scenario, block|

Timeout.timeout(0.5) do

block.call

end

end

You may want to take a look at SystemTimer if you want a more reliable timeout.

Step hooks

Warning: AfterStep hook does not work with scenarios which have backgrounds (cucumber 0.3.11)

AfterStep do |scenario|

# Do something after each step.

end

Tagged hooks

Sometimes you may want a certain hook to run only for certain scenarios. This can be achieved by associating a Before, After, Around or AfterStep hook with one or more tags. You can OR and AND tags in much the same way as you can when running Cucumber from the command line. Examples:

For OR tags, pass the tags in a single string comma separated:

Before('@cucumis, @sativus') do

# This will only run before scenarios tagged

# with @cucumis OR @sativus.

end

For AND tags, pass the tags as separate tag strings:

Before('@cucumis', '~@sativus') do

# This will only run before scenarios tagged

# with @cucumis AND NOT @sativus.

end

You create complex tag conditions using both OR and AND on tags:

Before('@cucumis, @sativus', '@aqua') do

# This will only run before scenarios tagged

# with (@cucumis OR @sativus) AND @aqua

end

After Step example:

AfterStep('@cucumis', '@sativus') do

# This will only run before steps within scenarios tagged

# with @cucumis AND @sativus.

end

Think twice before you use this feature, as whatever happens in hooks is invisible to people who only read the features. You should consider using background as a more explicit alternative if the setup should be readable by non-technical people.

Global hooks

If you want something to happen once before any scenario is run – just put that code at the top-level in your env.rb file (or any other file in your features/support directory. Use Kernel#at_exit for global teardown. Example:

my_heavy_object = HeavyObject.new

my_heavy_object.do_it

at_exit do

my_heavy_object.undo_it

end

Running a Before hook only once

If you have a hook you only want to run once, use a global variable:

Before do

if !$dunit

# do it

step "run the really slow log in method"

$dunit = true

end

end

AfterConfiguration

You may also provide an AfterConfiguration hook that will be run after Cucumber has been configured. The block you provide will be passed the cucumber configuration (an instance of Cucumber::Cli::Configuration). Example:

AfterConfiguration do |config|

puts "Features dwell in #{config.feature_dirs}"

end

This hook will run only once; after support has been loaded but before features are loaded. You can use this hook to extend Cucumber, for example you could affect how features are loaded or register custom formatters programatically.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值