Ask your Voice Assistant

Alexa, tell VoxSnap to play posts from BlazeMeter

How to Use JMeter Assertions in Three Easy Steps

JMeter Assertions are undeniably valuable, providing the criteria you set to determine whether the a will be considered a “pass.” You can use them to run against a sample and its subsamples to ensure returned values match expected results, and you can also apply them to JMeter Variables.

But are you using assertions efficiently? Do you know the pitfalls to avoid? This article takes you through three key steps for using assertions, including factors to consider when setting them, examples of commonly used assertions, and ways to view results.

1. Considerations Before Setting Assertions
The Cost of JMeter Assertions
All assertions come with a cost, in terms of CPU or memory consumption. However, some assertions carry a greater cost than others. According to the JMeter Performance and Tuning Tips guide, the Response Assertion and the Duration Assertion are typically lower-impact choices, whereas Compare Assertion and other XML-based ones like the XPath Assertion consume more CPU and memory.

The Scope of JMeter Assertions
You must also consider the scope when setting assertions. Assertions can be applied to a main sample and its subsamples, or only to subsamples. Some assertions, like the Response Assertion or the Size Assertion, can also be used against a JMeter Variable. Code-based assertions (such as Beanshell and JSR223) don’t have the GUI element that identifies scope. This means you must manually implement all assertion logic – including the scope.

An assertion can apply to samples on the same level (greater scope) or to parent samples (lesser scope).

Sonce assertions that fail cause the whole Transaction Controller to fail, use care when applying these assertions.

Combining Assertions
You can add more than one assertion to the sampler, controller, thread group, or test plan. Failed assertions will cause all affected samples to fail, so caution is essential.

2. Commonly Used Assertions & Their Uses
Response Assertions
The most commonly used assertion is the Response Assertion, which checks whether a response text, body, code, message or header contains, matches, or equals a specified pattern.

The pattern can be either be:
1. a “string” for “Equals” or “Substring” clauses
2. a “Perl5-style” Regular Expression for “Contains” or “Matches” clauses

Response Entities that can be checked include:

1. Text response - This is for the response that can be displayed in a browser
2. Document (text) - This is for anything supported by Apache Tika (it assumes the presence of apache-tika.jar in /lib folder of a JMeter installation). This can include PDF, Office, audio, and video formats. Be careful, because this can be memory-intensive for high loads.
3. URL Sampled - This assertion is used against a requested URL to ensure it matches expectations. For example, you may want to check that the redirect URL doesn’t contain an error somewhere in …

See the rest at blazemeter.com