Karate中continueOnStepFailure不生效,无法全量校验JSON属性求助
Karate实现JSON全属性校验的配置方案
问题描述
使用Karate Core 1.0及以上版本,已在match步骤前配置continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] },但仍无法实现JSON每个属性的校验——第一个属性校验失败后,后续属性不再进行对比。询问除版本外是否需要额外配置。
测试代码
* def response1 = """ { "name": "Tom", "gender": "male", "age": "20" } """ * def response2 = """ { "name": "Jon", "gender": "female", "age": "21" } """ * configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] } * match response1 == response2 * configure continueOnStepFailure = false
当前执行输出
match failed: EQUALS $ | not equal | match failed for name: 'name' (MAP:MAP) {"name":"Tom","gender":"male","age":"20"} {"name":"Jon","gender":"female","age":"21"} $.name | not equal (STRING:STRING) 'Tom' 'Jon' classpath:LIB_API/SampleTest.feature:51
解决方案
你混淆了两个配置的作用:
continueOnStepFailure用于控制步骤之间的执行逻辑,即某个步骤(如单个match命令)失败后,是否允许后续步骤继续执行,它不影响单个match命令内部的校验流程。- 要实现单个
match命令内全属性校验、汇总所有错误,需要配置match的failFast参数为false,这才是控制单个match是否在第一个错误出现时中断校验的开关。
修改后的代码如下:
* def response1 = """ { "name": "Tom", "gender": "male", "age": "20" } """ * def response2 = """ { "name": "Jon", "gender": "female", "age": "21" } """ * configure match = { failFast: false } // 关闭match的快速失败,校验所有属性 * match response1 == response2 * configure match = { failFast: true } // 恢复默认配置
执行上述代码后,会输出name、gender、age三个属性的所有不匹配错误,而非仅停留在第一个错误。
内容的提问来源于stack exchange,提问作者Pmtester38
相关产品推荐
相关产品推荐

