CS/IT Tutorials Collections

6 Common Types Of Assertions in Jmeter With Useful Examples

Pinterest LinkedIn Tumblr

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

We have also mentioned the assertion in some previous articles written on Selenium web driver 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

Types of Assertions in JMeter

Following are the common types of assertions used on JMeter.

1. 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.

Example of Adding Response Assertion

Before adding assertions in the 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 in the previous tutorial “Getting Started with JMeter“. Use the following steps to set up response assertion after adding samplers and listeners in JMeter.

Response Assertions
  • First, right-click on the 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 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 is returned otherwise it will be failed.
Response Assertion Results

2. 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.

Example of Adding Duration Assertion

Use the following steps to set up duration assertions 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 failed and others are passed. Here one of the failed requests has taken 3961 milliseconds for loading.
Duration assertion results

3. 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 by specifying the size to be equal to, greater than, or not equal to a given number of bytes. The assertion will be failed if the size of the response received does not match the conditions provided.

Example of Adding Size Assertion

Use the following steps to set up size assertions in JMeter.

Size Assertions
  • To add size assertion in the 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 failed and others are passed. In the screenshot below, two requests failed due to the request size being 71030 bytes.
Size Assertion Results

4. XML Assertion

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

Example of Adding XML Assertion

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

XML Assertion

5. HTML Assertion

Similar to the XML assertion, HTML assertion validates the 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.

Example of Adding HTML Assertion

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

HTML Assertion

6. 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.

Example of Adding Xpath Assertion

You can add Xpath assertions going through Thread Group same as adding other assertions in Jmeter. You can select the document type and HTML formats such as HTML, XHTML, or XML and run the test plan to view the results.

Author

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

Comments are closed.