Karate测试框架中如何实现两个变量存储值的大小比较
Karate框架变量大小比较实现方案
现有写法问题
你当前的代码写法是错误的,#()变量插值不能直接嵌套在#?谓词的字符串表达式中,会被识别为普通字符串而无法正确取到currentdayaftereexecution的变量值。
正确实现逻辑
1. 单值比较
如果todayusage是单个数值,直接用Karate原生支持的比较运算符即可:
print currentdayaftereexecution print todayusage # 大于比较 match todayusage > currentdayaftereexecution # 小于比较 match todayusage < currentdayaftereexecution
2. 数组每个元素比较(对应你用到的match each场景)
如果todayusage是数组,需要每个元素都满足大小条件,用#?谓词语法,其中_指代当前遍历的数组元素,直接引用变量名即可,不需要额外加插值符号:
print currentdayaftereexecution print todayusage # 每个元素都大于currentdayaftereexecution match each todayusage == '#? _ > currentdayaftereexecution' # 每个元素都小于currentdayaftereexecution match each todayusage == '#? _ < currentdayaftereexecution'
注意事项
如果变量存储的是字符串格式的数字,比较前需要先转成数值类型,避免出现字符串排序导致的异常结果:
# 转浮点数比较示例 match each todayusage == '#? parseFloat(_) > parseFloat(currentdayaftereexecution)'
内容的提问来源于stack exchange,提问作者Varun V Nair
相关产品推荐
相关产品推荐

