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
Types of Assertions in JMeter
Following are the common types of assertions used on JMeter.
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
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.
- 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 “
- 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.
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.
- 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
requesthas taken 3961 milliseconds for loading.
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.
- 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.
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.
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.
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.