Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Assertions and Listeners

Assertions in JMeter: Response, JSON, Duration and Size

Verify responses in JMeter with assertions: Response Assertion, JSON Assertion, Duration, Size, XPath, HTML and JSR223 checks, plus scope, patterns, failure messages and best practices.

A fast error page is still an error. By default JMeter calls a sample successful when the HTTP status is below 400, so a login page that returns 200 OK with the text "Invalid password" counts as a pass. Assertions close that gap: they inspect each response and mark the sample failed when it does not meet your expectation. Without them, your error rate is not an error rate.

Honesty note. JMeter cannot run here. Assertion names, fields and menu paths are real. The runnable blocks are stand-ins: JavaScript functions that implement the same checks (contains, equals, regex, JSON path, duration, size) on sample responses so you can see pass/fail logic.

Add assertions via Add > Assertions under a sampler (check one request) or under a controller or Thread Group (check all samplers below; see scoping). They run after post-processors for each sampler.

Response Assertion

The all-round check. Fields:

Field Options
Apply to Main sample and sub-samples, Main sample only, Sub-samples only, or JMeter Variable name to use
Field to Test Text Response, Document (text), Response Code, Response Message, Response Headers, Request Headers, Request Data, URL Sampled
Pattern Matching Rules Contains, Matches, Equals, Substring, optionally with Not and Or
Patterns to Test One or more patterns (Java regular expressions for Contains and Matches)
Custom failure message Text shown in the results when the assertion fails

Mind the differences:

  • Contains treats patterns as regular expressions found anywhere in the field.
  • Matches requires the regular expression to match the whole field.
  • Equals and Substring are literal comparisons, with no regex.

Example checks:

  1. Response Code Equals 201 for a create call.
  2. Text Response Contains Welcome, ${username} for a login (variables work in patterns).
  3. Text Response Contains with Not ticked and pattern (Exception|Internal Server Error) to catch error text hidden inside a 200 page.

Use several patterns with Or when different valid answers are possible. When a regex special character such as . or ( should be literal, escape it (\.) or use Substring.

JSON Assertion

Checks a value inside a JSON response: set Assert JSON Path exists to $.status; tick Additionally assert value and enter the expected value ok (or a regex with Match as regular expression). Tick Expect null or Invert assertion for negative checks. It is more precise than searching the raw text, since it cannot be fooled by the same word appearing elsewhere.

Duration Assertion

Fails a sample slower than a limit: Duration in milliseconds 1500. Use it to turn service-level goals into pass/fail per request. Be aware that under heavy load many samples may fail it, which is the point, but it also inflates the error rate, so decide whether slowness counts as an "error" in your reports or whether you prefer to judge latency with percentiles in the dashboard.

Size Assertion

Checks the response body size in bytes: Size to Assert with comparison operators (=, !=, >, <, >=, <=). Good against truncated or unexpectedly empty responses; combine with Field to test (full response, headers, body, message and code).

XML, XPath2, HTML and other assertions

Assertion Use
XML Assertion Response is well-formed XML
XPath2 Assertion An XPath expression evaluates true or finds nodes (SOAP and XML APIs)
HTML Assertion Response HTML is valid (uses a checker; slow, for spot-checks only)
XML Schema Assertion Validates against an XSD file
MD5Hex Assertion The response body's MD5 matches an expected hash (static files)
Compare Assertion Compares samples with each other (resource heavy)
JSR223 Assertion Your own Groovy logic (JSR223)
Assertion Duration, JSON JMESPath Assertion Newer or plugin variants for specific needs

Pick the cheapest assertion that proves correctness. HTML and Compare assertions burn CPU, which matters when each thread runs hundreds of loops.

What happens when an assertion fails?

The sample is marked failed (a red cross in the Results Tree), the failure message is recorded, and the error counts in listeners and the report. The thread still continues to the next sampler, unless the Thread Group's Action to be taken after a Sampler error says otherwise. Two or more assertions on one sampler are all evaluated; the sample fails if any fails and shows each message.

If you want a request that should fail (a negative test, such as 404 on a deleted record), assert the 404, which makes the sample pass when the status is 404. Alternatively, tick Ignore Status on the Response Assertion so the HTTP status does not matter and only your pattern decides.

Stand-in: assertion logic

JS Stand-in: Response, JSON, Duration and Size checks

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Change elapsedMs to 900 and rerun to see the sample succeed.

Matches versus Contains

JS Stand-in: Contains, Matches, Equals and Substring

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second and last lines are the classic surprises: Matches needs the pattern to cover the whole text, and Substring does not interpret regular expressions.

Best practices

  • Assert on every sampler, at least on the status and one piece of expected content.
  • Keep assertions close to samplers; use shared Thread Group assertions only for truly global checks (such as "no stack trace in the body").
  • Prefer specific text (an order number shown, a JSON field) over generic words that appear on error pages too ("Home", "Error" in a menu).
  • Write a custom failure message that tells a stranger what went wrong.
  • Remember assertions are not free: XPath and HTML checks on large bodies cost CPU.
  • Test the assertion itself: run once with wrong credentials and confirm the sample fails.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A page returns 200 OK but its body says 'Invalid password'. How does JMeter treat the sample by default?

    Without assertions JMeter judges by the HTTP status only, so you need a body assertion.
  2. 2. Which Response Assertion rule requires the regular expression to match the whole field?

    Matches requires a full match; Contains finds the regex anywhere; Substring and Equals are literal.
  3. 3. Which assertion fails a sample that is too slow?

    Duration Assertion fails samples longer than the specified milliseconds.
  4. 4. For a negative test where a 404 is the expected answer, what should you do?

    Asserting the expected 404 makes the sample pass when the application behaves correctly.

Summary

  • Without assertions, only status below 400 counts as success; add real checks.
  • Response Assertion (Contains, Matches, Equals, Substring) is the all-rounder; JSON Assertion is precise for APIs.
  • Duration and Size assertions turn goals and sanity checks into pass/fail.
  • Scope follows the tree; a failed assertion marks the sample failed but the thread continues by default.
  • Choose cheap assertions, meaningful messages, and verify them with a deliberately failing run.

Next: Listeners and Results