Introduction to JSON Assertions of Jmeter's Common Assertions

JSON assertions can be used to validate JSON documents returned by the server.
JSON assertion has two usage modes:
1. Whether the path can be found in the JSON document according to JSONPath;
2. Extract the value and verify the value according to JSONPath.
Result judgment : If the document format is not JSON, the assertion fails; the assertion fails when the path cannot be found; the assertion fails when the extracted value is inconsistent with the expected value.

1. Add assertion method

As needed, assertions can be added under [Test Plan], [Thread Group], and [Thread Request], generally under the corresponding [Thread Request], as shown in the figure below, which is what this article introduces.
Add path: [thread request-right click-add-assertion-JSON assertion]
insert image description here

Two, Json assertion

2.1. Examples

If the response data is returned in Json format, we can perform json assertion operations on it, and extract the value from the response data in the view structure tree of the request to assert it. The example extracts the value of the msg field in the response data to assert

2.1.1. Interface response result

insert image description here

2.1.2.json assertion settings are as follows

insert image description here

2.2.Detailed explanation of json assertion configuration items

2.2.1. Name

The name of the assertion can be filled in according to your own naming convention as needed

2.2.2. Notes

Assertion comments can be filled in according to your own specifications as needed

2.2.3.Assert JSON Path exists:

The path (JSONPath) and field value of the JSON element used for the assertion

2.2.4.Additionally assert value

Whether to additionally verify: whether the value extracted according to JSONPath is correct
a. Unchecked. Then: Verify that JSONPath can match the path in the JSON document
b. Check it. Then: verify that the value extracted according to JSONPath is as expected

2.2.5.Match as regular expression

Whether the expected value can use a regular expression
a. Unchecked. Expected value: Cannot be expressed using regular expressions
b. Checked. Expected value: Can be expressed using regular expressions

2.2.6.Expected Value:

Fill in the expected value of the extracted field

2.2.7.Expect null

If the extracted value is verified to be null, check this option.
There are two points that need additional attention :
a. To verify the null value, you still need to check "Additionally assert value", otherwise the verification is whether JSONPath can match the path
b. If the expected value is not filled, it means an empty character, which is not equivalent to null

2.2.8.Invert assertion(will fail if above conditions met)

If checked, it means that the assertion result is negated

2.2.9. Note:

In addition to null, there is also a special value, which is an empty array. The expected value cannot be left blank. It needs to be set to: [], which means an empty array.

3. Assertion result view

3.1. [Assertion result] component
Adding path: [Test plan - right click - Add - Listener - Assertion result]
insert image description here
All assertion result error messages of all requests under the entire test plan will be displayed here, as shown in the figure below, all The error message of the assertion result is displayed directly.
insert image description here
3.2. [View result tree] Component
adding path: [Test plan - right click - Add - Listener - View result tree], all assertion result error messages of all requests under the entire test plan will be displayed here, as shown in the figure below , to toggle viewing.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_37600187/article/details/129288752