将 SpecFlow 测试构建为功能

Structuring SpecFlow tests as features

鉴于 Before/After-Scenario 是实例方法属性,而 Before/After-Feature 是静态方法属性,是否有一种方法可以在功能文件中构建场景,以便它们可以相互受益?

我假设如果没有办法保证场景的执行顺序那么这将是一个纯学术问题?

补充:当一个feature文件有多个场景时,后台场景是每个场景执行一次还是每个feature执行一次?这会使上述问题的答案复杂化,我认为(?)

背景

背景是每个场景 运行 一次(该功能中的每个场景),即:

Feature: Hello World

Background:
   Given I go to the homepage
   When I search for "Hello, World!"

Scenario: See a result
   Then I should see a result

Scenario: See multiple results
   Then I should see a maximum of 10 results

写法一样:

Feature: Hello World

Scenario: See a result
   Given I go to the homepage
   When I search for "Hello, World!"
   Then I should see a result

Scenario: See multiple results
   Given I go to the homepage
   When I search for "Hello, World!"
   Then I should see a maximum of 10 results

依赖其他场景的场景

场景之间根本不应该相互依赖。测试的顺序 运行 应该能够在不影响测试功能的情况下改变。

这意味着在每个场景的基础上设置测试。这方面的一个例子是背景(如上所示),或者在 "Given" 步骤中到达同一点。

如果我用上面的例子这样做:

Feature: Hello World

Scenario: See a result
   Given I have searched for "Hello, World!"
   Then I should see a result

Scenario: See multiple results
   Given I have searched for "Hello, World!"
   Then I should see a maximum of 10 results

而不是做这样的事情(把它划掉,这样就很清楚不要做这个):

Feature: Hello World

Scenario: See a result
   Given I have searched for "Hello, World!"
   Then I should see a result

Scenario: See multiple results
   Then I should see a maximum of 10 results