首页 > 解决方案 > 通过重试重用功能文件,直到

问题描述

在空手道方面取得了惊人的成功。处理在 GET 上使用“重试直到”超时的端到端测试,以等待响应正文中的特定参数值。随着被测系统中数据处理的完成,预计该参数的状态会从 A 变为 B。.feature由于对每个 API 路由一个模型进行了标准化,因此有兴趣了解模式。然而,这只有在我们可以参数化这些retry until术语时才有可能。否则,这将意味着编写多个功能来支持不同的重试组合。

retry until---评论重用的例子---

get_notification_ref.feature要为每个until组合维护一个而不是一个,until请在调用中提供可由retry.feature 内部使用的外部参数。

依赖于在 .feature 文件中指定 until 参数的实现。最终得到每个重试组合的 GET Notification 功能文件:

Scenario: Get notification & wait for status
   * call read('classpath:NotifyV1/get_notification_ref_wait_status.feature')
   .
   .

Scenario: Get notification & wait for status indicator colour
   * def expectedColour = 'GREEN'
   * call read('classpath:NotifyV1/get_notification_ref_wait_colour.feature')

get_notification_ref_wait_status.feature

Scenario: Get notification and wait on response status = 200
    Given path 'notification', notificationTypeReference
    And retry until responseStatus == 200
    When method get
    * def notificationResponse = $

get_notification_ref_wait_colour.feature

Scenario: Get notification and wait on response status = 200 and colour
    Given path 'notification', notificationTypeReference
    And retry until responseStatus == 200 && response.statusColour == expectedColour
    When method get
    * def notificationResponse = $

上面的实现可以处理参数化重试直到看起来像这样 - 注意现在只有一个 GET 通知功能文件:

Scenario: Get notification & wait for status 200
   * call read('classpath:NotifyV1/get_notification_ref.feature')
   .
   .

Scenario: Get notification & wait for status 200 and indicator colour
   * def UntilTerm = function(response){ return karate.match(response, '{statusColour: "GREEN"}').pass }
   * call read('classpath:NotifyV1/get_notification_ref.feature')

get_notification_ref.feature

Scenario: Get notification
    * def untilTerm = karate.get('UntilTerm') ? UntilTerm : function(response){ return true }
    * def untilStatus = karate.get('UntilStatus') ? UntilStatus : 200
    Given path 'notification', notificationTypeReference
    And retry until responseStatus == untilStatus && untilTerm(response)
    When method get
    * def notificationResponse = $
    * karate.set('UntilTerm',null)
    * karate.set('UntilStatus',null)

标签: karateintuit

解决方案


我会说retry until可能就足够了。由于您可以调整默认时间和间隔,因此即使在需要时也可以进行不同的设置,即特定的 HTTP 调用:https ://github.com/intuit/karate#retry-until

除非你真的有办法让外部进程回调——在这种情况下你可以看看karate.signal()和朋友。否则我认为你最好坚持下去retry until


推荐阅读