如何使用 yml 文件验证空手道结构响应
How to validate with yml file a karate structure response
最近我第一次开始使用 Karate 和 Yaml。我能够验证所有答案数据都在同一级别的简单响应结构。但是现在我要验证一个更复杂的结构,我花了很多时间都没有成功。
当我执行 GET 请求时,我收到下一个答案:
[
{
"factories": [
{
"id": 1,
"scopes": [
{
"id": null,
"name": "name 1",
"expireD": 10,
"isExpired": true
}
],
"companyName": "TEST",
},
{
"id": 2,
"scopes": [
{
"id": null,
"name": "name 2",
"expireD": 7,
"isExpired": true
}
],
"companyName": "TEST2",
}
],
"scopeId": null
}
]
空手道代码中没有直接进行结构验证。它在这样的 yml 文件中:
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories:
companyName: '#string'
id: '#number'
scopes:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
我检查了大约 100 次结构,当我到达这里时,我一直有同样的错误:
* match response contains responseMatches
错误是下一个:
$[1].工厂 |数据类型不匹配 (LIST:MAP)
我尝试使用 match each,一个一个地忽略结构以查看哪个结构失败,并将验证减少为 #array 并且它不起作用。
我们非常欢迎任何帮助。谢谢。
我真的建议不要使用 YAML,尤其是在测试/验证场景中。但终于轮到你了。
这里有一个提示可以节省您一些时间,您可以打印出 YAML 的值并查看哪里出错了。我不知道 YAML(并尽可能避免使用它),但我在几次尝试失败后进行了猜测,并设法在正确的位置插入了一个 -
(显然还有很多其他方法)来制作一些 YAML 一个 JSON 数组——这就是你想要的。
* text foo =
"""
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories:
-
companyName: '#string'
id: '#number'
scopes:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
"""
* yaml foo = foo
* print foo
尝试上面的方法,看看它与您的示例有何不同。
终于找到了解决办法。 The Karate documentation 提供了有关如何使用它来定义可用作类型的数据结构的想法。我之前尝试过,但我在 responseMatches 部分之前添加了。 yml 文件如下所示:
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories: '#[_ <= 5] factoryStructure'
factoryStructure:
companyName: '#string'
id: '#number'
scopes: '#[] scopeStructure'
scopesStructure:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
最近我第一次开始使用 Karate 和 Yaml。我能够验证所有答案数据都在同一级别的简单响应结构。但是现在我要验证一个更复杂的结构,我花了很多时间都没有成功。
当我执行 GET 请求时,我收到下一个答案:
[
{
"factories": [
{
"id": 1,
"scopes": [
{
"id": null,
"name": "name 1",
"expireD": 10,
"isExpired": true
}
],
"companyName": "TEST",
},
{
"id": 2,
"scopes": [
{
"id": null,
"name": "name 2",
"expireD": 7,
"isExpired": true
}
],
"companyName": "TEST2",
}
],
"scopeId": null
}
]
空手道代码中没有直接进行结构验证。它在这样的 yml 文件中:
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories:
companyName: '#string'
id: '#number'
scopes:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
我检查了大约 100 次结构,当我到达这里时,我一直有同样的错误:
* match response contains responseMatches
错误是下一个: $[1].工厂 |数据类型不匹配 (LIST:MAP)
我尝试使用 match each,一个一个地忽略结构以查看哪个结构失败,并将验证减少为 #array 并且它不起作用。
我们非常欢迎任何帮助。谢谢。
我真的建议不要使用 YAML,尤其是在测试/验证场景中。但终于轮到你了。
这里有一个提示可以节省您一些时间,您可以打印出 YAML 的值并查看哪里出错了。我不知道 YAML(并尽可能避免使用它),但我在几次尝试失败后进行了猜测,并设法在正确的位置插入了一个 -
(显然还有很多其他方法)来制作一些 YAML 一个 JSON 数组——这就是你想要的。
* text foo =
"""
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories:
-
companyName: '#string'
id: '#number'
scopes:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'
"""
* yaml foo = foo
* print foo
尝试上面的方法,看看它与您的示例有何不同。
终于找到了解决办法。 The Karate documentation 提供了有关如何使用它来定义可用作类型的数据结构的想法。我之前尝试过,但我在 responseMatches 部分之前添加了。 yml 文件如下所示:
operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
scopeId: '##number'
factories: '#[_ <= 5] factoryStructure'
factoryStructure:
companyName: '#string'
id: '#number'
scopes: '#[] scopeStructure'
scopesStructure:
expireD: '#number'
name: '#string'
id: '#null'
isExpired: '#boolean'