CS/IT Turotials

How to Use Different Types of Assertions in JMeter

Google+ Pinterest LinkedIn Tumblr
In the previous article, you have learned about what the JMeter is and how to get started with JMeter that includes all about installing of JMeter and creating the first JMeter test. In this tutorial, you will learn about the different types of assertions in JMeter along with the examples of their uses for testing the software.

We have also mentioned about the assertion in some previous articles written on selenium webdriver and Selenium IDE. In JMeter, assertion confirms the validity of the test plan by comparing the response generated by a sample request. With the use of assertion testing, we can easily identify any inconsistent or unexpected responses from the target software application.

Read Also: Handling Assertions in Selenium WebDriver with TestNG

Following are the common types of assertions used on JMeter.

Response Assertion

Response assertion is the most commonly used assertion in JMeter. It allows you to add pattern strings that were compared against the various response from the server. It will match the various response patterns such as containing text, response codes, response headers etc.

Before adding assertions on JMeter test plan you have to add a sampler i.e. HTTP Request and some listeners such as “View Results Tree” and “View Results in Table” as given on the previous tutorial “Getting started with JMeter“. Use the following steps to setup response assertion after adding samplers and listeners in JMeter.

Response Assertions
  • At first, right click on Thread Group name, go to Add>Assertions and click on Response Assertion.
  • You can choose and apply the assertion to “Main sample only”, “Sub-samples only”, Main sample and sub-samples” etc. In this example, I have selected “Main sample only”.
  • Now, select one of the fields to test such as “Text Response”, “Response Code”, “Response Message” etc. Here we are comparing Response Code equal to 200.
  • There are different pattern matching rules are available, select the option “Equals” and add “200” on the pattern to test to check whether the testing URL returns a response code “200” or not.
  • JMeter has a special listener to view assertion results, to add assertion results, go to Listener from the thread group and click on Assertion Results.
  • Now execute the test plan and observe the test results through “View Results Tree”, “Assertion Results”, “View Results in Table” etc. The assertion will be passed if the expected response code returned otherwise it will be failed.
Response Assertion Results

Duration Assertion

Duration assertion will check whether the response was received within the expected time. The assertion will be passed if the response was received within the given amount of time and it will be failed if not received in the given time.

Use the following steps to setup duration assertion in JMeter.

Duration Assertion
  • Same as adding response assertion, you can add duration assertion. For this, right click on Thread Group, go to Add>Assertions and click on Duration Assertion.
  • Specify the duration in asserts in milliseconds. In the screenshot above, I have added 3000 milliseconds in duration.
  • Run the Test Plan and observe the results. In the screenshot below, on the five HTTP requests two of them are failed and other are passed. Here one of the failed request has taken 3961 milliseconds for loading.
Duration assertion results

Size Assertion

As the name suggested that size assertion checks the size of the response. In size assertion we set the limits for the size with specifying the size to be equal to, greater than or not equal to a given number of bytes. The assertion will be failed if size of the response received does not matches to the conditions provided.

Use the following steps to setup size assertion in JMeter.

Size Assertions
  • To add size assertion in JMeter test plan, right click on Thread Group, go to Add>Assertions and click on Size Assertion.
  • Specify the size to assert in bytes and also specify the type of comparison. In the screenshot above, I have added the size “71031” in bytes and selected “=” on the type of comparison.
  • Run the Test Plan and observe the results. In the screenshot below, among the five HTTP requests two of them are failed and other are passed. In the screenshot below, two requests are failed due to the request size is 71030 bytes.
Size Assertion Results

XML Assertion

we will use XML assertion in JMeter to check whether the provided URL consist of valid XML data. If the invalid XML code found, failure will be reported with an assertion failure message.

To verify XML assertion, you just have to add XML Assertion same as the way of adding other assertions and run the test plan. Following screenshot shows the test result when tested with HTML page.

XML Assertion

HTML Assertion

Similar to the XML assertion, HTML assertion validates HTML syntax of the HTML document. It will check whether the response received is a well formed HTML, XHTML or XML document and show the validation messages.

You can add HTML assertion going through Thread Group same as adding other assertions. You can select the document type and HTML formats such as HTML, XHTML or XML as shown on the screenshot below and run the test plan to view the results.

HTML Assertion

XPath Assertion

The Xpath assertion will find the specified HTML element in XPath format is present or not. It also returns the number of occurrences if the target element was found.

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 7 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Write A Comment