In Part-2 we have seen how to Install JMeter, Create a Test Plan and Run the Test. Here we will go through how to do the Assertions.
Assertions in JMeter
Assertion verifies that your server under test returns the expected results.
Following are some of the assertions used in JMeter.
1. Response Assertion
2. Duration Assertion
3. Size Assertion
4. HTML Assertion
5. XML Assertion
6. XPATH Assertion
Response Assertion
The response assertion lets you add pattern strings to be compared against various fields of the server response.
For example, you send a user request to some website and get the server response. You can use Response Assertion to verify if the server response Equals the expected pattern string (e.g. “OK”).
Right-click on Group Thread(Users)
Go to Add--> Assertions--> Response Assertion
Here we can test the Text Response, Document, URL Sampled, Response Code, Message, and Headers. For Now, choose Response code .
When you send a request to server, it may return some response code as below:
404: Server error
200: Server OK
Here we will be verifying the web server numpyninja.com response is ‘Equals’ pattern 200.
On Response Assertion Panel, click Add -> a new blank entry display -> enter 200 in Pattern to Test.
Now Run the test by clicking green button.
When you see the results generated, If the Response Assertion matches you can see the status green which means the test assertion is passed.
You can check the negative case by giving the wrong status code.
Let us give 201 in the Patterns to Test.
Now rerun the test and check the status.
You can see status as Red which means that the Assertion test failed .
When you expand the test you can that assertion is failed.
For a better view of assertion results, we can add a listener called Assertion results. For this
· Right-click on Thread Group
· Click Add-->Listener-->Assertion Results
Run the test again
If the Assertion fails you can see the failure message in Assertion results as below.
If the Assertion is passed, you will not get any exceptions .
Duration Assertion
The Duration Assertion tests that each server response was received within a given amount of time. Any response that takes longer than the given number of milliseconds (specified by the user) is marked as a failed response.
For example, if a user request is sent to www.numpyninja.com by JMeter and gets a response within the expected time of 1000 ms then the Test Case will pass, otherwise, the test case will fail.
To do this
· Right-click on Thread Group
· Go to Add-->Assertion-->Duration Assertion
Here I am giving 1000 ms in the Duration and running the test again.
Below you can see the Requests with more than 1000ms Response time are failed.
You can also check the Assertion in Assertion Results.
Size Assertion
The Size Assertion tests that each server response contains the expected number of byte in it. You can specify that the size be equal to, greater than, less than, or not equal to a given number of bytes.
To do this
Right-click on Thread Group
Go to Add-->Assertion-->Size Assertion
For now, JMeter sends a user request www.numpyninja.com and gets a response packet with a size less than expected byte of 748500 bytes a test case pass. If else, the test case failed.
Run the test
Check the Results
Above you can see Response with greater than 748500 bytes failed and less than 748500 bytes passed. You can also check this in Assertion results as below.
HTML Assertion
The HTML Assertion allows the user to check the HTML syntax of the response data. It means the response data must meet the HTML syntax.
To do this
Right-click on Thread Group
Go to Add-->Assertion-->HTML Assertion
Here let’s check by giving the error threshold ( count of errors that can be ignored and considered the request as pass) and the warning threshold (number of warnings which can be ignored and considered the request as passed) as 0. Then Run the test.
Above you can see failed status as the errors here are 44 and warnings are 677 we have given in the assertion as 0.
Now lets give error threshold as 44 and warning threshold as 677. Then rerun the test.
Above you can see that tests are passed.
If you want to log the errors you can give the location where to log the errors in Filename as below.
The errors and warnings will be logged into that file as below.
XML Assertion
In JMeter, XML Assertion is mainly used to validate the format of XML embedded in the response. This type of assertion does not need any input. When the response is well XML formatted then the sampler is passed else failed with an error.
To do this
· Right-click on Thread Group
· Go to Add-->Assertion-->XML Assertion
Run the Test
Above you can see the failed Assertion
Troubleshooting
If you face the issue while running the above scenarios … do the following:
Check whether you are connecting to the internet via a proxy. If yes, remove the proxy.
Open a new instance of JMeter
Open the Assertion Test plan , here in our case it is MyFirstTestPlan.jmx JMeter
Click on Thread Group -> Assertion Result
Run the Test
For Part-1 and Part-2 you check the below links
Comentários