JMeter Assertions
JMeter Assertions
Assertions are used to validate server responses and ensure test accuracy. Assertion helps to confirm that your server under test returns the expected result. Assertions allow you to define the pass/fail criteria for your test.
Some commonly used assertions are:
- Response Assertion: Checks if the server response contains expected data.
- Duration Assertion: Ensures a response is received within a specified time.
- Size Assertion: Validates the size of the response data.
You can configure what kind of assertions is active for every test plan. So test assertion is – a condition which compares the expected value with the actual value for a specified test case. One of the most widely used assertion is Response Assertion
which allows us to verify the content of the response /status code. Similarly there are several types of assertions that we can use in our tests.
Some of the available assertions in JMeter are as follows:
- Response Assertion
- Bean shell Assertion
- Compare Assertion
- Duration Assertion
- Size Assertion
- XML Assertion
- HTML Assertion
- XPath Assertion
- XML Schema Assertion
To add an assertion
Follow the below steps to add an assertion, for example to add Response Assertion:
Right click on the HTTP Request > Add > Assertions > Response Assertion
The Response Field To Test is set to ‘Text Response’. Here when we select ‘Text Response’ which checks for the response text from the server, i.e. the body, excluding any HTTP headers.
and “Pattern Matching Rules” is set to Sub-string. This returns true if the text contains the pattern string (case-sensitive).
To view the assertion results, we need to add an Assertion Listener to the Thread Group
Thread Group > Add > Listener > Assertion Results
Save and Run the Test
If the response is failed, it will display a message something like in Assertion Results:
HTTP Request
Response Assertion : Test failed: text expected to contain <Expected Value>