接口封装所遇到的坑--1

2021/12/7 6:16:49

本文主要是介绍接口封装所遇到的坑--1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.common文件下的读写yaml时候,拼接路径不要忘记"/",因为找不到路径可能会导致写入文件失败

import os
import yaml

#读取extract数据
def read_extract_yaml():
    with open(os.getcwd()+"/extract.yaml",'r+',encoding="utf-8")as f:
        value = yaml.load(f,Loader=yaml.FullLoader)
        return value
#写extract数据
def write_extract_yaml(data):
    with open(os.getcwd()+"/extract.yaml",'a',encoding="utf-8")as f:
        yaml.dump(data=data,stream=f,allow_unicode=True)
#写extract数据
def del_extract_yaml():
    with open(os.getcwd()+"/extract.yaml",'w',encoding="utf-8")as f:
        f.truncate()

#读取config.yaml
def read_config_yaml(one,two):
    with open(os.getcwd()+"/config.yaml","r+",encoding="utf-8") as f:
        value = yaml.load(stream=f,Loader=yaml.FullLoader)
        print(value)
        return value[one][two]
#获取对应的用例
def read_testcases_yaml(path):
    with open(os.getcwd()+path,"r+",encoding="utf-8") as f:
        value = yaml.load(stream=f,Loader=yaml.FullLoader)
        return value

2.搭建项目时候一定要注意python.ini文件,注意1.python不要写成pytest,2.python_files = test*.py,代表模块名,一定要指定到.py文件

[pytest]
addopts= -vs
python_files = test*.py
python_classes = Test*
python_functions = test*
testpaths = ./testcases
markers =
    smoke: 冒烟

3.清除数据的,mode为”w+“

#写extract数据
def del_extract_yaml():
    with open(os.getcwd()+"/extract.yaml",'w',encoding="utf-8")as f:
        f.truncate()

4.为了适配不同模块多路径,直接使用request的init方法

import requests
from common.yaml_util import read_config_yaml
class RequestUtil:
    #获取session对象,
    session = requests.session()
    def __init__(self,two):
        self.baseurl = read_config_yaml("base",two)
    def sendutil(self,method,url,**kwargs):
        method = str(method).lower()
        url = self.baseurl+url
        res = RequestUtil.session.request(method,url,**kwargs)
        return res

5.请求的路径,根据yaml文件的的配置,若是os.getcwd(),路径就不加”.“,直接/1111/1111.yaml,   如果没有加os.getcwd()就需要加上”.“,则  ./1111/1111.yaml

import pytest
from common.request_util import RequestUtil
from common.yaml_util import write_extract_yaml, read_extract_yaml, read_testcases_yaml
class Testproduct:
    @pytest.mark.parametrize("args",read_testcases_yaml("/testcases/testcase_product/test_get_token.yaml"))
    def test_get_token(self,args):
        url = args["request"]["url"]
        method = args["request"]["method"]
        datas = args["request"]["datas"]
        res = RequestUtil("pdrl").sendutil(url=url,method=method,params=datas)
        write_extract_yaml({'access_token':res.json()['access_token']})

    @pytest.mark.parametrize("args", read_testcases_yaml("/testcases/testcase_product/test_get_flags.yaml"))
    def test_get_flags(self,args):
        access_token = read_extract_yaml()["access_token"]
        print(access_token)
        url = args["request"]["url"]+access_token
        res = RequestUtil("pdrl").sendutil(url=url, method="get")
        print(res.json())

 

6,获取config.yaml其实可以和testcases.yaml 都放在接口参数化,这个时候不用去request的init方法(我未验证只是思路)

 

  每篇一句:

  睡觉

 



这篇关于接口封装所遇到的坑--1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程