Assertions in JMeter

Assertions in JMeter

Introduction

Assertions help us verify whether the response of a sampler is correct or not, instead of manual verification. An assertion is a test element that allows us to set criteria for verifying the sampler request. If the sampler's request matches the expected criteria, the sampler will be considered a pass or else it will fail during the test run.

Below are some of the most commonly used assertions in JMeter:

Response Assertion

  • In a response assertion, we check if the sampler's response is correct or not by setting specific criteria. We can validate the text response, response code, response message, etc. If the response matches the criteria set in the assertion, the test will pass otherwise it will fail.

    Example: Suppose after logging into a platform, you receive a message like "Hi Anderson, Good Morning," and you want to validate it. In this case, you can add an assertion.

    In Response Assertion, we have multiple attributes:

    • Apply to: Here, we select the level at which we want to apply the assertion.

    • Field to test: This is where we specify the type of assertion we want to perform, such as a text response assertion or a response code assertion.

    • Pattern Matching Rules: Here, we choose how we want to validate the response. For an exact match, we select "Equals"; for matching, we choose "Matches," and there are other options available as well.

      How to add Response assertion:

      • Right-click on the sampler.

      • Hover over -> Add Assertion -> Response Assertion.

      • Click on Response Assertion.

Size Assertion

  • In this assertion, we validate the size of the response generated by a request. It involves checking how many bytes are consumed by the server to generate the response. With this assertion, we can ensure that the response is fully generated.

    Suppose, we have a requirement that every request's size should be generated within 5000 bytes, we can use a size assertion to validate this.

    How to add Size Assertion:

    • Right-click on the sampler.

    • Hover over -> Add Assertion -> Size Assertion.

    • Click on Size Assertion.

Duration Assertion

  • In a Duration Assertion, we check how much time a request takes to generate a response. The value specified in milliseconds sets the limit for how long a particular request should take to generate a response, otherwise it will fail.

    For example, if there is a requirement that every request should receive a response within 5 seconds, we can add this requirement in the Duration Assertion and validate the response time.

    How to add Size Assertion:

    • Right-click on the sampler.

    • Hover over -> Add Assertion -> Duration Assertion.

    • Click on Duration Assertion.

BeanShell Assertion

  • In JMeter, the BeanShell Assertion is used to add custom assertion logic by writing a script with the help of one of the supporting languages provided by JMeter. The BeanShell assertion is also known as a script-based assertion.

    Main input fields-

    • Script Files – Here, we can directly add a file in which the BeanShell script is already written, and the assertion will be based on that script.

    • Script – In this field, we write our own script and logic for the assertion.

  • How to add Size Assertion:

    • Right-click on the sampler.

    • Hover over -> Add Assertion -> BeanShell Assertion.

    • Click on BeanShell Assertion.

Conclusion

Assertions play a crucial role in ensuring the accuracy and reliability of sampler responses in JMeter. By setting specific criteria, we can determine whether a sampler request meets the expected standards. Response Assertion checks for correctness, Size Assertion validates response size, and Duration Assertion monitors response time. For customized logic, BeanShell Assertion offers script-based flexibility.

Knowing how to apply these assertions empowers testers to conduct thorough and efficient performance testing in JMeter.

Did you find this article valuable?

Support Perf Insights by becoming a sponsor. Any amount is appreciated!