Scripting JMeter Assertions in Groovy - A Tutorial
Scripting JMeter Assertions in Groovy - A Tutorial
Learn to use JMeter™ assertions, which enable you to set the criteria to determine if a response will be considered a pass or fail in your load tests.
Join the DZone community and get the full member experience.Join For Free
Sensu is an open source monitoring event pipeline. Try it today.
Apache JMeter™ assertions are components that enable developers to set criteria determining whether a response will be considered a "pass" or a "fail." You can use assertions to ensure returned values from samples and subsamples match expected results, and you can also apply them to JMeter Variables. They will be executed after every sampler in the same scope.
Assertions can be applied to either the main sample, the sub-samples, or both. For example, if you request the page www.google.com and want to determine if the server response was successful, you can use the Response Assertions to verify if the server response body includes the string 'I'm Feeling Lucky'.
JMeter includes quite a few assertion elements for validating the sampler's response.
However, sometimes your validation decision might follow complex and advanced logic, and can't be configured using the out-of-the-box JMeter assertions. For example, confirming the validity of a JSON response, followed by evaluation of the values of the response, and having a customized failure message that will help debug the issue.
The Apache Groovy scripting language is great for writing concise and maintainable tests, and for all your build and automation tasks. Groovy has a seamless and transparent integration with Java and any third-party libraries, making it easy to use for Java developers who use JMeter. Features such as Power Assertion make testing and asserting easy and quick in Groovy, compared to any other scripting language available, such as BeanShell.
In the following example, we want to test that the server response includes a well structured JSON. We will run a request through our application server, and we expect to receive a structured JSON that includes at least the 3 of the following keys: api_version, error, and result.
Additionally, there is a key named "numberOfExpectedServers" inside "result," and we expect it to be equal to 3.
Since we know which keys we are expecting to receive in the response, and the value that should be assigned to them, we can use a Groovy assertion to assert the response.
In the beginning of the assertion, we will first want to verify that the response was successful, so we will check that the response code is equal to 200. Then, we will show a step-by-step of how to code this example.
1. Start with a simple script that includes a Thread Group and a Sampler. I named the Sampler "Choose flight."
2. Add the assertion element that provides us with the ability to use Groovy as our scripting language: JSR223
Right click on our Sampler -> Add -> Assertions -> JSR223 Assertion
3. In the element configuration part, set the following property: Script Language: Groovy 2.x.x.
JSR223 Assertion Fields Explained:
- Name: Element name
- Language: The scripting language to be used (Groovy, BeanShell, JS, etc.)
- Parameters: The parameters to pass to the script. The parameters are stored in the following variables: Parameters, args
- File Name: A path to a pre-made script file. Will override any script that is written in the main script field
Script compilation caching is the major advantage of using JSR223 elements, as opposed to Beanshell elements.
4. You can now use a premade script (from the Script file section), or write a script from scratch.
Let's look at this script, written from scratch. (To learn more about how to script in Groovy, click here).
The script does the following:
- Checks if the response code is equal to 200.
- Checks if the first level of the JSON contains the following keys: api_version, error, result.
- Checks if the key result numberOfExpectedServers is equal to 3.
If any of the conditions fail, the failure message will be added as a string. Therefore, if the failure message is empty, the assertion was successful. If it's not empty, the assertion is set to fail and to display a failure message.
The script starts with importing the JSON Slurper. The JSON Slurper parses JSON text or reader content into Groovy data structures. Read more here.
5. Run the script and display the results in a listener.
An unsuccessful response:
In this response, the number of servers is 2, and not 3.
By using Blazemeter, a load testing tool that enhances JMeter abilities, we designed our "Errors" report to fit this sort of usage. If the assertion fails, the "Errors" report will help you understand the root-cause of the failure. This makes the debugging process easier.
As you can see, it's clear that the test had a failed element, and any details that were passed to the assertion failure Message would be listed in this table.
Want to learn more? You might be interested in viewing my webinar, Advanced JMeter Scripting - Writing Assertions in Groovy.
To learn more about JMeter check out this free 5-day online course.
Published at DZone with permission of Ariel Bell , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.