JMeter assertion JSON assertion

JSON assertion

If the Response Body returned by the server is in JSON format, it is a better choice to use JSON assertions to determine the test results.

First, you need to extract the actual results that need to be judged from the returned JSON data according to the JSON Path, then set the expected results, and compare the two to get the assertion result.

The following first introduces the basic knowledge related to JSON and JSON Path.

1. JSON and JSONPath

▲ What is JSON

● JSON (JavaScript Object Notation, JavaScript Object Notation) is a lightweight data exchange format.

● JSON is easy to understand, easy to read and write; at the same time, it is easy for computers to parse and generate, so JSON has a wide range of applications.

JSON is based on the following two structures:

1. A collection of name/value pairs

In various languages, this can be an object, record, struct, dictionary, hash table, keyed list, or associative array )to fulfill.

2. An ordered list of values

In most languages, this is implemented as an array, vector, list, or sequence.

JSON is used to describe text data structure and has the following forms:

1.Object

An object is an unordered set of name/value pairs. The object starts with {(left brace) and ends with }(right brace). Each name is followed by a colon, and name/value pairs are separated by commas.

For example:

{"name":"zhangsan","sex":1,"age":25}

2.Array

An array is an ordered collection of values. The array starts with [(left bracket) and ends with ](right bracket). Separate values with commas.

For example:

{
"man":[{"name":"zhangsan","sex":1,"age":21},
{"name":"lisi","sex":0,"age":18},
{"name":"wangwu","sex":0,"age":25}]
}

3. value

Values can be strings, numbers, true/false, null, objects, or arrays.

4. string

A string is a sequence of zero or more Unicode characters, enclosed in double quotes and escaped with backslashes.

Characters are represented as a single string. Strings are very similar to strings in C or Java.

5.Number

A series of number combinations from 0 to 9, which can be negative numbers or decimals. Exponential form can also be expressed using e or E; numbers are very similar to C or Java numbers, but just not in octal and hexadecimal formats.

6. whitespace

Whitespace characters (including spaces, line feeds, carriage returns, and horizontal tabs) can be inserted between any pairs of syntax symbols.

Now I have also found a lot of testing friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

▲ What is JSONPath

We often use XPath to parse, transform, and selectively extract data from XML documents. Similar to XPath, JOSNPath makes it easy to discover and extract data from JSON structures.

The “root member object” in JSONPath is always called $, whether it is an object or an array. JSONPath expressions have two different expression styles: “dot-notation” (. sign) and “bracket-notation” ([] sign).

For example,

$.store.book[0].title or
$['store']['book'][0]['title']

▲ JSONPath syntax elements

▲ JSONPath Example

{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honor",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}

2. JSON assertion

JSON assertions can verify JSON documents returned by the server.

JSON assertions have two usage patterns:

1. Can the path be found in the JSON document according to JSONPath;

2. Extract the value based on JSONPath and validate the value.

● If the document format is non-JSON, the assertion fails;

● If the path cannot be found, the assertion fails;

● If the extracted value is inconsistent with the expected value, the assertion fails.

▲ Configuration items

Assert JSON Path exists:

The path (JSONPath) of the JSON element used for assertions.

1.Additionally assert value

Whether to additionally validate extracted values against JSONPath.

● If unchecked, verify whether JSONPath can find the path in the JSON document;

● Check to verify whether the value extracted based on JSONPath is expected.

2.Match as regular expression

Whether the expected value can use regular expressions.

● If unchecked, the expected value cannot be expressed by regular expressions;

● Check, the expected value can be expressed using regular expressions.

Expected Value:

Expected value.

1.Expect null

● If the value extracted is verified to be null, check this option.

There are two areas that require additional attention:

a. To verify the null value, you still need to check “Additionally assert value”, otherwise the verification is whether JSONPath can find the path;

b. If the expected value is not filled in, it means a blank character, which is not equivalent to null.

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

● If checked, it means the assertion result will be inverted.

Note:

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

3. Application cases

Here we still use the interface for querying the total amount of purchased goods introduced earlier as an example to describe the use of JSON assertions.

The response data returned by this interface is JSON, so JSON assertions can be used.

▲ Operation steps

1. Parameterize the items to be verified for the expected results

Verify err_msg, result here

Write expected results into csv file

For example:

case_name,goods_id,goods_attr,goods_num,error_msg,rslt

case1,9,226,3,,¥6630 yuan

case2,,226,1, The specified product was not found or the specified product attribute was not found. ,

case3,9,,1,,¥2298 yuan

case4,9,226,,¥2308 yuan

2. Add JSON Assertion and configure it

JSON assertions can only assert one parameter at a time, so multiple JSON assertions need to be added here.

Assume that assertions are made on the two return parameter values of error message and product amount.

{
"err_msg": "The specified product was not found or the specified product attribute was not found.",
"result": "",
"qty": 1,
"err_no": 1
}
$.err_msg --> The specified product was not found or the specified product attributes were not found.
$.qty --> 1

▲ JSON assertion configuration

1. Assert error messages

2. Assert the commodity amount:

END Today’s sharing ends here. If it helps you, leave a message!

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeAdvanced grammarCommon standard library 386501 people are learning the system