当前位置: 首页 > 知识库问答 >
问题:

@在Cucumber中的场景大纲末尾多次调用After

孟沛
2023-03-14

我的cucumber小cucumber看起来像这样:

Feature: Story XYZ- Title of Story
  """
  Title: Story XYZ- Title of Story  
  """

  Background: 
    Given Appointments are being created using "SOAP" API

  @scenario1
  Scenario Outline: Create an appointment for a New start service order    
    Given Appointment does not exist for order id "Test_PR_Order123"
    When Request for create appointment is received for order "Test_PR_Order123" and following
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    Then Appointment ID should be created
    And Appointment for order "Test_PR_Order123" should have following details
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    And Appointment history should exist for "Create Appointment"

Examples: 
  | ServiceType | ServiceGroupName      |
  | Service1    | ServiceGroup1         |
  | Service2    | ServiceGroup2         |

@scenario22
  Scenario Outline: Create an appointment for a Change Service order
    Given Appointment does not exist for order id "Test_CH_Order123"
    When Request for create appointment is received for order "Test_CH_Order123" and following
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    Then Appointment ID should be created
    And Appointment for order "Test_CH_Order123" should have following details
      | FieldName        | FieldValue         |
      | ServiceGroupName | <ServiceGroupName> |
      | SerivceGroupID   | TestSG123          |
      | ServiceType      | <ServiceType>      |
    And Appointment history should exist for "Create Appointment"

Examples: 
  | ServiceType | ServiceGroupName      |
  | Service1    | ServiceGroup1         |
  | Service2    | ServiceGroup2         |

在上面的功能中,有一个背景,它将为两个场景大纲中的每个示例执行。此外,在java实现中,我们已经实现了@After和@Before钩子,它们也将针对每个示例执行。

我们正在使用spring Cumber进行步骤之间的数据注入。

当第一个场景大纲中的所有示例结束时,调用@After实现的方法两次,就会出现问题。当第二次@After同时启动时,第二个场景大纲示例开始执行。因此,场景的顺序执行受到干扰,自动化开始失败。

请建议,如果这是cucumber的一个错误,或者我们错过了什么。

共有1个答案

璩正志
2023-03-14

你缺少的很多东西之一就是保持场景简单。通过使用场景大纲,在你的小cucumber中嵌入如此多的技术细节,你会让事情变得更加困难。此外,你还可以使用前后挂钩来完成这项工作。

另一个问题是你的场景没有意义。它们都是关于为订单预约,但你在任何时候都不会创建订单。

最后,你有两个相同的场景,你说做不同的事情。第一个是新的,第二个是改变。必须有一些不同,否则你就不需要第二种情况。

我要做的是尝试从这个混乱中提取一个场景,并用它来诊断任何问题。你应该能得到这样的结果

Scenario: Create an appointment for an order
  Given there is an order
  And appointments are made using SOAP
  When a new start appointment is made for the order
  Then the appointment should be created
  And the appointment should have a history

如果没有任何@before或@after,就没有理由不能让这个场景工作。当你有了这个工作,然后创建其他场景,不管你试图检查的是什么其他情况。同样,你应该能够做到这一点,而无需做以下任何一项

  1. 使用示例数据区分场景
  2. 使用大纲
  3. 使用之前和之后

在使用Cucumber时,您希望将自动化的复杂性推到Cucumber之外。要么在Cucumber启动之前将其向上拉到脚本中,要么向下推以在单步定义中调用的helper方法执行。如果你在Cucumber中保持复杂性,尤其是尝试将场景相互链接,并使用@before和@after在场景之间保持状态,那么使用(误用)Cucumber就不会有愉快的时光。

比起cucumber,你的问题更可能是由你的代码引起的。即使Cucumber确实有轮廓和挂钩的问题,你也可以通过不使用它们来解决问题。轮廓是完全不必要的,挂钩大多被滥用。

 类似资料: