eradicator
eradicator

Reputation: 13

jmeter send email assertion

I am new to Jmeter and am looking for a way to send emails out for every failed assertion.

Test Structure:

  1. Thread Group:
    • Transaction Controller:
      • Http Request:
      • Http Request:
      • Http Request:
  2. Thread Group:
    • Transaction Controller:
      • Http Request:
      • Http Request:
      • Http Request:

Each Http request contains a response assertion. I would like to capture and send one email that contains all of the failed assertions within a transaction controller. Is there a way to do this? I tried using adding a SMTP Sampler within a thread group with a child Bean PreProcessor that contains this code:

import org.apache.jmeter.assertions.AssertionResult;


try {
        AssertionResult[] results = prev.getAssertionResults();
    StringBuilder body = new StringBuilder();
    for (AssertionResult result : results) {
        body.append(result.getName());
        body.append(result.getFailureMessage());
        body.append(System.getProperty("line.separator"));
    }
    vars.put("body", body.toString());
}
catch (Throwable ex) {
    log.error("Error in Beanshell", ex);
    throw ex;
}

When i do this it will only send an email of the last failed assertion instead of all failed assertions.

Upvotes: 0

Views: 541

Answers (2)

Dmitri T
Dmitri T

Reputation: 167992

  1. Add a global JSR223 Listener (it is much better to use Groovy rather than Beanshell, see Groovy is the New Black article for details)
  2. Put the following code into "Script" area (it is basically your code but amended to read previous samplers results from the ${body} variable and append new results to them

    import org.apache.jmeter.assertions.AssertionResult;
    
    try {
        AssertionResult[] results = prev.getAssertionResults();
    
        StringBuilder body = new StringBuilder();
        String previousBody = vars.get("body");
        if (previousBody != null) {
            body.append(previousBody);
        }
        for (AssertionResult result : results) {
            body.append(result.getName());
            body.append(result.getFailureMessage());
            body.append(System.getProperty("line.separator"));
        }
        vars.put("body", body.toString());
    } catch (Throwable ex) {
        log.error("Error in Groovy", ex);
        throw ex;
    }
    

Upvotes: 0

theChrisFlame
theChrisFlame

Reputation: 173

We have a test that prints out request body information to the console on failure. This is added to each sampler individually and works quite nicely. You could update this code send a failure message instead of printing to the console.

//Loop through assertions and check for failed
for (int i = 0; i < results.length; i++){
     if (results[i].isFailure() || results[i].isError()){
         failed_assertion = true;
         System.out.println("\n********** Request Body ***************\n"+ctx.getCurrentSampler().getUrl().getPath()+ctx.getCurrentSampler().getQueryString()+'\n'+ctx.getCurrentSampler().getArguments().getArgument(0).getValue()+"\n");
    System.out.println("************ ERROR DETECTED ***********\n"+prev.getResponseDataAsString()+"\n****************************************\n");
    break;
   }
}

If that doesn't work you could construct an IF block on each sampler. Similar to what they're doing here.

Upvotes: 0

Related Questions