空手道重复API调用 [英] Karate Repeat API Call

查看:123
本文介绍了空手道重复API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用空手道对微服务进行后端测试.我希望能够对后端API进行N次调用,其中N可以配置为数字而不必做难看的事情.

We're using Karate for backend testing of a microservice. I'd like to be able to make N calls to the backend API, where N is configurable as a number without having to do ugly things.

这是我的第一种方法:

    Given url baseUrl
    And headers HEADERS
    When method get
    Then status 200

    Given url baseUrl
    And headers HEADERS
    When method get
    Then status 200

    Given url baseUrl
    And headers HEADERS
    When method get
    Then status 200

(只是重复通话),它可以工作,但是扩展性很差(想象其中的1000个).

(Just repeating the call) It works, but obviously does not scale well (imagine 1000 of these).

下一种方法要好一些-我将呼叫置于单独的功能中,并使用了 https://github.com/intuit/karate#data-driven-features 方法:

Next approach was a bit better - I put the call in a separate feature and used the https://github.com/intuit/karate#data-driven-features approach:

    * table jwts
      | headers |
      | HEADERS |
      | HEADERS |
      | HEADERS |
      | HEADERS |
      | HEADERS |

    * def result = call read('call-once.feature') jwts

稍微好一点,但仍然无法扩展.我们还尝试了karate.repeat()的变体,这似乎是最自然的方法,但是语法有问题.我找不到的示例都没有在for-each内部进行API调用.

Slightly better but still does not scale. We also tried varieties of karate.repeat() which seems like the most natural approach, but had trouble with the syntax. None of the examples I could find had an API call inside of a for-each.

* def callFunction = function (HEADERS) { read('call-putaway-once.feature'); { HEADERS: '#(HEADERS)'} }
* def result = karate.repeat(5, callFunction)

但无法获得任何类似的效果.

But couldn't get any varieties of that working.

谁能提供一个示例,说明如何将相同的空手道线重复N次?我真的在寻找类似的东西:

Can anyone provide an example of how to repeat the same exact Karate lines N times? I'm really looking for something like:

for (int i = 0; i < numTimes; i++) {
    Given url baseUrl
    And headers HEADERS
    When method get
    Then status 200
}

(或功能等效).

谢谢!

推荐答案

在这里.首先,第二个called.feature:

Here you go. First, the second called.feature:

@ignore
Feature:

Scenario:
Given url 'http://httpbin.org'
And path 'headers'
And header X-Karate = count
When method get
Then status 200

现在您可以在第一个功能中做到这一点:

And now you can do this in your first feature:

* def fun = function(x){ return { count: x } }
* def data = karate.repeat(5, fun)
* call read('called.feature') data

P.S.通过在自述文件中搜索轮询"的方式,循环中有一个API调用的示例:

P.S. by the way search the readme for "polling", there is an example of an API call in a loop: polling.feature

这篇关于空手道重复API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆