How to fail a Gatling test from within "exec"?

hauron picture hauron · Sep 2, 2014 · Viewed 8.3k times · Source

A Gatling scenario with an exec chain. After a request, returned data is saved. Later it's processed and depending on the processing result, it should either fail or pass the test.

This seems like the simplest possible scenario, yet I can't find any reliable info how to fail a test from within an exec block. assert breaks the scenario and seemingly Gatling (as in: the exception throw doesn't just fail the test).

Example:

// The scenario consists of a single test with two exec creating the execChain
val scn = scenario("MyAwesomeScenario").exec(reportableTest(

     // Send the request
     exec(http("127.0.0.1/Request").get(requestUrl).check(status.is(200)).check(bodyString.saveAs("MyData")

     // Process the data
    .exec(session => { 
         assert(processData(session.attributes("MyData")) == true, "Invalid data");
    })
))

Above the scenario somewhere along the line "guardian failed, shutting down system".

Now this seems a useful, often-used thing to do - I'm possibly missing something simple. How to do it?

Answer

Stephane Landelle picture Stephane Landelle · Sep 2, 2014

You have to abide by Gatling APIs.

  1. With checks, you don't "fail" the test, but the request. If you're looking for failing the whole test, you should have a look at the Assertions API and the Jenkins plugin.
  2. You can only perform a Check at the request site, not later. One of the very good reasons is that if you store the bodyString in the Sessions like you're doing, you'll end using a lot of memory and maybe crashing (still referenced, so not garbage collectable). You have to perform your processData in the check, typically in the transform optional step.