使用yaml进行数据驱动

一、需求描述

1、请求登陆接口,从登陆接口的响应头数据中获取token值,并写入yml文件;

2、读取写入yml文件中的token值作为下个接口的传参,请求查询物料列表接口,查看查询结果。

yaml_util.py

import os
import yaml


class YamlUtil:
    # 拼接文件路径
    filename = os.getcwd() + '\\extract_yml'def read_extract_yml(self, key):
        """读取extract_yml文件"""
        with open(YamlUtil.filename, 'r', encoding='utf-8') as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value[key]

    def write_extract_yml(self, data):
        """写入extract_yml文件"""
        with open(YamlUtil.filename, 'w', encoding='utf-8') as f:
            yaml.dump(data=data, stream=f, allow_unicode=True)

    def clear_extract_yml(self):
        """清空extract_yml文件"""
        with open(YamlUtil.filename, 'w', encoding='utf-8') as f:
            f.truncate()
test_request.py

import requests
import unittest
from util.yaml_util import YamlUtil


class TestCase(unittest.TestCase):
    def test_user_login(self):
        """用户登录"""
        url = "xxx/api-system/sys/user/wjjGmOpenToken/login"
        data = {"userName": "superadmin", "password": "123456"}
        res = requests.post(url, json=data)
        YamlUtil().write_extract_yml({'token': res.headers['token']})

    def test_query_material_list(self):
        """查询物料列表"""
        url = "xxx/api-goods/goodsInfo/goods/list/select"
        token = YamlUtil().read_extract_yml('token')
        data = {"pageNo": 1, "pageSize": 10, "goodsMgtAttr": {}}
        headers = {"token": token}
        res = requests.post(url, json=data, headers=headers)
        print(res.json())


if __name__ == '__main__':
    unittest.main()

二、执行结果

 

posted @ 2022-08-14 17:37  淘气的包子  阅读(309)  评论(0)    收藏  举报