Sam Aybar joined Runscope in 2016, prior to its acquisition by CA Technologies. He was responsible for everything from pre-sales engineering through post-sales customer success as well as support. He is currently a member of the customer-facing engineering team.

Become a JMeter and Continuous Testing Pro

Start Learning
Slack

Test Your Website Performance NOW!

arrowPlease enter a URL with http(s)
Assert:is found in response
Sep 23 2021

Advanced Assertions with BlazeMeter API Testing and Monitoring

 

Using 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. It is based on 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.

Basic Assertions with BlazeMeter API Testing

 

It is very easy to make simple 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.

 

Complex Assertions using Environment Variables

 

One of the topics we touched on briefly in the End-to-End Testing webinar was how to make 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.

 

 

We can then reference the variable in the assertion.

 

 

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?

 

Assertions with Post Response Scripts

In addition to making assertions on the 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(data.cards, 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.

 

 

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");

}

 

 

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. 

 

 

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;

            break;

        case "eu2":

            maxResponse = 600;

            break;

        case "hk1":

            maxResponse = 800;

            break;

        default:

            log("other location");

    }

    assert(response.response_time_ms<maxResponse, "Production response time in " + requestLocation + "<" + maxResponse + "ms");

}

 

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

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.

 

 

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.

 

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. If you are already a BlazeMeter user give these tools a try. And if you aren’t yet using BlazeMeter, you can sign up for a free trial here.
 

   
arrowPlease enter a URL with http(s)

Interested in writing for our Blog?Send us a pitch!