(黄瓜) Background 和 Before 标签有什么区别

(Cucumber) What's the difference between Background and Before tag

据我所知,@Before 挂钩在每个场景之前执行,现在我遇到了 Background 标签,但在我看来它与 @Before 标签具有完全相同的功能,除了这是自己的步骤。 谁能解释一下现实生活中的diff.s是什么?什么时候我必须使用背景而不是之前?

根据 Cucumber documentation,这是 Before 的作用:

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

下面是 Background 的作用:

Background allows you to add some context to the scenarios in a single feature. A Background is much like a scenario containing a number of steps. The difference is when it is run. The background is run before each of your scenarios but after any of your Before Hooks.

事实上,正如您已经注意到的,它们的结构有点不同。 通常的做法是按如下方式使用它们:

  • 当您为场景提供客户可读的先决条件时使用Background
  • 当您必须在场景之前进行一些技术设置时使用 Before

不过这里主要要理解的是操作顺序:

挂钩前 1 -> 挂钩前 2 -> ... -> 背景 -> 场景

它们只是代表了不同级别的前置条件。