BreadcrumbHomeResourcesBlog Advanced API Assertions With BlazeMeter API Testing and Monitoring September 23, 2021 Advanced API Assertions with BlazeMeter API Testing and MonitoringAPI TestingBy Sam AybarUsing 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.Table of ContentsWhat are API assertions? Basic API Assertions with BlazeMeter API TestingComplex API Assertions using Environment VariablesTable of Contents1 - What are API assertions? 2 - Basic API Assertions with BlazeMeter API Testing3 - Complex API Assertions using Environment VariablesBack to topWhat 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 topBasic 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. Back to topComplex 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. 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? API Assertions with Post Response ScriptsIn 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 codeassert.equal(response.status, 200, "status was 200 OK"); Or something slightly more complex like: // parse JSON response body into objectvar data = JSON.parse(response.body); // check that a value is a certain lengthassert.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 libraryassert(_.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 namevar envName = variables.get("envName"); //conditional assertionif (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 namevar envName = variables.get("envName") ; //retrieve test run locationvar requestLocation = variables.get("location"); //vary response time assertion in production bby locationvar 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} (You can see the location codes in the BlazeMeter documentation here).Assertions Using SnippetsFinally, 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.START TESTING NOWBack to top
Sam Aybar Principal Product Manager, CA Technologies 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.