Unittest+Python 接口自动化测试如何进行 token 关联?
 作者:虫无涯
- 2023-02-21  陕西
- 本文字数:1552 字 - 阅读完需:约 5 分钟 
业务背景
有些业务在做接口自动化的时候,接口头需要传入 token 参数,那么如何做呢?下边是整理的内容,当然也借鉴了网友的一些资料。
1、先封装对 json 格式的数据存储,主要是用来保存和读取获取到的 token 值
operation_json.py
#coding:utf-8import jsonclass OperetionJson:
    def __init__(self,file_path=None):        if file_path  == None:            self.file_path = '../case/user.json' # 获取的token需要保存的地方        else:            self.file_path = file_path        self.data = self.read_data()
    #读取json文件    def read_data(self):        with open(self.file_path, 'r', encoding='utf-8') as fp:            data1 = fp.read()            if len(data1) > 0:                data = json.loads(data1)            else:                data = {}            return data
    #根据关键字获取数据    def get_data(self,id):        print(type(self.data))        return self.data[id]
    #写json    def write_data(self,data):        with open('../case/token.json','w') as fp:            fp.truncate()  # 先清空之前的数据,再写入,这样每次登录的token都是不一样的            fp.write(json.dumps(data))
if __name__ == '__main__':    opjson = OperetionJson()    #print(opjson.get_data('shop'))    data = {                "user":"zhang",                "passwd":123456            }    opjson.write_data(data)复制代码
 2、封装如何获取 token 脚本
get_token.py
import jsonimport requestsfrom common.operation_json import OperetionJson
class OperationHeader:
    def __init__(self, response):        self.response = json.loads(response)
    def get_response_token(self):        '''        获取登录返回的token        '''        token = {"data":{"token":self.response['data']['token']}}        #token = {"token": self.response['data']['token']}        return token    # 把数据写入文件    def write_token(self):        op_json = OperetionJson()        op_json.write_data(self.get_response_token())
    def get_response_msg(self):        reponse_msg = {"msg":self.response['msg']}        #print("reponse_msg:", reponse_msg)        return reponse_msg
if __name__ == '__main__':
  # 一个登录接口数据,仅供参考    url = "http://192.168.1.117/api/user/login"
    data = {                "username": "zhang",                "password": "123456",                "deviceId": 0            }
    res = requests.post(url,data).json()    res1 = json.dumps(res)    print(type(res1))    op = OperationHeader(res1)    print(op.get_response_msg())复制代码
 3、在用例管理里边进行调用
部分代码:test_interface.py
class Test_api(unittest.TestCase):    @classmethod    def setUpClass(cls):        # 登录获取token        cls.s = requests.session() # 创建会话        url = "http://192.168.1.102/api/user/login"        headers = {"content-type":"application/json","Connection":"keep-alive"}        data = {"username":"zhang","password":"123456","deviceId":0}        res = requests.post(url=url, json=data, headers=headers).json()        res1 = json.dumps(res)        #print(type(res1))        op = OperationHeader(res1)        op.write_token()
        writeexcel.copy_excel(testxlsx, reportxlsx) # 复制xlsx
复制代码
 划线
评论
复制
发布于: 刚刚阅读数: 5
版权声明: 本文为 InfoQ 作者【虫无涯】的原创文章。
原文链接:【http://xie.infoq.cn/article/0d62f1aaab7523e662e7add88】。文章转载请联系作者。


虫无涯
关注
专注测试领域各种技术研究、分享和交流~ 2019-12-11 加入
CSDN测试领域优质创作者 | CSDN博客专家 | 阿里云专家博主 | 华为云享专家 | 51CTO专家博主










 
    
评论