从0开始python学习-44.通过yaml实现数据代码分离

目的:将数据和代码进行分离,便于批量执行多个用例,且修改数据上方便

问题:所有的请求和请求参数的写在了代码中,不利于批量测试多个用例,一旦有修改工作量大

原代码:

python 复制代码
class TestApi:
    # get请求
    def test_phpwind(self):
        url = 'http://aaa/phpwind/'
        res = RequestUtil().send_request(method="get",url=url)

        res_token = re.search('name="csrf_token" value="(.*?)"', res.text)
        data = {"csrf_token":res_token.group(1)}
        writer_yaml(data)
        
    # post请求
    def test_phpwind_login(self):
        url = 'http://aaa/phpwind/index.php?m=u&c=login&a=dorun'
        header = {
            "Accept": "application/json, text/javascript, /; q=0.01"
            "X-Requested-With": "XMLHttpRequest"
        }
        data = {
            "username": "aaa",
            "password": "aaa",
            "csrf_token": read_yaml('csrf_token')
            "backurl": "http://aaa/phpwind/",
            "invite": ""
        }
        
        res = RequestUtil().send_request(method="post",url=url, data=data, headers=header)
        print(res.json())
        
    # 文件上传。需要获取上面的token
    def test_file_upload(self):
        url = 'https://api.weixin.qq.com/cgi-bin/media/uploadimg'
        params = {
            'access_token':read_yaml('csrf_token')
        }
    
        file = {
            'media':'D:\\1.jpg'
            }
        res = RequestUtil().send_request(method="post", url=url, params=params, files=file)
        print(res.json())

修改后:

  1. 在原有读取yaml文件代码基础上,新增一个读取所有用例的方法,注意这里传入的yaml_path不能写死,需要根据不同用例不同的传参

  2. 根据每个用例的需求分别写一个对应的yaml文件(这样的弊端是会有很多yaml文件,后期需要优化冗余代码)

  3. yaml文件是无法调用方法的,所有当需要调用token等参数的时候,在用例中调用,此时只需要将yaml中的该字段置位''即可

1. 新增读取所有用例的方法

python 复制代码
def read_testcase(yaml_path): 
    with open(yaml_path,encoding="utf-8") as f:
        value = yaml.safe_load(f)
        return value

2. 根据用例数新增对应的yaml文件--根据需求写

2.1 对应test_phpwind用例的yaml文件--test_phpwind.yaml

python 复制代码
-
  request:
    method: get
    url: http://aaa/phpwind/

2.2 对应test_phpwind_login用例的yaml文件--test_phpwind_login.yaml

python 复制代码
-
  request:
    method: post
    url: http://aaa/phpwind/index.php?m=u&c=login&a=dorun
    header:
      Accept: "application/json, text/javascript, /; q=0.01"
      X-Requested-With: "XMLHttpRequest"
  data:
    username: aaa
    password: aaa
    csrf_token: ''
    backurl: "http://aaa/phpwind/"
    invite: ""

2.3 对应test_file_upload用例的yaml文件--test_file_upload.yaml

python 复制代码
-
  request:
    method: post
    url: https://api.weixin.qq.com/cgi-bin/media/uploadimg
    params:
      access_token: ''
    file:
      media: 'D:\\1.jpg'

3. 修改后的用例

python 复制代码
class TestApi:
    @pytest.mark.parametrize('caseinfo',read_testcase('./test_study/test_pytest/test_phpwind.yaml'))
    def test_phpwind(self,caseinfo):
        method = caseinfo['request']['method']
        url = caseinfo['request']['url']
        res = RequestUtil().send_request(method=method,url=url)
        
        res_token = re.search('name="csrf_token" value="(.*?)"', res.text)
        data = {"csrf_token":res_token.group(1)}
       writer_yaml(data)
       
    @pytest.mark.parametrize('caseinfo', read_testcase('./test_study/test_pytest/test_phpwind_login.yaml'))
    def test_phpwind_login(self,caseinfo):
        method = caseinfo['request']['method']
        url = caseinfo['request']['url']
        header = caseinfo['request']['header']
        data = caseinfo['data']
        # 这里通过读取yaml的方式重新将token赋值使用
        data['csrf_token'] = read_yaml('csrf_token')
        res = RequestUtil().send_request(method=method,url=url, data=data, headers=header)
        print(res.json())
        
    @pytest.mark.parametrize('caseinfo', read_testcase('./test_study/test_pytest/test_file_upload.yaml'))
    def test_file_upload(self,caseinfo):
        method = caseinfo['request']['method']
        url = caseinfo['request']['url']
        params = caseinfo['request']['params']
        params['access_token'] = read_yaml('access_token')
        # 通过yaml的方式传入文件
        file = caseinfo['request']['file']
        res = RequestUtil().send_request(method=method, url=url, params=params, files=file)
        print(res.json())
相关推荐
JJJJ_iii14 小时前
【左程云算法09】栈的入门题目-最小栈
java·开发语言·数据结构·算法·时间复杂度
枫叶丹414 小时前
【Qt开发】显示类控件(三)-> QProgressBar
开发语言·qt
三体世界14 小时前
测试用例全解析:从入门到精通(1)
linux·c语言·c++·python·功能测试·测试用例·测试覆盖率
Python私教14 小时前
Django全栈班v1.04 Python基础语法 20250912 下午
后端·python·django
Bear on Toilet14 小时前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
xchenhao14 小时前
Scikit-Learn 对糖尿病数据集(回归任务)进行全面分析
python·机器学习·回归·数据集·scikit-learn·特征·svm
xchenhao14 小时前
Scikit-learn 对加州房价数据集(回归任务)进行全面分析
python·决策树·机器学习·回归·数据集·scikit-learn·knn
这里有鱼汤14 小时前
发现一个高性能回测框架,Python + Rust,比 backtrader 快 250 倍?小团队必备!
后端·python
☼←安于亥时→❦14 小时前
数据分析之Pandas入门小结
python·pandas
程序员东岸14 小时前
C语言入门指南:字符函数和字符串函数
c语言·笔记·学习·程序人生·算法