是否有任何After关键字可用于运行黄瓜步骤的背景

时间:2017-09-27 20:17:34

标签: cucumber cucumber-jvm gherkin atdd

我知道后台关键字可用于在运行每个方案之前运行常见步骤。同样有" After"关键字可用于每个场景后的公共步骤,而不是像Java钩子之类的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

1 个答案:

答案 0 :(得分:0)

简短的回答,您可以使用After挂钩,更多关于here,但 推荐您的情况。 BDD用于与非技术利益相关者沟通

从您编写方案大纲的方式来看,似乎只运行一次,如果响应形式为200,则最后两步将失败(甚至是第一个Then )。

如果您需要检查的唯一事项是响应为200的快乐流程,则Scenario Outline不需要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步骤)仅在特征文件中写入一次并使用所有方案。

据我所知,没有After可以执行与Background相同的验证步骤,对于前提条件,使用Gherkin语言。

有一种方法可以简化这一过程,但会降低可重复性。例如,如果您有10个场景和2个场景轮廓使用完全相同的四个Then步骤,那么您可以尝试将所有这些步骤嵌套在更常规的步骤中,如果步骤来自不同的步骤定义文件,那么您可以使用picocontainer对它们进行分组,并减少在要素文件中调用它们的次数。更多详情here

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

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

希望它有所帮助!