{"id":726,"date":"2016-04-18T10:38:10","date_gmt":"2016-04-18T10:38:10","guid":{"rendered":"https:\/\/loadfocus.com\/blog\/?p=726"},"modified":"2024-02-25T22:03:14","modified_gmt":"2024-02-25T22:03:14","slug":"how-to-use-assertions-in-apache-jmeter-tests","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/2016\/04\/how-to-use-assertions-in-apache-jmeter-tests","title":{"rendered":"How to use assertions in Apache JMeter tests"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 5<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><h2>What are assertions ?<\/h2>\n<p class=\"lead\">In case you are not familiar with the term,\u00a0an assertion &#8220;is a test \/ a condition that must be tested to confirm conformance to a requirement&#8221;.\u00a0Basically a test assertion is \u2013 a condition that compares the expected value with the actual value for a specified use case.<\/p>\n<h2>Why should I use assertions ?<\/h2>\n<p>Before we deliver a product, we need to run many test assertions to make sure the product meets the customer&#8217;s expectations. Also, you can check our intro blog post regarding <a href=\"https:\/\/loadfocus.com\/blog\/2016\/04\/13\/how-to-use-apache-jmeter-to-load-test-a-web-server\/\">how to run a load test with Apache JMeter<\/a>.<\/p>\n<h2>Should we use assertions in the JMeter tests\u00a0?<\/h2>\n<p>Usually assertions are not used when we create the scripts for load or stress testing an application. Most of the tester that run<br \/>\nthese kind of tests base their results only on the response status codes that the application under test returns. This might be a problem\u00a0and might make the final results not accurate.<\/p>\n<h2>What are the cases in which not using an assertions may affect your final results ?<\/h2>\n<p>Not all the time a successfull response from the server (200 OK) means that the application responded as expected.<br \/>\nYou may ask yourself and the a few cases are mentioned below:<\/p>\n<ol>\n<li>the application redirects to a maintaince page<\/li>\n<li>the application responds a 200 OK but sends an error message in the body of the response indicating a error message<\/li>\n<li>the application redirects to an error page and sends back a 200 OK with and error message indicating why the error returns<\/li>\n<\/ol>\n<p>To avoid these kind of errors to affect\u00a0your performance report is a very good idea to use assertions:<\/p>\n<ul>\n<li>in <em>GET\/POST\/PUT\/PATCH<\/em> methods<\/li>\n<li>in API performance testing<\/li>\n<li>in functional testing<\/li>\n<\/ul>\n<p>Be aware that if your are creating a long running test or a test with a lot of requests is good not to use a lot of assertions as<br \/>\nthey tend to consume quite a bit of memory which could make your <strong>JMeter<\/strong> tool crash and means starting the test again.<\/p>\n<h2>What type of assertions does JMeter support ?<\/h2>\n<p>There are several types of assertions that you can use in your test scripts.<br \/>\nThe most used ones are:<\/p>\n<ul>\n<li>&#8220;<strong>Response Assertion<\/strong>&#8220;: the response assertion control panel lets you add pattern strings to be compared against various fields of the response<\/li>\n<li>&#8220;<strong>XPath Assertion<\/strong>&#8220;: the XPath Assertion tests a document for well formedness, has the option of validating against a DTD, or putting the document through JTidy and testing for an XPath (if that XPath exists, the Assertion is true)<\/li>\n<li>&#8220;<strong>HMTL Assertion<\/strong>&#8220;: the HTML Assertion allows the user to check the HTML syntax of the response data using JTidy.<\/li>\n<li>&#8220;<strong>XML Assertion<\/strong>&#8220;: tests that the response data consists of a formally correct XML document; it does not validate the XML based on a DTD or schema or do any further validation.<\/li>\n<li>&#8220;<strong>Compare Assertion<\/strong>&#8220;: can be used to compare sample results within its scope; either the contents or the elapsed time can be compared, and the contents can be filtered before comparison.<br \/>\nCompare Assertion MUST NOT BE USED during load test as it consumes a lot of resources (memory and CPU). Use it only for either functional testing or during Test Plan debugging and Validation.<\/li>\n<li>&#8220;<strong>Size Assertion<\/strong>&#8220;: verifies that each response contains the right number of bytes in it; you can specify that the size be equal to,<br \/>\ngreater than, less than, or not equal to a given number of bytes<\/li>\n<\/ul>\n<h1>How much do assertions consume in terms of CPU and memory ?<\/h1>\n<p>If the test that your are using is a long running test or makes a very large number of requests you may encounter unexpected failures\u00a0related to &#8220;out of memory&#8221; errors or similar.<\/p>\n<p>In terms of resources the HMTL Assertion, XML Assertion or Compare Assertion consume a lot\u00a0of resources so is a good idea to avoid using those in long running tests. The other assertions does not have a very bad impact on the\u00a0resources so they can be used without a problem.<\/p>\n<h1>Which is the scope to which the assertions apply ?<\/h1>\n<p>There are 3 cases related to which scope an assertion applies to:<\/p>\n<p>1. applies to all the child requests<\/p>\n<img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"538\" class=\"aligncenter size-medium wp-image-733\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/Screen-Shot-2016-04-11-at-11.05.53.png\" alt=\"Screen Shot 2016-04-11 at 11.05.53\" \/>\n<p>2. applies to all the sibling requests<\/p>\n<img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"554\" class=\"aligncenter size-full wp-image-732\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/Screen-Shot-2016-04-11-at-11.05.47.png\" alt=\"Screen Shot 2016-04-11 at 11.05.47\" \/>\n<p>3. applies only to one specific request<\/p>\n<img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"560\" class=\"aligncenter size-full wp-image-731\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/Screen-Shot-2016-04-11-at-11.05.38.png\" alt=\"Screen Shot 2016-04-11 at 11.05.38\" \/>\n<h2>How to use a &#8220;Response Assertion&#8221; in JMeter ?<\/h2>\n<p>Response Assertion is one of the most widely used assertion as it allows to verify the content of the response, status code or headers.<\/p>\n<p>It can be configured to verify:<\/p>\n<ul>\n<li>the different segments of the response like text (response body)<\/li>\n<li>the document type like pdf, doc etc<\/li>\n<li>the headers<\/li>\n<li>the response code (<em>2xx, 4xx<\/em>)<\/li>\n<li>response message (description of code) and response headers<\/li>\n<li>tests the response against a pattern string which can be a plain text or a Perl5 regular expression; the specified pattern is matched against the selected segment of the response<\/li>\n<\/ul>\n<p>Let&#8217;s start by adding a response assertion that expects that the response code is <strong>200<\/strong>.<\/p>\n<img loading=\"lazy\" decoding=\"async\" width=\"2554\" height=\"1478\" class=\"aligncenter size-full wp-image-735\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/Screen-Shot-2016-04-11-at-11.24.32.png\" alt=\"Screen Shot 2016-04-11 at 11.24.32\" \/>\n<p>We will now add another assertion that expects that the text &#8220;wikipedia&#8221; is present in the response of the server.<\/p>\n<img loading=\"lazy\" decoding=\"async\" width=\"2556\" height=\"1478\" class=\"aligncenter size-full wp-image-736\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/Screen-Shot-2016-04-11-at-11.26.17.png\" alt=\"Screen Shot 2016-04-11 at 11.26.17\" \/>\n<p>Running the script will show that that the request succeeded without any problem.<\/p>\n<img loading=\"lazy\" decoding=\"async\" width=\"2558\" height=\"1482\" class=\"aligncenter size-full wp-image-737\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/Screen-Shot-2016-04-11-at-11.26.34.png\" alt=\"Screen Shot 2016-04-11 at 11.26.34\" \/>\n<p>Now we will change the text of the second assertion to &#8220;dummy_text_inexistent&#8221; and run the test again.<\/p>\n<img loading=\"lazy\" decoding=\"async\" width=\"2554\" height=\"1480\" class=\"aligncenter size-full wp-image-738\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/Screen-Shot-2016-04-11-at-11.29.34.png\" alt=\"Screen Shot 2016-04-11 at 11.29.34\" \/>\n<p>In the next tutorials we will show you how to use\u00a0the rest of the assertions that are supported in JMeter.<\/p>\n<p>You can also use <a href=\"https:\/\/loadfocus.com\">LoadFocus<\/a> to <a href=\"https:\/\/loadfocus.com\">load test your websites<\/a>, APIs or any web-app with thousands of users from AWS cloud locations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 5<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>What are assertions ? In case you are not familiar with the term,\u00a0an assertion &#8220;is a test \/ a condition that must be tested to confirm conformance to a requirement&#8221;.\u00a0Basically a test assertion is \u2013 a condition that compares the expected value with the actual value for a specified use case. Why should I use&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/2016\/04\/how-to-use-assertions-in-apache-jmeter-tests\" class=\"more-link\" title=\"Read How to use assertions in Apache JMeter tests\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,6,48,1],"tags":[145,144],"class_list":["post-726","post","type-post","status-publish","format-standard","hentry","category-load-testing","category-performance-testing","category-test-automation","category-uncategorized","tag-jmeter-assertions","tag-use-assertions-in-jmeter"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/comments?post=726"}],"version-history":[{"count":1,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":2947,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/posts\/726\/revisions\/2947"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/media?parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/categories?post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/wp-json\/wp\/v2\/tags?post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}