我的自动化测试包括两个请求:
首先,一个POST批量请求修改3个“对象”的名称;
第二,向每个对象发出 GET 请求,以确认名称已被修改。这里的想法是通过使用“示例:”来使用数据驱动场景功能。
Scenario Outline: POST multiple objects names
* header Authorization = $authHeader
Given url $baseUrl
And path "/objects/ids/bulk"
And param list = "11,22,33"
And request {"Label":"ChangedName11;ChangedName22;ChangedName33"}
When method POST
Then status 200
* json response = response
* match response['Successful Requests'] == '#[3]'
* match each response['Successful Requests'] == {"Status": 200,"Resource ID": #number,"Message": "Resource updated!"}
* match response['Failed Requests'] == '#[0]'
* header Authorization = $authHeader
Given url $baseUrl
And path "/objects/ids/" + '<object>'
When method GET
Then status 200
* match response['Label'] == '<label>'
Examples:
| object | label
| 11 | ChangedName1
| 22 | ChangedName2
| 33 | ChangedName3
测试没有按照我预期的方式进行,因为它也发送了 3 次 POST 请求,而我想要的只是 1 个 POST 请求,然后是 3 个 GET 请求。
如果空手道不允许这样做,我想听听其他人对我的测试的最佳方法的意见。预先感谢您!
如果您需要使用第一个响应来驱动数据源,您甚至可以这样做:
Feature: using the results of an API call as a data-source
@setup
Scenario:
* url 'https://jsonplaceholder.typicode.com/users'
* method get
* def data = [{ id: 1, name: 'a' }, { id: 2, name: 'b' }]
Scenario Outline: id: ${id} | name: ${name}
* url `https://httpbin.org/anything/${id}`
* param name = name
* method get
Examples:
| karate.setup().data |
此处对此进行了解释:https://stackoverflow.com/a/76527245/143475
否则,不要使用
Scenario Outline
,将数据建模为 JSON 数组,然后你可以这样做:
Feature: mixing 2 http calls but the second one in a loop
Scenario:
* url 'https://jsonplaceholder.typicode.com/users'
* method post
* table data
| id | name |
| 1 | 'foo' |
| 2 | 'bar' |
* call read('@called') data
@ignore @called
Scenario:
* url `https://httpbin.org/anything/${id}`
* param name = name
* method get
此处对此进行了解释:https://github.com/karatelabs/karate#data-driven-features
上述方法的另一个示例解释如下:https://stackoverflow.com/a/72388475/143475
想要更多想法的人,请参考:https://stackoverflow.com/a/76320041/143475