如何使用空手道 continueOnStepFailure 断言每个 Json 字段值
How to use Karate continueOnStepFailure to assert each Json field values
目标是对每个 JSON 字段进行软断言。
* def KAFKA_TOPIC = "topic1"
* def kafkaExpected = {field1:"value1",field2:"value2",field3:"value3"}
* def kafkaActual = {"topic1":[{field1:"value1",field2:"x",field3:"y"}]}
* configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }
* match (kafkaActual[KAFKA_TOPIC]) == ['#(kafkaExpected)'] <-- do we have one-liner like this to do soft assertions on all fields?
* configure continueOnStepFailure = false
Output:
$[0].field2 | not equal (STRING:STRING)
'x'
'value2'
而不是一个接一个地做。
* match (kafkaActual[KAFKA_TOPIC])[0].field1 == kafkaExpected.field1
* match (kafkaActual[KAFKA_TOPIC])[0].field2 == kafkaExpected.field2
* match (kafkaActual[KAFKA_TOPIC])[0].field3 == kafkaExpected.field3
Output:
match failed: EQUALS
$ | not equal (STRING:STRING)
'x'
'value2'
match failed: EQUALS
$ | not equal (STRING:STRING)
'y'
'value3'
奇怪的是,在终端日志上它只打印了一个断言,无论是在两种方法上。
$ | not equal (STRING:STRING)
'y'
'value3'
尝试使用 Karate.forEach 但似乎不是正确的路径。
从 Peter 提供的 中找到了解决方案。我只需要将 JSON 转换为键值列表格式并将其用作数据源。
From:
{field1:"value1",field2:"value2",field3:"value3"}
Transformed To:
[{key:"field1",value:"value1"},{key:"field2",value:"value2"},{key:"field3",value:"value3"}]
使用的函数和用法:
* def input = INPUT
* def func =
"""
function(obj){
var output = [];
for (var i in obj) {
output.push({key: i, value: obj[i]});
}
return output
}
"""
* json kafkaAttributes = func(input)
* configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }
* karate.call('kafka.feature@validateFieldsAndValues',kafkaAttributes)
* configure continueOnStepFailure = false
@validateFieldsAndValues
Scenario:
* match (response[KAFKA_TOPIC][0][key]) contains value
目标是对每个 JSON 字段进行软断言。
* def KAFKA_TOPIC = "topic1"
* def kafkaExpected = {field1:"value1",field2:"value2",field3:"value3"}
* def kafkaActual = {"topic1":[{field1:"value1",field2:"x",field3:"y"}]}
* configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }
* match (kafkaActual[KAFKA_TOPIC]) == ['#(kafkaExpected)'] <-- do we have one-liner like this to do soft assertions on all fields?
* configure continueOnStepFailure = false
Output:
$[0].field2 | not equal (STRING:STRING)
'x'
'value2'
而不是一个接一个地做。
* match (kafkaActual[KAFKA_TOPIC])[0].field1 == kafkaExpected.field1
* match (kafkaActual[KAFKA_TOPIC])[0].field2 == kafkaExpected.field2
* match (kafkaActual[KAFKA_TOPIC])[0].field3 == kafkaExpected.field3
Output:
match failed: EQUALS
$ | not equal (STRING:STRING)
'x'
'value2'
match failed: EQUALS
$ | not equal (STRING:STRING)
'y'
'value3'
奇怪的是,在终端日志上它只打印了一个断言,无论是在两种方法上。
$ | not equal (STRING:STRING)
'y'
'value3'
尝试使用 Karate.forEach 但似乎不是正确的路径。
从 Peter 提供的
From:
{field1:"value1",field2:"value2",field3:"value3"}
Transformed To:
[{key:"field1",value:"value1"},{key:"field2",value:"value2"},{key:"field3",value:"value3"}]
使用的函数和用法:
* def input = INPUT
* def func =
"""
function(obj){
var output = [];
for (var i in obj) {
output.push({key: i, value: obj[i]});
}
return output
}
"""
* json kafkaAttributes = func(input)
* configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }
* karate.call('kafka.feature@validateFieldsAndValues',kafkaAttributes)
* configure continueOnStepFailure = false
@validateFieldsAndValues
Scenario:
* match (response[KAFKA_TOPIC][0][key]) contains value