Python-based interface automation - manipulation of the JSON module

Table of contents

introduction

1. What is JSON?

2. Valid data types of JSON

3. Use of Python JSON library

epilogue


introduction

        When using Python for interface automation test scripts, generally the script only writes the interface test logic implementation, and the test case data required when executing the script is written into excel, database or specified configuration file, and the script reads these tests Data is used to execute different test cases, so that the separation of test scripts and test data is achieved, and it is easy to maintain the subsequent realistic scripts. This part of the follow-up interface automation framework introduces how to realize the separation of scripts and data.

When a python script builds an interface test request, such as when sending a request such as put or post, the request body data is generally sent in JSON format. The interface test data we write is generally carried out by referring to the interface document description, and That is, the request body of the written interface test data is generally in JSON format.

Since the python data type is different from the standard JSON data format, the JSON data needs to be converted into a python object before the python script reads the data for interface testing.

For json data exchange, python language encapsulation implements a related module: json, the following introduces the main operations and applications of the python json module.

1. What is JSON?

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy for humans to read and write. In current application software, front-end and back-end data interactions generally use JSON

2. Valid data types of JSON

In JSON, general data exists in the form of key-value pairs, and the data is enclosed in curly braces {}, and the value value must be one of the following data types:

  • string
  • number
  • object (JSON object)
  • array
  • Boolean
  • Null

JSON values ​​cannot be one of the following data types:

  • function
  • date
  • undefined

Example of valid JSON data:

字符串:{ "name":"John" }
数字:{ "name":123 }
对象:{"employee":{ "name":"Bill Gates", "age":62, "city":"Seattle" }}
数组:{"employees":[ "Bill", "Steve", "David" ]}
布尔:{ "sale":true }
null:{ "middlename":null 
If the json is more complicated, you can go to the json online parsing website: JSON online | JSON parsing and formatting—SO JSON online tool to verify whether the standard of json is correct.

3. Use of Python JSON library

To use JSON functions, you need to import the json library: import json

After opening the Pycharm tool and importing the json library, the method of packaging under the module is as follows:

It mainly encapsulates json.dumps(), json.loads(), json.dump(), json.load()

json.dumps(): Encode Python objects into JSON strings

json.loads(): decode an encoded JSON string into a Python object

json.dump(): Serialize Python's built-in type into a json object and write it to a file

json.load(): convert the json format string read in the file into python type

The python data type and json type conversion relationship table is as follows:

Convert python data to json type:

Convert json type to python data type:

Example of using json.dumps():

Edit the following code:

import json
data1 = (1,2,3) # 元组
data2 = {'张山':'25','王五':'30'} # 字典
data3 = [1,2,3] # 列表
print(json.dumps(data1))
print(json.dumps(data2))
print(json.dumps(data3))

The result of the operation is as follows:

[1, 2, 3]
{"\u5f20\u5c71": "25", "\u738b\u4e94": "30"}
[1, 2, 3]

After running, it is found that all python objects are converted into json type, among which tuples and lists are converted into json arrays, dictionaries are converted into json format with double quotes, and strings are converted into ascii codes by default. If you do not want to convert into ascii codes, Change the above code to the following:

print(json.dumps(data2,ensure_ascii=False))

Example of using json.loads():

Edit the following code:

import json
data1 = '{"name":"zhangshan","age":25}'
print(json.loads(data1))

  The output is as follows:

{'name': 'zhangshan', 'age': 25}

Example of using json.dump():

Edit the following code:

import json
 
data = {
    'name': 'wangwu',
    'liebiao': [1, 2, 3, 4],
    'yuanzu': (1, 2, 3)
}
with open('json_test.txt', 'w+') as f:
    json.dump(data,f)

  After the operation is completed, a json_test.txt file will be generated under the same path. The content of the file is as follows:

 Example of using json.load():

Based on the file generated above, read the content of the file, the code is as follows:

import json
 
data = {
    'name': 'wangwu',
    'liebiao': [1, 2, 3, 4],
    'yuanzu': (1, 2, 3)
}
with open('json_test.txt', 'w+') as f:
    json.dump(data,f)
 
with open('json_test.txt') as f:
    print(json.load(f))

  The output is as follows:

{'yuanzu': [1, 2, 3], 'name': 'wangwu', 'liebiao': [1, 2, 3, 4]}

The above is the use and application of the relevant methods of the python json library. When there is subsequent interactive conversion between json data and python types, the json library needs to be introduced to realize data conversion.

epilogue

If you think the article is not bad, please like, share, and leave a message , because this will be the strongest motivation for me to continue to output more high-quality articles!

If there is something you don’t understand, you can go to this Python interface automation test practical tutorial, which is quite detailed!

------------------------------

[Message: Automated testing, free tutorials]

From entry to proficiency [Python + interface automation testing] advanced collection of actual combat projects! !

Guess you like

Origin blog.csdn.net/weixin_67553250/article/details/131032320