5

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

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
4

1 回答 1

0

简短的回答,您可能可以使用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

如果您希望添加更多响应代码,那么以不同的方式重写场景大纲可能是个好主意。您的验证不需要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      |

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

| Content-Type | application/json;v=3 |在步骤定义中隐藏和管理这些细节也可能是一个好主意。

更新:

我从您的评论中收集到的是,您希望最后 4 个步骤(Then步骤)只在功能文件中编写一次,并在所有场景中使用它们。

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

有一种方法可以简化这一点,但它会降低可读性。例如,如果您有 10 个场景和 2 个场景大纲使用完全相同的四个Then步骤,那么您可以尝试将它们全部嵌套在一个更通用的步骤中,如果步骤来自不同的步骤定义文件,那么您可以使用picocontainer将它们分组并减少您在功能文件中调用它们的次数。更多细节在这里

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

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

希望能帮助到你!

于 2017-09-28T07:26:31.150 回答