手把手教你学会接口自动化系列十一-将用例写在json中,持久化管理起来下

发布时间:2024年01月15日

上一篇我写了登录,我们发现json还是没有什么大问题,还蛮好用的,但是我们再写下一个,比如线索新建接口的时候,我们写着写着会发现问题:

我们写获取url的没有问题,代码如下:

# !/usr/bin/env python

# -*- coding: utf-8 -*-

# @Time : 2024/01

# @Author : Laopi

import json



import requests



from config.read_json_file import read_json_file

from config.setting import ConfigHandler

from tools.yamlControl import GetYamlData



baseUrl = GetYamlData(ConfigHandler.config_path).get_yaml_data()



jsonpath = (ConfigHandler.root_path+'/testdata/demo2.json')

# 读取JSON文件

url = baseUrl['host']+read_json_file(jsonpath)['url']

print(url)

下一个我们要获取的是headers,但是有个问题,这次的headers传值不像之前的登录,他还要传递登录获取的令牌cookie

headers={"Content-Type":"application/json","Admin-Token":cookie}

怎么办?我们只有调用之前的tokenCreate.py把生成的Admin-Token写入到demo2.json中,由次,我们开始编写json写入的程序代码

file_path = ConfigHandler.root_path+'/testdata/demo2.json'

print(file_path)

data = read_json_file(jsonpath)

data['headers']['Admin-Token']=get_token()

with open(file_path, 'w') as file:

json.dump(data, file)

上一步中,第一行代码是获取文件路径

在通过读取之前的json文件,通过代码

data['headers']['Admin-Token']=get_token()

将最新获取的token写入文件。

最后形成的文件内容如下:

{"url": "/CrmLeads/addOrUpdate", "method": "POST", "headers": {"Content-Type": "application/json", "Admin-Token": "44669243e3ff4151a37456913a6b7ec1"}, "casedata": [[{"entity": {"leads_name": "\u6536\u5230\u5c31\u597d2", "telephone": "15991710522", "mobile": "15991710522", "address": "111", "next_time": "2024-01-08 00:00:00", "remark": "11111111"}, "field": [{"fieldId": 2, "formType": "select", "isNull": 0, "name": "\u7ebf\u7d22\u6765\u6e90", "options": "\u4fc3\u9500,\u641c\u7d22\u5f15\u64ce,\u5e7f\u544a,\u8f6c\u4ecb\u7ecd,\u7ebf\u4e0a\u6ce8\u518c,\u7ebf\u4e0a\u8be2\u4ef7,\u9884\u7ea6\u4e0a\u95e8,\u7535\u8bdd\u54a8\u8be2,\u90ae\u4ef6\u54a8\u8be2", "isUnique": 0, "inputTips": "null", "type": 3, "value": "\u641c\u7d22\u5f15\u64ce", "fieldType": 0, "fieldName": "\u7ebf\u7d22\u6765\u6e90", "setting": ["\u4fc3\u9500", "\u641c\u7d22\u5f15\u64ce", "\u5e7f\u544a", "\u8f6c\u4ecb\u7ecd", "\u7ebf\u4e0a\u6ce8\u518c", "\u7ebf\u4e0a\u8be2\u4ef7", "\u9884\u7ea6\u4e0a\u95e8", "\u7535\u8bdd\u54a8\u8be2", "\u90ae\u4ef6\u54a8\u8be2"]}, {"fieldId": 5, "formType": "select", "isNull": 0, "name": "\u5ba2\u6237\u884c\u4e1a", "options": "IT,\u91d1\u878d\u4e1a,\u623f\u5730\u4ea7,\u5546\u4e1a\u670d\u52a1,\u8fd0\u8f93/\u7269\u6d41,\u751f\u4ea7,\u653f\u5e9c,\u6587\u5316\u4f20\u5a92", "isUnique": 0, "inputTips": "null", "type": 3, "value": "\u623f\u5730\u4ea7", "fieldType": 0, "fieldName": "\u5ba2\u6237\u884c\u4e1a", "setting": ["IT", "\u91d1\u878d\u4e1a", "\u623f\u5730\u4ea7", "\u5546\u4e1a\u670d\u52a1", "\u8fd0\u8f93/\u7269\u6d41", "\u751f\u4ea7", "\u653f\u5e9c", "\u6587\u5316\u4f20\u5a92"]}, {"fieldId": 6, "formType": "select", "isNull": 0, "name": "\u5ba2\u6237\u7ea7\u522b", "options": "A\uff08\u91cd\u70b9\u5ba2\u6237\uff09,B\uff08\u666e\u901a\u5ba2\u6237\uff09,C\uff08\u975e\u4f18\u5148\u5ba2\u6237\uff09", "isUnique": 0, "inputTips": "null", "type": 3, "value": "A\uff08\u91cd\u70b9\u5ba2\u6237\uff09", "fieldType": 0, "fieldName": "\u5ba2\u6237\u7ea7\u522b", "setting": ["A\uff08\u91cd\u70b9\u5ba2\u6237\uff09", "B\uff08\u666e\u901a\u5ba2\u6237\uff09", "C\uff08\u975e\u4f18\u5148\u5ba2\u6237\uff09"]}]}], [{"entity": {"leads_name": "\u6536\u5230\u5c31\u597d2", "telephone": "15991710522", "mobile": "15991710522", "address": "111", "next_time": "2024-01-08 00:00:00", "remark": "11111111"}, "field": [{"fieldId": 2, "formType": "select", "isNull": 0, "name": "\u7ebf\u7d22\u6765\u6e90", "options": "\u4fc3\u9500,\u641c\u7d22\u5f15\u64ce,\u5e7f\u544a,\u8f6c\u4ecb\u7ecd,\u7ebf\u4e0a\u6ce8\u518c,\u7ebf\u4e0a\u8be2\u4ef7,\u9884\u7ea6\u4e0a\u95e8,\u7535\u8bdd\u54a8\u8be2,\u90ae\u4ef6\u54a8\u8be2", "isUnique": 0, "inputTips": "null", "type": 3, "value": "\u641c\u7d22\u5f15\u64ce", "fieldType": 0, "fieldName": "\u7ebf\u7d22\u6765\u6e90", "setting": ["\u4fc3\u9500", "\u641c\u7d22\u5f15\u64ce", "\u5e7f\u544a", "\u8f6c\u4ecb\u7ecd", "\u7ebf\u4e0a\u6ce8\u518c", "\u7ebf\u4e0a\u8be2\u4ef7", "\u9884\u7ea6\u4e0a\u95e8", "\u7535\u8bdd\u54a8\u8be2", "\u90ae\u4ef6\u54a8\u8be2"]}, {"fieldId": 5, "formType": "select", "isNull": 0, "name": "\u5ba2\u6237\u884c\u4e1a", "options": "IT,\u91d1\u878d\u4e1a,\u623f\u5730\u4ea7,\u5546\u4e1a\u670d\u52a1,\u8fd0\u8f93/\u7269\u6d41,\u751f\u4ea7,\u653f\u5e9c,\u6587\u5316\u4f20\u5a92", "isUnique": 0, "inputTips": "null", "type": 3, "value": "\u623f\u5730\u4ea7", "fieldType": 0, "fieldName": "\u5ba2\u6237\u884c\u4e1a", "setting": ["IT", "\u91d1\u878d\u4e1a", "\u623f\u5730\u4ea7", "\u5546\u4e1a\u670d\u52a1", "\u8fd0\u8f93/\u7269\u6d41", "\u751f\u4ea7", "\u653f\u5e9c", "\u6587\u5316\u4f20\u5a92"]}, {"fieldId": 6, "formType": "select", "isNull": 0, "name": "\u5ba2\u6237\u7ea7\u522b", "options": "A\uff08\u91cd\u70b9\u5ba2\u6237\uff09,B\uff08\u666e\u901a\u5ba2\u6237\uff09,C\uff08\u975e\u4f18\u5148\u5ba2\u6237\uff09", "isUnique": 0, "inputTips": "null", "type": 3, "value": "A\uff08\u91cd\u70b9\u5ba2\u6237\uff09", "fieldType": 0, "fieldName": "\u5ba2\u6237\u7ea7\u522b", "setting": ["A\uff08\u91cd\u70b9\u5ba2\u6237\uff09", "B\uff08\u666e\u901a\u5ba2\u6237\uff09", "C\uff08\u975e\u4f18\u5148\u5ba2\u6237\uff09"]}]}], [{"entity": {"leads_name": "\u6536\u5230\u5c31\u597d2", "telephone": "15991710522", "mobile": "15991710522", "address": "111", "next_time": "2024-01-08 00:00:00", "remark": "11111111"}, "field": [{"fieldId": 2, "formType": "select", "isNull": 0, "name": "\u7ebf\u7d22\u6765\u6e90", "options": "\u4fc3\u9500,\u641c\u7d22\u5f15\u64ce,\u5e7f\u544a,\u8f6c\u4ecb\u7ecd,\u7ebf\u4e0a\u6ce8\u518c,\u7ebf\u4e0a\u8be2\u4ef7,\u9884\u7ea6\u4e0a\u95e8,\u7535\u8bdd\u54a8\u8be2,\u90ae\u4ef6\u54a8\u8be2", "isUnique": 0, "inputTips": "null", "type": 3, "value": "\u641c\u7d22\u5f15\u64ce", "fieldType": 0, "fieldName": "\u7ebf\u7d22\u6765\u6e90", "setting": ["\u4fc3\u9500", "\u641c\u7d22\u5f15\u64ce", "\u5e7f\u544a", "\u8f6c\u4ecb\u7ecd", "\u7ebf\u4e0a\u6ce8\u518c", "\u7ebf\u4e0a\u8be2\u4ef7", "\u9884\u7ea6\u4e0a\u95e8", "\u7535\u8bdd\u54a8\u8be2", "\u90ae\u4ef6\u54a8\u8be2"]}, {"fieldId": 5, "formType": "select", "isNull": 0, "name": "\u5ba2\u6237\u884c\u4e1a", "options": "IT,\u91d1\u878d\u4e1a,\u623f\u5730\u4ea7,\u5546\u4e1a\u670d\u52a1,\u8fd0\u8f93/\u7269\u6d41,\u751f\u4ea7,\u653f\u5e9c,\u6587\u5316\u4f20\u5a92", "isUnique": 0, "inputTips": "null", "type": 3, "value": "\u623f\u5730\u4ea7", "fieldType": 0, "fieldName": "\u5ba2\u6237\u884c\u4e1a", "setting": ["IT", "\u91d1\u878d\u4e1a", "\u623f\u5730\u4ea7", "\u5546\u4e1a\u670d\u52a1", "\u8fd0\u8f93/\u7269\u6d41", "\u751f\u4ea7", "\u653f\u5e9c", "\u6587\u5316\u4f20\u5a92"]}, {"fieldId": 6, "formType": "select", "isNull": 0, "name": "\u5ba2\u6237\u7ea7\u522b", "options": "A\uff08\u91cd\u70b9\u5ba2\u6237\uff09,B\uff08\u666e\u901a\u5ba2\u6237\uff09,C\uff08\u975e\u4f18\u5148\u5ba2\u6237\uff09", "isUnique": 0, "inputTips": "null", "type": 3, "value": "A\uff08\u91cd\u70b9\u5ba2\u6237\uff09", "fieldType": 0, "fieldName": "\u5ba2\u6237\u7ea7\u522b", "setting": ["A\uff08\u91cd\u70b9\u5ba2\u6237\uff09", "B\uff08\u666e\u901a\u5ba2\u6237\uff09", "C\uff08\u975e\u4f18\u5148\u5ba2\u6237\uff09"]}]}]]}

最后我们修改demo2的代码如下

# !/usr/bin/env python

# -*- coding: utf-8 -*-

# @Time : 2024/01

# @Author : Laopi

import json



import requests



from config import setting



from config.read_json_file import read_json_file

from config.setting import ConfigHandler

from demo.tokenCreate import get_token

from tools.yamlControl import GetYamlData



baseUrl = GetYamlData(ConfigHandler.config_path).get_yaml_data()



jsonpath = (ConfigHandler.root_path+'/testdata/demo2.json')

# 读取JSON文件



url = baseUrl['host']+read_json_file(jsonpath)['url']

print(url)

file_path = ConfigHandler.root_path+'/testdata/demo2.json'

print(file_path)

data = read_json_file(jsonpath)

data['headers']['Admin-Token']=get_token()

with open(file_path, 'w') as file:

json.dump(data, file)

data = read_json_file(jsonpath)

headers=data['headers']

reqdata =data['casedata'][0][0]

print(reqdata)

response1 = requests.post(url=url,headers=headers,data=json.dumps(reqdata))

print(response1.json())

运行结果如下:

文章来源:https://blog.csdn.net/pacermaker/article/details/135580506
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。