接口关联封装(pytest+requests+yaml)结合allure生成测试报告

2 篇文章 0 订阅
1 篇文章 0 订阅

目前调用自己的两个接口,分别获取token(get)及获取用户信息(post)

新建common的package:存放用例数据文件或者共享配置的文件

新建testcase.py:存放用例

新建temp目录:存放allure生成的临时json

新建report目录:存放生成的报告

新建conftest.py:全局配置,部分用例的前后置等

新建all.py:main()方法单独存放

1.请求方式封装:

common下新建request_until.py文件

import pytest
import requests
class RequestUntil():
    def send_request(self,method,url,data,**kwargs):
        method = str(method).lower()
        rep=""
        if method == 'get':
            rep = requests.request(method,url=url,params=data,**kwargs)
        else:
            rep = requests.request(method,url=url,data=data,**kwargs)
        return rep.json()

2.yaml做数据驱动

   a.common下新建yaml_until.py,负责将token写入一个yml文件,方便其他接口使用,以及负责        测试用例数据的读取 :

import os
import yaml
class YmalUntil():
    #写入extract.yml文件
    def write_token_extract(self,data):
        print(self.__class__)
        print("=======================lxj=================================")
        print(os.getcwd())
        print(data)
        with open(os.getcwd()+"\extract.yml",mode="a",encoding="utf-8") as f:
            value=yaml.dump(data=data,stream=f,allow_unicode=True)
            return value
    #读取extract.yml文件
    def read_token_extract(self):
       with open(os.getcwd()+"\extract.yml",mode="r",encoding="utf-8") as f:
            value=yaml.load(stream=f,Loader=yaml.FullLoader)
            return value

    #清除extract.yml
    def clean_token_extract():
        with open(os.getcwd() + "\extract.yml", mode="w", encoding="utf-8") as f:
            f.truncate()
    #读取测试测试用例的yml文件
    def read_testcase_yml(self,yaml_name):
        with open(os.getcwd() + "\Testcase\\"+yaml_name, mode="r", encoding="utf-8") as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value

   b.新建对应的用例的yml文件存放数据

4. 测试用例的编写调用公共的方法:

 get_token方法:

   a.引入yml文件做数据驱动:

b.调用封装的请求

c.获取到的token通过调用YamlUntil入extract文件

 

 d.get_user接口需要用到token,调用YamlUntil读取yaml

e.conftest.py调用 YamlUntil清除extract文件中生成的历史token

 完整代码如下:

import json

import pytest
import requests
from common.yaml_until import YmalUntil
from common.request_until import RequestUntil

class TestLogin():
    #mm = YmalUntil();
    @pytest.mark.parametrize('caseinfo',  YmalUntil().read_testcase_yml("get_token.yml"))
    def test_get_token(self,caseinfo,conn_database):
        # print(caseinfo['name'])
        # print(caseinfo['request']['method'])
        # print(caseinfo['request']['url'])
        # print(caseinfo['request']['data'])
        # url="http://192.168.50.150:8888/zaj/first/login"
        # data={
        #     "userId":"lxj",
        #     "password":"123"
        # }
        method=caseinfo['request']['method']
        url=caseinfo['request']['url']
        data=caseinfo['request']['data']
        RequestUntil().send_request(method,url,data)
        rep=requests.request(method,url=url,params=data)
        print(rep.json()['data']['token'])
        print(rep.json())
        if  rep.json()['msg']=="SUCCESS" and rep.json()['code']==0 :
            assert rep.json()['data']['token']

            YmalUntil().write_token_extract({'token':rep.json()['data']['token']})
    @pytest.mark.parametrize('caseinfo', YmalUntil().read_testcase_yml("get_user.yml"))
    def test_get_user(self,caseinfo):
        print(caseinfo)
        method = caseinfo['request']['method']
        url = caseinfo['request']['url']
        data = caseinfo['request']['data']
        # url="http://192.168.50.150:8888/zaj/first/getUser"
        # data={
        #     "userName":"lxj"
        # }
        header={"token":YmalUntil().read_token_extract()['token']}
        RequestUntil().send_request(method, url, data)
        rep= requests.request(method,url=url,json=data,headers=header)
        assert rep.json()['msg'] == "SUCCESS" and rep.json()['code'] == 0 and rep.json()['data']['userName']=="lxj"

执行结果如下:

5.allure生成测试报告

新建pytest.ini文件 对文件及用例进行配置 

代码如下:

[pytest]
addopts = -vs -m "smoke" -p no:warnings --alluredir ./temp
testpaths = ./Testcase
python_files = test_*.py
pytest_class = Test*
pytest_function=test_*
markers=
    smoke:maoyan

--alluredir ./temp将生成的json放到临时目录下
执行完成后结果如下:

 all.py新增

os.system("allure generate ./temp -o ./reports --clean")将temp目录下的文件转换成测试报告存放于reports目录下

执行完成后结果如下:

题外:

markers=
    smoke:maoyan 标记环境为冒烟环境,在用例里添加对应的标记,则执行其对应的用例

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值