0
votes

Following is the JSR223 Assertion groovy code where we are trying to log only when request or assertion fails or both fails.

import groovy.json.JsonSlurper

String jsonString = SampleResult.getResponseDataAsString();
String StartProcessResponseCode= SampleResult.getResponseCode();
def tokenJSON = new JsonSlurper().parseText(jsonString)

if (!SampleResult.isSuccessful()) {
 log.info("Failed Response Data---------"+SampleResult.getResponseDataAsString())
 }

String status= tokenJSON.status

assert StartProcessResponseCode.equals("200")
assert "SUCCESS".equals(status)

for (a in SampleResult.getAssertionResults()) {
  if (a.isError() || a.isFailure()) {
  //log.error(Thread.currentThread().getName()+": "+SampleLabel+": Assertion 
  failed for response: " + new String((byte[]) ResponseData));
  log.info("Failed Assertion Message---------------------"+AssertionResult.getFailureMessage());
  log.info("Failed Response Data---------------------"+SampleResult.getResponseDataAsString())
}

}

But in this case errors not logging for any Assertion failure

1

1 Answers

1
votes

First of all, if you want to capture all information on Assertions you should go for JSR223 Listener instead, as per JMeter Test Elements Execution Order listeners are executed after Assertions so it guarantees you that all the assertions will be applied prior your check.

With regards to the question itself, my expectation is that your assertion logic is failing on assert statements which throw unhandled AssertionError and your test simply doesn't proceed

See the following example:

log.info('Assertion Start')
assert 'foo'.equals('bar')
log.info('Assertion Ended')

according to what you've implemented you should see Assertion Ended message in the jmeter.log file, however it will not ever happen cause assert fails

enter image description here

And if you amend your code to catch the AssertionError like:

log.info('Assertion Start')
try {
    assert 'foo'.equals('bar') : "Failure here"
}
catch (AssertionError e) {
    log.info(e.getMessage())
}
log.info('Assertion Ended')

You will see both AssertionError message and Assertion Ended statement

JMeter Assertion Groovy

See Scripting JMeter Assertions in Groovy - A Tutorial for more information on using JSR223 Assertions in JMeter