Advanced API Assertions with BlazeMeter API Testing and Monitoring
September 23, 2021

Advanced API Assertions with BlazeMeter API Testing and Monitoring

API Testing

Using API assertions after requests in your test can help ensure your APIs are up, fast, and returning the data you expect in all of your environments. Such assertions can range from basic to more complex. In this blog post I will walk you through how to create complex assertions in BlazeMeter.  Details are found in a webinar we recently hosted: ”End-to-End Testing for Your APIs with BlazeMeter”. In the webinar, we covered how to use a single API test throughout the API lifecycle - from development through QA and into production.

Back to top

What are API assertions? 

API assertions allow you to validate HTTP responses in your API tests. Assertions are checked every time a test is run. Using API assertions after requests in your test can help ensure your APIs are up, fast, and returning the data you expect in all of your environments. Such assertions can range from basic to more complex.


Back to top

Basic API Assertions with BlazeMeter API Testing


It is very easy to make simple API assertions with BlazeMeter API Testing -- a few clicks on the assertion tab of a request step allows you to monitor status codes, response times, and data properties.


API assertions

Back to top

Complex API Assertions using Environment Variables


One of the topics we touched on briefly in the End-to-End Testing webinar was how to make API assertions that are more complex. For example, perhaps you want your assertions to differ between environments.  Maybe one of your header properties varies based on which environment you are testing.


In this case, we can use environment variables to drive our assertions. Instead of using a fixed Target Value, we define the target variable in the environment.


API assertions variables


We can then reference the variable in the assertion.


API assertions for variable data


By using a different value for the variable in each environment, we can have our assertions vary. This can be used for specific properties of the response or response times. But what if we only want to make assertions in some environments but not in others?


API Assertions with Post Response Scripts

In addition to making assertions on the API Assertions tab, BlazeMeter API Testing includes a  Script Engine, a powerful tool allowing you to execute custom JavaScript before or after request steps. You can include assertions in post-response scripts, using the assert module of the built in Chai Assertions Library


A typical post-response assertion might look something like:


// check for specific status code

assert.equal(response.status, 200, "status was 200 OK");


Or something slightly more complex like:


// parse JSON response body into object

var data = JSON.parse(response.body);


// check that a value is a certain length

assert.ok(data.deck_id.length == 12,"Deck ID is 12 characters");


// check that all items in a list contain a valid code for a card in a deck with regex and Underscore.js library

assert(_.every(, function(card) {return card.code.match(/(A|[2-9]|10)[CDHS]/);}), "All cards are valid");


However, if we want the script to vary based on the environment, we can use the built-in variable {{runscope_environment}} to find out the environment name. While this variable is directly accessible in the test (for example for a condition step), to access it in a script you must set an environment variable.


API assertion conditions


In the script, we can then retrieve the environment name and nest our assertion inside an if statement. For example if you only want to make an assertion about response times in your production environment, you could include the following script:


//retrieve environment name

var envName = variables.get("envName");


//conditional assertion

if (envName == "Production"){

    assert(response.response_time_ms < 500,"Production response time < 500ms");



API assertions - status responses


You can further customize this approach to address different response times for different locations. For example, perhaps you are monitoring your endpoints from multiple locations around the globe. 


API assertions for locations


You can configure your post-response script assertion to have different response times for each location. Just as we set an initial variable for our environment name, we’ll also set an initial variable for the location using the built in variable {{runscope_region}}. We can then write  a script to use separate response times based on the region in which a test runs.


//retrieve environment name

var envName = variables.get("envName") ;


//retrieve test run location

var requestLocation = variables.get("location");


//vary response time assertion in production bby location

var maxResponse = 500;

if (envName == "Production"){

    switch (requestLocation) {

        case "us2":

            maxResponse = 400;


        case "eu2":

            maxResponse = 600;


        case "hk1":

            maxResponse = 800;



            log("other location");





(You can see the location codes in the BlazeMeter documentation here).

Script responses

Assertions Using Snippets

Finally, if you are going to be using the same assertions for multiple steps in your test, rather than writing a post-response script for each step, you can use the Snippet functionality to write a script once and then easily attach it to any test step you wish. Snippets can be accessed from the Script Library in the top right of BlazeMeter API Testing.


API assertions snippets


From the script library page you simply give your snippet a name and add the code to make the assertions you want. Then, for each step of the test for which you want to use that script, you can select Add Snippet and choose the relevant snippet.


Using snippets

Individual steps can have more than one snippet, so you could have different snippets for different environments if you wanted or different snippets to cover different sets of assertions. The beauty of snippets is that if you want to change the logic you can do it once and it will apply to all instances of the snippet!

Hopefully this blog post has been helpful in understanding how you can make more complex assertions with BlazeMeter API Testing.


Back to top