我正在运行JSR223 Assertion,我使用groovy将预期响应与实际响应进行比较。如果它们不相等,则断言失败。 这是代码
import groovy.json.JsonSlurper;
JsonSlurper slurper = new JsonSlurper();
boolean set_assertion = true;
def expected_response = slurper.parseText("<JSON response goes here>");
def actual_response = slurper.parseText(prev.getResponseDataAsString());
if (expected_response != actual_response)
{
set_assertion = false;
log.error("expected_response != actual_response");
assert set_assertion == true;
SampleResult.setSuccessful(false);
}
现在,断言确实失败了。但我认为它失败了,因为我在日志中出现内部错误。 这是我的断言结果。 Assertion Results 这是日志
ERROR - jmeter.threads.JMeterThread: Error processing Assertion Assertion failed:
assert set_assertion == true
| |
false false
at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:402)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:650)
at Script48.run(Script48.groovy:16)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:352)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:153)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:206)
at org.apache.jmeter.assertions.JSR223Assertion.getResult(JSR223Assertion.java:47)
at org.apache.jmeter.threads.JMeterThread.processAssertion(JMeterThread.java:755)
at org.apache.jmeter.threads.JMeterThread.checkAssertions(JMeterThread.java:746)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:410)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:241)
at java.lang.Thread.run(Unknown Source)
理想情况下,我不应该有错误,我的断言应该失败并且应该有以下结果'断言错误:错误'和'断言失败:真'。
你能帮我理解我犯错的地方吗?我最担心的是日志中的错误。谢谢!答案 0 :(得分:3)
以下是发生的事情:
你正在使用Groovy“断言”。如下所示,比较是错误的:
expected_response!= actual_response
你最终失败了:
断言set_assertion == true;
抛出org.codehaus.groovy.runtime.powerassert.PowerAssertionError,它是一个子类或java.lang.Error,它被困在这里:
所以JMeter执行这个代码就是你得到的:
assertionResult = new AssertionResult("Assertion failed! See log file."); assertionResult.setError(true); assertionResult.setFailureMessage(e.toString());
这对我来说似乎很好,但为什么在这里使用Groovy断言?这样做会更有效率:
if ( expected_response != actual_response )
{
log.error("expected_response != actual_response");
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("expected_response != actual_response");
}
您可以了解有关阅读此blog的断言的更多信息。