是否有像Background这样的After关键字可用于运行黄瓜步骤

mma*_*mar 7 cucumber gherkin cucumber-jvm atdd

我知道背景关键字可用于在运行每个场景之前运行常见步骤。同样,是否有类似“After”关键字的内容可用于每个场景之后的公共步骤,而不是像 after hooks 这样的 java 代码中的逻辑步骤,我的意思是在小黄瓜步骤本身中。我需要像下面这样

Background
Given I use the API header information
  | Content-Type | application/json;v=3 |
And I connect to postgresql

Scenario Outline:
    And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
    And I store the input payload individual field details for database validation
    And I form a client with this resource url "/transaction"
    When I make a POST call and capture the response
    And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"


Examples:
| HTTPCode |
| 200      |

After
Then I validate the output response with expected data
And I verify the HTTP error code is "<HTTPCode>"
And I fetch and validate latest created data from "transaction" table
And I validate the created card is inserted into "field" table
Run Code Online (Sandbox Code Playgroud)

Dan*_*riu 1

简短的回答,您可能可以使用After钩子,更多信息请参见此处建议您使用它。BDD 用于与非技术利益相关者进行沟通

从您编写场景大纲的方式来看,这似乎只运行一次,如果响应的形式不同200,那么最后两个步骤将失败(甚至第一个步骤Then)。

如果您唯一需要检查的是快乐流程(其中响应为 )200,则不需要Scenario Outlinewith Examples。只需创建一个场景。

Background
Given I use the API header information
  | Content-Type | application/json;v=3 |
And I connect to postgresql

Scenario: Happy flow
    And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
    And I store the input payload individual field details for database validation
    And I form a client with this resource url "/transaction"
    When I make a POST call and capture the response
    And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"
    Then I validate the output response with expected data
    And I verify the HTTP error code is "200"
    And I fetch and validate latest created data from "transaction" table
    And I validate the created card is inserted into "field" table
Run Code Online (Sandbox Code Playgroud)

如果您希望添加更多响应代码,那么以不同的方式重写场景大纲可能是个好主意。After您的验证不需要关键字(Then步骤)。无论如何,在使用场景大纲时,您只需要编写一次它们。

Background
Given I use the API header information
  | Content-Type | application/json;v=3 |
And I connect to postgresql

Scenario Outline:
    And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
    And I store the input payload individual field details for database validation
    And I form a client with this resource url "/transaction"
    When I make a POST call and capture the response
    And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"
    Then I validate the output response with expected data
    And I verify the HTTP error code is "<HTTPCode>"
    And I fetch and validate latest created data from "transaction" table
    And I validate the created card is inserted into "field" table

Examples:
| HTTPCode |
| 200      |
Run Code Online (Sandbox Code Playgroud)

请记住,如果您要添加更多响应代码,则需要以不同的方式管理最后的步骤。

隐藏细节(​​例如,| Content-Type | application/json;v=3 |在步骤定义中管理它们)也可能是一个好主意。

更新:

我从您的评论中了解到,您希望将最后 4 个步骤(Then步骤)仅在功能文件以及使用它们的所有场景中编写一次。

据我所知,在Gherkin语言中,没有After一个可以执行与前提条件相同的验证步骤。Background

有一种方法可以简化这个过程,但它会降低可读性。例如,如果您有 10 个场景和 2 个场景大纲使用完全相同的四个Then步骤,那么您可以尝试将所有这些场景嵌套在一个更通用的步骤中,如果步骤来自不同的步骤定义文件,那么您可以使用picocontainer对它们进行分组并减少在功能文件中调用它们的次数。更多详细信息请参见此处

问题是你的Then步骤有点复杂,只能用一个甚至两个更简单的步骤来编写,因为你有 3 个参数和 5 个验证。

总而言之,我认为最好为每个场景/场景大纲编写它们。对于其他人来说,我很难在查看功能文件时看不到任何内容Then,而只是在底部找到它们。更好的方法是尝试将更多场景分组到场景大纲中,这样步骤就不会重复太多。

希望能帮助到你!