Cucumber 黄瓜场景的清理步骤

Cucumber 黄瓜场景的清理步骤,cucumber,Cucumber,有没有办法为Cucumber中的某个功能的所有场景定义清理步骤?我知道Background用于定义每个场景的设置步骤,但是有没有办法定义在每个场景结束时发生的类似情况?您可以使用After,它将在每个场景之后运行: After do ## teardown code end Before do ## setup code end 还有一个Before钩子,允许您在场景之前设置状态和/或测试数据: After do ## teardown code end Before do

有没有办法为Cucumber中的某个功能的所有场景定义清理步骤?我知道
Background
用于定义每个场景的设置步骤,但是有没有办法定义在每个场景结束时发生的类似情况?

您可以使用After,它将在每个场景之后运行:

After do
  ## teardown code
end
Before do
  ## setup code
end
还有一个Before钩子,允许您在场景之前设置状态和/或测试数据:

After do
  ## teardown code
end
Before do
  ## setup code
end

前后挂钩提供了
Test::Unit
中的
setup
teardown
功能,它们通常位于
features/support
目录中的
hooks.rb
中。

还应注意,“Before”和“After”是全局钩子,即这些钩子针对功能文件中的每个场景运行

如果您希望只为几个测试用例(按标记分组)运行安装和拆卸,那么您需要使用taggedHooks,其中语法是

Before('@cucumis, @sativus') do
# This will only run before scenarios tagged
# with @cucumis OR @sativus.
end


AfterStep('@cucumis', '@sativus') do
# This will only run after steps within scenarios tagged
# with @cucumis AND @sativus.
end
有关更多信息: