Karate 空手道:包含功能
我有下面的内容,我正在比较两个环境的反应。 在我的例子中,对于某些服务,响应json元素/数组顺序不是强制性的,但对于某些服务,它是强制性的。所以我使用了contains,但我怀疑在响应中是否缺少任何元素/数组 在比较以下两种情况时说:Karate 空手道:包含功能,karate,Karate,我有下面的内容,我正在比较两个环境的反应。 在我的例子中,对于某些服务,响应json元素/数组顺序不是强制性的,但对于某些服务,它是强制性的。所以我使用了contains,但我怀疑在响应中是否缺少任何元素/数组 在比较以下两种情况时说: A: { "hotels": [ { "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 }, { "roomInformation": [{ "ro
A: {
"hotels": [
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 },
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 }
]
}
B:
{
"hotels": [
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 },
{ "roomInformation": [{ "roomPrice": 680.79}], "totalPrice": 680.79 },
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 }
]
}
使用下面的匹配项,将每个匹配项与包含的内容进行匹配,是否给出肯定的结果?请建议
Feature:
Background:
Scenario: test
* json input = read('input.json')
* def stage= call read('A.feature') input;
* def prod = call read('B.feature') input;
#* def rp = $prod[*].response
#* def rs = $stage[*].response
#* match rs contains rp
#* match each $prod[*].response[*] contains $stage[*].response
# * match each $prod[*].response[*] contains $stage[*].response.[*]
你的问题有些复杂,很难回答
此外,断言可以是预期整体的部分列表。使用print
命令帮助您调试它
* def roomPrices = response.hotels[*].totalPrice
* print roomPrices
* match roomPrices contains [ 680.79, 1.00 ]
由于顺序很重要,并且在数组/列表中,“等于”检查比“包含”检查更有效:
以下代码导致错误
示例代码:
Feature: Validation
Scenario:
* def stage =
"""
{
"hotels": [
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 },
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 }
]
}
"""
* def prod =
"""
{
"hotels": [
{ "roomInformation": [{ "roomPrice": 679.79}], "totalPrice": 679.79 },
{ "roomInformation": [{ "roomPrice": 618.4 }], "totalPrice": 618.4 }
]
}
"""
* match prod == stage
#ERROR: path: $.hotels[0], actual: {roomInformation=[{"roomPrice":679.79}], totalPrice=679.79}, expected: {roomInformation=[{"roomPrice":618.4}], totalPrice=618.4}, reason: [path: $.hotels[0].roomInformation[0], actual: {roomPrice=679.79}, expected: {roomPrice=618.4}, reason: [path: $.hotels[0].roomInformation[0].roomPrice, actual: 679.79, expected: 618.4, reason: not equal (Double)]]
在我的例子中,我比较了stage和prod,并动态比较了整个响应,这两个区域都有相同的数据,所以两个结果应该是相同的。对于某些服务,JSON中响应元素的顺序会改变,因此当我进行精确的匹配(==)或甚至包含时,都会失败。我的假设是,如果顺序不同,contains
将产生积极的结果!就我个人而言,我从不试图比较整个回答。我通常会使用karate.filter
将响应内容缩小到我想要区分的区域。此外,我刚刚意识到我上面的示例代码可能无法正常工作。可能将assert
更改为match
,因为DSL包含的运算符可能在match
操作中。我对这些东西还是新手。(编辑我的答案)