接口测试:excel/yaml读取、文件上传、定制化mark、skip跳过、模拟调用mock、Fixture环境初始化、Allure报告、Jenkins设置

在这里插入图片描述

目录标题

前言:创作不易,大佬求打赏

一、接口读取excel的读取、调用、问题

1、以表名,关键字读取excel方法

def get_excelData(sheetName,testName): # sheetName是页名,testName是关键字
    excel="./data/系统接口测试用例.xls"
    workBook = xlrd.open_workbook(excel,formatting_info=True)
    workSheet = workBook.sheet_by_name(sheetName)
    # print(workSheet.col_values(0))

    retnList=[]
    index=0
    for one in workSheet.col_values(0):
        if testName in one:
            retnNine=workSheet.cell_value(index, 9)
            retnEleven=workSheet.cell_value(index, 11)
            # print(type(retnNiht))
            # 将string格式的retnNine和retnEleven 转换为字典格式,已元祖的形式放入列表中
            retnList.append((json.loads(retnNine),json.loads(retnEleven)))
            					# 转换为字典格式
        index+=1
    return retnList

2、装饰器调用excel方法

class TestLogin:	# [({indata,respData}),({indata,respData})]
					#[ ( { 字典1,字典2 } ),( { 字典1,字典2 } ) ] 
					# [({下图中的请求参数,预期结果}),({下图中的请求参数,预期结果})]
    @pytest.mark.parametrize('indata,respData', get_excelData('登录模块', 'Login'))
    def test_login(self, indata, respData):
        url = "http://100.100.100.100:8082/account/login"

        resp = requests.post(url, params=indata).json() #返回json格式

        if resp["code"] == respData['code']:
            print("-----------")


if __name__ == '__main__':
    pytest.main(["test_abcd.py", "-s"])
  • 具体调用关系

在这里插入图片描述

3、请求参数的data中存在列表[ ]字段

在这里插入图片描述

id=2870
idMenu=4803
idShop=5336
specsJson=[{"specs":"默认","packing_fee":"0","price":"20"}]

specsJson是[ ],在excel中则像下面那么写即可

				将 [ ] 用 " " 框起来,里面用 ‘ ’ 框起来字段

“specsJson”:"[{‘specs’:‘默认’,‘packing_fee’:‘0’,‘price’:‘20’}]"

  • 例子的:excel的写法

{“name”:“葱香千层饼”,“id”:“2870”,“idShop”:“5336”,“category_id”:“4803”,“specsJson”:"[{‘specs’:‘默认’,‘packing_fee’:‘0’,‘price’:‘20’}]"}

二、yaml的使用

1、基本语法

安装yaml

pip install pyyaml 

①、读取

def host():
    file = open("../zTest0404/test.yaml", encoding="UTF-8")
    				   # yaml.读取(哪个文件,处理警告)
    result = yaml.load(file, Loader=yaml.FullLoader) 
    # print(result["host"])
    return result

if __name__ == '__main__':
    print(host())

②、yaml读取列表list

- name
- age
- data

③、list包含字典

-
  name: xiaoming
  age: 18
-
  data: 1000

④、字典里包含列表

hongxing:
  - 10
  - 20
  - 30
mingzhu:
  - 10
  - 20
  - 30

⑤、一个yaml读取多个数据

hongxing:
  - 10
  - 20
  - 30
---
mingzhu:
  - 10
  - 20
  - 30
def host2():
    file = open("../zTest0404/test.yaml", encoding="UTF-8")
    result = yaml.load_all(file, Loader=yaml.FullLoader) #处理警告
    # print(result["host"])
    for one in result:
        print(one)
        
if __name__ == '__main__':
    print(host2())
读取结果: 
{'hongxing': [10, 20, 30]}
{'mingzhu': [10, 20, 30]}

2、yaml格式测试用例的调用

yaml文件示例:

-
  url: /www/file  #url地址
  method: POST #方法:get、post
  detail: 账号密码正确 #详情
  headers: #请求头
  data:
    username: wgy001
    password: 123456 #请求体
  resp: #预期结果
    code: 20000
    msg: 成功

-
  url: /www/file  #url地址
  method: POST #方法:get、post
  detail: 账号正确,密码错误 #详情
  headers: #请求头
  data:
    username: wgy001
    password: qwertt #请求体
  resp: #预期结果
    code: 9999
    msg: 输入的密码错误

代码:

def host():
    file = open("../zTest0404/test.yaml",encoding="UTF-8")
    result = yaml.load(file, Loader=yaml.FullLoader) #处理警告
    resList=[]
    for one in result:
        resList.append((one['data'],one['resp']))
    return resList

if __name__ == '__main__':
    print(host())

返回值为:

[ ( { 字典1,字典2 } ),( { 字典1,字典2 } ) ]  
[({'username': 'wgy001', 'password': 123456}, {'code': 20000, 'msg': '成功'}), ({'username': 'wgy001', 'password': 'qwertt'}, {'code': 9999, 'msg': '输入的密码错误'})]

也就是调用时使用的格式:

class TestLogin:	# [{(indata,respData),(indata,respData)}]
					# [ 
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值