Working with Assertions

The information in requests that we have sent to the server is verified and validated using assertions. We can compare the expected outcome with the actual result using assertions. For instance, we would use response assertion to add the text to the contains column if we wanted to see a message similar to the welcome user message. If the specified text appears in the server's response, the assertion is true; otherwise, the test fails when that request is made. One of the assertions offered by JMeter in this case is the response assertion. As shown below, JMeter supports a wide range of assertions.

JMeter's assertions include:

  • Bean Shell Assertion.
  • Comparative BSF assertion
  • Duration assertion
  • HTML assertion
  • Assertion JSR223
  • Assertion Size
  • MD5Hex Assertion Response
  • SMIME Assure
  • Assertion in XML
  • XML Schema
  • The XPath Assertion

Including assertions in the test plan:
Similar to adding a thread group to the test plan, adding assertions does the same. Test plan/thread group >> Add >> Assertions >> Required Assertions is the order in which to add an assertion. You can also make use of this picture.

We'll now quickly define a couple of the most utilized assertions.

Response Assertion:
Response assertions are used to check the patterns in the server's response body. To validate the response, various pattern matching rules, such as contains, matches, equal, etc., can be applied. These comprise the response assertion's fundamental components.
Under Filed to test, select the Response code radio button, and under Patterns to test, click the add button. And then add 200 as seen in the image. The response code assertion is shown here.

It should pass for 200. If you adjust 200 to 500 and run again, the assertion should fail as indicated in the image.


Size Assertion:
The expected number of bytes is confirmed using the size assertion method. We can provide the anticipated size in bytes and check using various comparisons like >,, =, etc. The following elements make up JMeter's Size assertion.

When you run the file, note below values. We will use these values in size assertion.

Once you add size assertion you will observe below screen, here I have selected Response body Radio button and entered value 4474 from above screen.as shown in below image.

 When you run it should pass for =, >= and <=. For all other signs it should fail.

Bean Shell Assertion:
We may verify a sampler's output using BeanShell Scripting and the BeanShell Assertion. The foundational components of a BSF assertion in JMeter are as follows.


In the above image, I've added code for checking response code. Since I'm comparing with 200 code, it will pass. But if I compare with code 300 then it will fail as shown in the below image.


Duration Assertion:
One of the claims in JMeter that is frequently used is the duration assertion. We may check whether the request is being completed within the allotted time using a duration assertion. 

In the thread group, I have added following configurations

For single user Sample time is 940 miliseconds

Now, we'll add duration assertion for 10 users as follows,
Thread group Configurations:

Duration assertion configuration:

Now click on run and check if it fails or not
It passes for 1000 milliseconds
You can try with different duration and check if it fails or pass

Comments

Popular Posts