Ensure system under test is in correct state

We are performing load testing against some of our REST APIs and have had some problems as we must ensure we have turned off some specific functions in the API before we start the load testing.

Because of this we have added an endpoint which returns the state of these functions.
We hoped we could add an evaluation in our load tests before the actual load testing began when we executed the in the cloud. We have tried using a threshold, but it does not end the tests directly.

Do you have any suggestions if it is possible to perform some sorts of initial check towards the system in the actual tests instead of having to rely on manually check the state of the system under tests?

Hmm you can call your API in the setup() function and throw an exception if the state of your system doesn’t match:

import http from 'k6/http';


export function setup() {
    if (Math.random() > 0.5) { // substitute your actual check here
        throw new Error("something was wrong");
    }
}

export default function () {
    // load test code
}

It’s going to get your script “aborted by script error” in the cloud, but it should do the job.

Just forgot to mention something regarding

We have tried using a threshold, but it does not end the tests directly.

If you use abortOnFail, the threshold should abort your test immediately after it has been crossed (or after delayAbortEval, if you’ve specified that as well). More info at Thresholds