Cucumber:在每次执行场景大纲之前是否有背景运行?

Cucumber: Does a Background run before each execution of a Scenario Outline?

给定如下特征文件:

Feature: Coffee improves mood in the background

  Background:
    Given the user drank coffee

  Scenario Outline: Coffee changes peoples moods
    Then user <USER> should be <MOOD>

    Examples:
      | USER     | MOOD         |
      |  Michael |  happy       |
      |  Elvis   |  electrified |
      |  John    |  sad         |

后台测试应该"the user drank coffee"运行1次,还是3次?

看看下面做作的答案。在输出中,您会看到后台步骤执行了 3 次。

feature.rb:

Feature: Does a background run before each scenario outline?

  Background:
    When I'm a background step

  Scenario Outline: foo
    Then I should print '<count>'

    Examples:
      |count|
      | 10  |
      | 20  |     
      | 30  |   

step_def.rb:

When(/^I'm a background step$/) do
  print "BACKGROUND EXECUTED"
end

Then(/^I should print '(\d+)'$/) do |num|
  # empty step
end

输出:

Feature: Does a background run before each scenario outline

BACKGROUND EXECUTED  Background:                  # features/login.feature:3
    When I'm a background step # features/step_definitions/login.rb:1

  Scenario Outline: foo           # features/login.feature:6
    Then I should print '<count>' # features/login.feature:7

    Examples:
      | count |
      | 10    |
BACKGROUND EXECUTED      | 20    |
BACKGROUND EXECUTED      | 30    |

3 scenarios (3 passed)
6 steps (6 passed)