JMeter 断言:比较变量时出错
JMeter Assertion : Error when comparing variables
我正在尝试使用从我的数据库中获得的值来断言我的 API 响应数据。
我的代码无法比较我的变量,除非我将 toString()
添加到我的两个变量。有什么解决办法还是 toString()
是强制性的?
有问题的代码是:
Boolean comparision = false;
for (int i; i < vars.getObject("dbData").size(); i++) {
if (vars.getObject("dbData").get(i).get("DbData").toString().equals(${codeid_API}.toString()))
{
comparision = true;
}
}
${codeid_API}
是我存储 API 响应数据的变量。
(vars.getObject("dbData").get(i).get("DbData")
从我的数据库中获取值。
您可以使用 Objects.equals 代替
Objects.equals(vars.getObject("dbData").get(i).get("DbData"), ${codeid_API});
Returns true if the arguments are equal to each other and false otherwise.
对于整数,您可以使用 ==
进行比较并使用 as int
进行转换
if (vars.getObject("dbData").get(i).get("DbData") as int == vars.get("codeid_API") as int );
不要将 ${codeid_API}
形式的 JMeter 变量内联到 Groovy 脚本中,如果启用编译脚本缓存,它将被解析 仅一次 它可能会破坏您的脚本逻辑。
考虑将其替换为 vars.get('codeid_API
) 而不是其中 vars
是 JMeterVariables class 实例
的 shorthand
引自 JSR223 Sampler 文档:
JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.
props.get("START.HMS");
props.put("PROP1","1234");
我正在尝试使用从我的数据库中获得的值来断言我的 API 响应数据。
我的代码无法比较我的变量,除非我将 toString()
添加到我的两个变量。有什么解决办法还是 toString()
是强制性的?
有问题的代码是:
Boolean comparision = false;
for (int i; i < vars.getObject("dbData").size(); i++) {
if (vars.getObject("dbData").get(i).get("DbData").toString().equals(${codeid_API}.toString()))
{
comparision = true;
}
}
${codeid_API}
是我存储 API 响应数据的变量。
(vars.getObject("dbData").get(i).get("DbData")
从我的数据库中获取值。
您可以使用 Objects.equals 代替
Objects.equals(vars.getObject("dbData").get(i).get("DbData"), ${codeid_API});
Returns true if the arguments are equal to each other and false otherwise.
对于整数,您可以使用 ==
进行比较并使用 as int
进行转换
if (vars.getObject("dbData").get(i).get("DbData") as int == vars.get("codeid_API") as int );
不要将 ${codeid_API}
形式的 JMeter 变量内联到 Groovy 脚本中,如果启用编译脚本缓存,它将被解析 仅一次 它可能会破坏您的脚本逻辑。
考虑将其替换为 vars.get('codeid_API
) 而不是其中 vars
是 JMeterVariables class 实例
引自 JSR223 Sampler 文档:
JMeter processes function and variable references before passing the script field to the interpreter, so the references will only be resolved once. Variable and function references in script files will be passed verbatim to the interpreter, which is likely to cause a syntax error. In order to use runtime variables, please use the appropriate props methods, e.g.
props.get("START.HMS"); props.put("PROP1","1234");