比较测试不同阶段的2个变量

问题描述 投票:0回答:1

我在JMeter中有这个方案:

获取var mainBalance中的余额值(GET) 充电(POST) 验证结果余额(GET) - 放入var updatedBalance,例如updatedBalance是mainBalance + 10 $ 所有值必须是float类型。

Im stuck at the last step: I've put a BeanShell assertion but doesn't work. It think that i get the values in the wrong way and also i dont按照JMeter的要求进行计算。我也尝试过vars.get(String.valueOf(“mainBalance”))

float a = new float(vars.get("mainBalance"));
float b = new float(vars.get("updatedBalance"));
if(b != (a + 10)) {
    Failure = true;
}

这是日志错误:

断言错误:true断言失败:false断言失败消息:org.apache.jorphan.util.JMeterException:调用bsh方法时出错:eval在文件中:内联求值:``float a = new float(“mainBalance”); float b = new float(“updatedBalance”); if(b ...''遇到'(“第1行,第20栏。

web-services jmeter automated-tests assertions beanshell
1个回答
1
投票
  1. 不要使用Float数据类型来代表货币操作,至少要使用BigDecimal
  2. 不要使用Beanshell,因为它可能会成为性能瓶颈,而是选择JSR223 Assertion

相关代码如下:

def a = new BigDecimal(vars.get('mainBalance'))
def b = new BigDecimal(vars.get('updatedBalance'))
if (b.compareTo(a.add(new BigDecimal('10'))) != 0) {
    AssertionResult.setFailure(true)
}

更多信息:Scripting JMeter Assertions in Groovy - A Tutorial

© www.soinside.com 2019 - 2024. All rights reserved.