Apache JMeter provides a variety of assertions which are helpful to apply a few checkpoints with each request. However the assertion must be carefully used because it consumes resources (memory or CPU). Below is a list of JMeter assertions:
- Bean shell assertion: allow the user to write their bean shell script to perform assertion.
- Compare assertion: used to compare sample results within its scope
- Duration assertion: check the response time of the request, if the request takes longer time than is specified in milliseconds then request will be marked as fail.
- HTML assertion: check the HTML syntax of the response data using JTidy
- JSR223 assertion: allow user to write JSR223 script to check the status of the request
- MD5Hex assertion: Use to check the MD5 hash of the response data
- BSF assertion: allow user to write BSF script to check various aspects of the SampleResult
- Response assertion: commonly used assertion which allows user to add pattern strings comparing against various fields of the response.
- Size assertion: allow the user to put a checkpoint on the size of the response. Provides comparison operators like =, <, >, != etc.
- SMIME assertion: use to evaluate the sample results from the Mail Reader Sampler
- XML assertion: checks that the response data consists of a formally correct XML document.
- XML Schema assertion: allow the user to validate a response against an XML Schema. The schema file is required.
- XPath assertion: Tests a document for well-formedness, has the option of validating against a DTD, or putting the document through JTidy and testing for an XPath
For more documentation, refer to http://jmeter.apache.org/usermanual/component_reference.html#assertions
Example assertion to confirm response has value using RegEx
We can apply a regex assertion in a response to confirm the response has certain text/values available. For example, we get a response from weather API in below format depending upon the city parameter in api call. We wanted to put an assertion to confirm that response always have fields (marked bold) with different values depending upon each request and input parameters.
{“coord”:{“lon”:-0.13,”lat”:51.51},
“weather”:[{“id”:522,”main”:”Rain”,”description”:”heavy intensity shower rain”,”icon”:”09n”}],
“base”:”stations”,
“main”:{“temp”:279.31,”prestassure”:982,”humidity”:93,”temp_min”:278.15,”temp_max”:281.45},
“visibility”:10000,
“wind”:{“speed”:2.1,”deg”:150},
“clouds”:{“all”:24},
“dt”:1452480859,
“sys”:{“type”:1,”id”:5093,”message”:0.0225,”country”:”GB”,”sunrise”:1452499363,”sunset”:1452528850},
“id”:2643743,
“name”:”London”,
“cod”:200}
Here is a copy of the JMeter test plan with example API calls and response assertions with regex.