如何重构空手道测试以消除多次调用?

问题描述 投票:0回答:1

嗨,我正在参加空手道测试。在测试中,我向服务 b 发出 4 个请求,向服务 a 发出 1 个请求。我如何以紧凑的方式重构对服务 b 的 4 次调用

以下是我的测试

@BcomRetry
Feature: Message Routing u

  Background:
    * configure ssl = true
    * configure httpVersion = 'http2'
    * def servicebBaseUrls = ['http://localhost:28090', 'https://localhost:28091', 'http://localhost:28092', 'https://localhost:28093']
    * def serviceaBaseUrl = 'http://localhost:28081'
    * configure retry = { count: 100 }
  Scenario: Test7   
    * def serviceb1BaseUrl = karate.get('servicebBaseUrls[0]')
    * def serviceb2BaseUrl = karate.get('servicebBaseUrls[1]')
    * def serviceb3BaseUrl = karate.get('servicebBaseUrls[2]')
    * def serviceb4BaseUrl = karate.get('servicebBaseUrls[3]')    
    * def serviceaBaseUrl = karate.get('serviceaBaseUrl')
    * def requestBody = read('sba-local-policy/request.json')
    * def expectedResponse1 = read('sba-local-policy/test7EndpointLookupResponse.json')
     
    * url serviceb1BaseUrl
    Given path '/service-b/v1/id'
    When method get
    Then status 200
    And print response
    
    * url serviceb2BaseUrl
    Given path '/service-b/v1/id'
    When method get
    Then status 200
    And print response

    * url serviceb3BaseUrl
    Given path '/service-b/v1/id'
    When method get
    Then status 200
    And print response

    * url serviceb4BaseUrl
    Given path '/service-b/v1/id'
    When method get
    Then status 200
    And print response

    * url serviceaBaseUrl    
    Given path '/service-a/v1/testLoadbalancer'
    And header Accept = 'application/json; charset=utf-8'
    And header Content-Type = 'application/json; charset=utf-8'
    And request requestBody
    When method post
    Then status 200
    Then match response == expectedResponse1  

谢谢你

karate
1个回答
0
投票

这是处理 url 的一种方法。还有其他方法。查看我使用过的场景大纲。您还可以将背景部分中提到的内容移至 karate-config.js 以使功能文件更清晰。

@BcomRetry
Feature: Message Routing u

  Background:
    * configure ssl = true
    * configure httpVersion = 'http2'
    * def servicebBaseUrls = ['http://localhost:28090', 'https://localhost:28091', 'http://localhost:28092', 'https://localhost:28093']
    * def serviceaBaseUrl = 'http://localhost:28081'
    * configure retry = { count: 100 }
    * def serviceb1BaseUrl = karate.get('servicebBaseUrls[0]')
    * def serviceb2BaseUrl = karate.get('servicebBaseUrls[1]')
    * def serviceb3BaseUrl = karate.get('servicebBaseUrls[2]')
    * def serviceb4BaseUrl = karate.get('servicebBaseUrls[3]')
    * def serviceaBaseUrl = karate.get('serviceaBaseUrl')
    * def requestBody = read('sba-local-policy/request.json')
    * def expectedResponse1 = read('sba-local-policy/test7EndpointLookupResponse.json')

  Scenario Outline: Handle Multiple urls
    Given url <url>
    And path '/service-b/v1/id'
    When method get
    Then status 200
    And print response


    Examples:
      | url                       |
      | 'http://localhost:28090'  |
      | 'https://localhost:28091' |

    Scenario: Test 2

      * url serviceaBaseUrl
      Given path '/service-a/v1/testLoadbalancer'
      And header Accept = 'application/json; charset=utf-8'
      And header Content-Type = 'application/json; charset=utf-8'
      And request requestBody
      When method post
      Then status 200
      Then match response == expectedResponse1  
© www.soinside.com 2019 - 2024. All rights reserved.