The most commonly used assertion is the Response Assertion, which checks whether a response text/body/code/message/header contains, matches, or equals a specified pattern.
- The Pattern can be either be :
- a string for Equals or Substring clauses
- a Perl5-style Regular Expression for Contains or Matches clause
Response Entities that can be checked include :
- Text response: This is for the response that can be displayed in a browser
- Document (text): This is for anything supported by Apache Tika (it assumes the presence of apache-tika.jar in /lib folder of a JMeter installation).
This can include PDF, Office, audio, and video formats. Be careful, because this can be memory-intensive for high loads.
- URL Sampled: This assertion is used against a requested URL to ensure it matches expectations. For example, you may want to check that the redirect URL does not contain an error somewhere in the path.
- Response Code : This checks to ensure the response code is expected. For 4xx and 5xx response codes, make sure you have checked the Ignore Status box (see below for a full explanation).
- Response Message : This verifies that the response message appears as expected.
- Response Headers : This is used against Response Headers to see if a specific HTTP header is present or absent.
- Ignore Status : JMeter out-of-the-box considers all 4xx and 5xx responses as failures. If your test case is negative and, for example, a 404 error is expected, you will check this box to suppress JMeter’s built-in status code check and substitute it with your own status code assertion.
The Following Example demonstrates how to validate JMeter Tests Pass/Fail Status
- Launch the JMeter in your system and then
- Open any recorded files. Here I am opening HTTP(s) Cookie Manager
- Once the Recorded file has been opened in the JMeter, Add Thread group into the Test Plan and then Copy and paste the test script into the Thread Group.
- Right Click on the Test Plan and then
Add-->Config Element-->HTTP Cookie Manager, if your test plan does not have a cookie manager
- We have added HTTP Cookie Manager into the Test Plan.
- Next, add the View Results Tree in the HTTP Request and run the test plan so that some results will be stored in the View Results Tree.
- Now click on the authenticate-81 script and then click on the Response Data and then scroll down to the sentence as below.
- Now copy the Test-inside the
h4 and then go to the
Authenticate-81 a request under Thread Group, right-click on it and then add Assertion.
- The Response Assertion will look like as below. Select the Click on Text Response in the Field to Test and then click on Contains under Pattern Matching Rules.
- Now Go back to the View Results Tree and Select the Text(Welcome to the Secure are When you are done click logout below) and then go to the Purchase request under the Thread group and then click on the Add From Clipboard.
- That means in the Response check if it contains Welcome to the Secure Area. When you are done click logout below. If this text is present then it is Pass or else it is failed.
- Now add one more Response Assertion to the Authenticate-81 Request under the Thread Group.
- And then click on the Response code and then check the response code in the View Results Tree and enter the same in the Response Assertion.
- Enter the Response code 200 as shown below and then click on the Equals.
- Now, save the Test As Assertions.jmx and run the file.
- We have passed the assertions without any error.
- Now delete the Cookie Manager and Clear all the View Results Tree and then run the Test Plan, you can see that the assertion has failed here.
- After the execution of the Test Plan, you can see an Assertion error as shown below.
- If you extend the /Authenticate-81, then you can see that the Response Assertion has been failed.
- Now add Cookie Manager to the Thread Group and run the test plan again you won't get any error.
- If you want to see the results of assertion, you can add Assertion Result under the Test Plan(Add-->Listerner-->Assertion Results)
- Once you add this Assertion listener this will show you what are the assertions you have, what are the expectations, and what are the actual logs will be displayed in the Assertion Results.
- Now run the test plan again the assertion result will clearly show the error.
- But when you add assertions and the HTTP cookies you will get the output assertions neatly.
- So in this way you need not validate the assertions manually, the Assertion Result will display the errors.