yaml格式配置文件和测试用例,相对路径转绝对路径,allure报告优化

yaml文件格式要求

1.区分大小写

2.使用缩进表示层级

3.不能使用tab键,只能用空格

4.同级元素左对齐

5.使用#注释

yaml文件是内容,列表和字典及其复合,---表示独立文件---,&和*表示引用

# 注释
# 1- 字典格式 键值对
#name: th0004
#password: 11449
#2-列表
#只要非数字都识别为字符串
#- 100
#-
#  - 1
#  - 2
#  - 3
#3 混合类型
#字典套字典
#a: tom
#b:
#  c: 10
#  d: 20
#name: tom
#info:
#  - 10
#  - 20
#  - 30
#复合元素

#4- 引号用法,双引号识别转义字符串,单引号原样输出
#a: hello\nworld
#b: 'hello\nworld'
#d: "hello\nWorld"

#5-引用变量
---
info: &liu xintian
data:
  - *liu
  - sq
  - 200
---
name: liu
age: 28

操作yaml文件

yamlControl.py

yaml强制转换,!!

str: !!str 3.14

int !!int  "123"

yaml套用yaml,写继承类------------------------------------

# -*- coding: utf-8 -*-
# 2022/2/19
# LiuRenHong

import yaml
#yaml套用yaml
def get_yaml_data(fileDir):
    #1在内存中加载yaml文件
    fo = open(fileDir,'r',encoding='utf-8')
    #2调用yaml读取
    res = yaml.load(fo,Loader=yaml.FullLoader)
    return res

def get_yamls_data(fileDir):
    resList = []
    #1在内存中加载yaml文件
    fo = open(fileDir,'r',encoding='utf-8')
    #2调用yaml读取
    res = yaml.load_all(fo,Loader=yaml.FullLoader)
    for one in res:
        resList.append(one)
    return resList
def get_yaml_case_data(fileDir):
    #1在内存中加载yaml文件
    resList = []
    fo = open(fileDir,'r',encoding='utf-8')
    #2调用yaml读取
    res = yaml.load(fo,Loader=yaml.FullLoader)
    baseInfo = res[0]#基本信息
    del res[0]
    for one in res:
        # print(one)
        resList.append((one['data'],one['resp']))
    return resList

#------------------------
"""
注意事项
1-尽量少改代码
2-知道pytest需要什么数据
[(请求数据1,期望响应数据1),(请求数据2,期望响应数据2)]
"""

if __name__ == '__main__':
    res = get_yaml_case_data('../data/data.yaml')
    print(res)
    # for a,b in res.items():
    #     print(a,b)

 相对路径转为绝对路径

config.py

# -*- coding: utf-8 -*-
# 2022/2/12
# LiuRenHong
# config.py

HOST = "http://121.41.14.39:8082"
#相对路径有可能找不到文件的问题----->把相对路径转为绝对路径
import os #os.path相当于python解释权的环境变量path
project_path = os.path.split(os.path.realpath(__file__))[0].split('configs')[0]
print(__file__)
print(os.path.realpath(__file__))
print(project_path+'data')

 allure报告优化

test_shop.py

#-*- coding: utf-8 -*-
#Date:2021/3/31
from libs.login import Login
from libs.shop import Shop
import pytest,os
from tools.excelControl import get_excel_data
import allure# pip install allure-pytest
#封装测试类

#多个fixture在一起,离测试的方法近先执行!
@pytest.mark.usefixtures('sq')#没有返回值的fixture--sq1
@pytest.mark.usefixtures('sq2')#没有返回值的fixture--sq2
@pytest.mark.shop#标签-1
@allure.epic("订餐系统")#工程级别
@allure.feature("店铺模块")
class TestShop:
    #----------------------使用fixture----------------------------------
    # def setup_class(self):#初始化  setup_class  类级别:这个类开始的时候只运行一次
    #     """
    #     这个初始化是干什么?
    #         1- 获取token
    #         2- 获取headers
    #         3- 登录操作
    #         4- 创建一个店铺实例---更适合
    #     :return:
    #     """
    #     self.token = Login().login({"username":"sq0777","password":"xintian"},getToken=True)
    #     self.shopObject = Shop(self.token)
    #     print('-----初始化操作-----')
    #
    # def teardown_class(self):
    #     print('-----数据清除操作-----')

    #-------------------------------------------------
    #1- 列出店铺的测试方法
    @pytest.mark.shop_list  # 标签-2
    @pytest.mark.parametrize('caseTitle,inBody,expData',get_excel_data('../data/Delivery_System_V1.5.xls',
                                                                       '我的商铺', 'listshopping','标题','请求参数','响应预期结果'))
    @allure.story("列出店铺")
    @allure.title("{caseTitle}")
    def test_shop_list(self,caseTitle,inBody,expData,shop_init):
        #调用列出接口
        res = shop_init.shop_list(inBody)
        if 'code' in res:
            assert res['code'] == expData['code']
        else:
            assert res['msg'] == expData['msg']

    # 2- 编辑店铺的测试方法
    @pytest.mark.shop_update#标签-3
    @pytest.mark.parametrize('caseTitle,inBody,expData',get_excel_data('../data/Delivery_System_V1.5.xls', '我的商铺', 'updateshopping', '标题','请求参数',
                                            '响应预期结果'))
    @allure.story("编辑店铺")
    @allure.title("{caseTitle}")#使用那个变量
    def test_shop_update(self,caseTitle, inBody, expData, update_shop_init):
        # 调用更新接口
        res = update_shop_init[0].shop_update(inBody,update_shop_init[1],update_shop_init[2])
        assert res['code'] == expData['code']




if __name__ == '__main__':
    pytest.main(['test_shop.py','-s','--alluredir','../report/tmp'])#  -s  显示打印
    os.system('allure serve ../report/tmp')

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值