pytest+parametrize+yaml实例

复制代码
# 一、yaml格式
#
# yaml是一种数据类型,可以和json之间灵活的切换,支持注释、换行、字符串等。可以用于配置文件或编写测试用例。
#
# 数据结构:一般是键值对的方式出现。注意编写时值前面必须有空格,键:(空格)值。
#
# 如果是数组,以-(空格) 开头来表示
复制代码
# 二、定义读取yaml测试用例的方法

# Configuration.yaml_util.py

yaml文件的读、写、删除

import os.path
import yaml

# 读取yaml文件
# def read_yml(ymlPath):
#     if not os.path.isfile(ymlPath):
#         raise FileNotFoundError("文件路径不存在,请输入正确的路径")
#     f = open(ymlPath, 'r', encoding="utf-8")
#     cfg = f.read()
#     d = yaml.safe_load(cfg)
#     return d

def read_yaml(path):
    """
    读取yaml文件
    :param path: 要读取的yaml文件路径
    :return: 返回yaml内容
    """
    with open(path, encoding="utf-8") as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        # return value[key]
        return value


def write_yaml(path, data):
    """
    写入yaml文件
    :param path:
    :param data:
    :return:
    """
    with open(path, encoding="utf-8", mode='a') as f:
        yaml.dump(data, stream=f, allow_unicode=True)


def clear_yaml(path):
    """
    清空yaml文件,一般在整个项目执行之前,清空
    :return:
    """
    with open(path, encoding="utf-8", mode="w") as f:
        f.truncate()

# Configuration.config.yaml

用于存储环境地址

复制代码
# 三、把测试的服务器地址作为配置文件
#
# 测试的服务器,正常来讲都是比较固定的,但测试环境和正式环境是不同的,因此我们将服务器地址抽出来作为配置文件,方便切换不同的测试环境。这里将配置文件放在config.yaml文件中。
Host:
  # 有些系统会有生产环境、小版本环境、测试环境等等,所以这里统一管理这些环境地址,后面使用的时候来这里调用就行了
  baseurl: "http://192.168.0.103/"

#datas.user.yaml

用来存储用例

# 不同用例之间用-分隔
-
  batteryBrandName: 12,
  batteryRatedCapacity: 12,
  batteryRatedVoltage: 12,
  batterySpceC: 12,
  batterySpceG: 12,
  batterySpceK: 12,
  batteryType: 12,
  batteryTypeName: 12,
  batteryWeight: 12,
  cellCount: 12
-
  batteryBrandName: 13,
  batteryRatedCapacity: 13,
  batteryRatedVoltage: 13,
  batterySpceC: 13,
  batterySpceG: 13,
  batterySpceK: 13,
  batteryType: 13,
  batteryTypeName: 13,
  batteryWeight: 13,
  cellCount: 13

#Module.AddBatterTypeInfo.py

复制代码
# 四、执行测试的用例文件
# 这里结合@pytest.mark.parametrize方法实现用例数据驱动。
import pytest
import requests
from Configuration.Logins import login
from Configuration.Logins import host
# from Configuration.yaml_util import read_yaml
from Configuration.yaml_util import *


@pytest.mark.parametrize("batteryBrandName, batteryRatedCapacity, batteryRatedVoltage, batterySpceC,batterySpceG, batterySpceK, batteryType, batteryTypeName, batteryWeight, cellCount",
                         read_yaml('../datas/user.yaml'))
def test_add_batter_type_info(batteryBrandName, batteryRatedCapacity, batteryRatedVoltage, batterySpceC,
                         batterySpceG, batterySpceK, batteryType, batteryTypeName, batteryWeight, cellCount):
    API_url = "be/cloud/bsm/batterytypeinfo"
    baseurl = read_yaml('../Configuration/config.yaml')['Host']['baseurl']
    FULL_URL = baseurl + API_url
    # FULL_URL = host() + API_url

    header = {
        'Authorization': login()
    }

    body = {
        "batteryBrandName": batteryBrandName,
        "batteryRatedCapacity": batteryRatedCapacity,
        "batteryRatedVoltage": batteryRatedVoltage,
        "batterySpceC": batterySpceC,
        "batterySpceG": batterySpceG,
        "batterySpceK": batterySpceK,
        "batteryType": batteryType,
        "batteryTypeName": batteryTypeName,
        "batteryWeight": batteryWeight,
        "cellCount": cellCount
    }
    r = requests.post(FULL_URL, headers=header, json=body)
    res = r.json()
    # 上面的两句也可以合成一句: res = requests.post(FULL_URL, headers=header, json=body).json()
    print(res)
    return res

    # 用于仅执行该文件


if __name__ == '__main__':
    # test_add_batter_type_info()
    pytest.main(["-s"])

#Module.BatterTpyeInfo_Search.py

import pytest
import requests
from Configuration.Logins import login
from Configuration.Logins import host


# 方法必须是test_开头,因为装饰器parametrize是属于pytest框架的
# 在方法中传入这几个参数名信息
# 格式:@pytest.mark.parametrize("参数1,参数2,参数3,参数4",[(用例1)(用例2)(用例3)])    每个用例中有四个参数
@pytest.mark.parametrize("batteryBrandName,batteryTypeName,batteryType,delFlag",
                         [("乐圆技术", "LY6050", "", ""), (1, 3, 3, "在用")])
def test_battery_type_info_search(batteryBrandName, batteryTypeName, batteryType, delFlag):
    API_URL = "be/cloud/bsm/batterytypeinfo/list"
    # 拼接URL
    FULL_URL = host() + API_URL
    # print("实际URL的值为:",FULL_URL)
    header = {'Authorization': login()}
    # print("实际URL的值为",header)

    # 执行用例,不要每条用例写一遍了,直接引用参数名即可
    par = {
        "batteryBrandName": batteryBrandName,
        "batteryTypeName": batteryTypeName,
        "batteryType": batteryType,
        "delFlag": delFlag
    }
    # get请求下,参数需要使用【params】来设置
    # post请求中,是使用data或者json来传递的
    r = requests.get(FULL_URL, headers=header, params=par).json()
    print("\n", r)
    return r


if __name__ == '__main__':
    test_battery_type_info_search()

# run.run_case.py

调用执行所以模块的所有测试用例

import pytest
from Module.AddBatterTypeInfo import test_add_batter_type_info
from Module.BatteryTypeInfo_Search import test_battery_type_info_search
from Module.battery_type_info_exportSelect import battery_type_info_export


# def add_batter_type_info():
#     res1 = test_add_batter_type_info()
#
#
# def battery_type_info_search():
#     res2 = test_battery_type_info_search()

#
# def battery_type_info_export():
#     res3 = battery_type_info_export()


if __name__ == '__main__':
    pytest.main(["run_case.py", "-s"])
相关推荐
VX_DZbishe1 小时前
springboot旅游管理系统-计算机毕业设计源码16021
java·spring boot·python·servlet·django·flask·php
青春之我_XP1 小时前
【知识图谱系列】Neo4j使用Py2neo与python进行链接
python·知识图谱·neo4j
IT数据小能手1 小时前
Python中爬虫编程的常见问题及解决方案
开发语言·爬虫·python
橙子味冰可乐1 小时前
isprintable()方法——判断字符是否为可打印字符
java·前端·javascript·数据库·python
Hi202402171 小时前
将数据切分成N份,采用NCCL异步通信,让all_gather+matmul尽量Overlap
pytorch·python·性能优化·分布式训练·nccl·融合算子
凉拌糖醋鱼4 小时前
Python-PDF文件密码破解小工具
开发语言·python·pdf·密码破解
IT数据小能手4 小时前
天猫商品列表数据接口(Tmall.item_search)
大数据·爬虫·python
逢生博客6 小时前
Transformers 安装与基本使用
pytorch·python·语言模型·tensorflow·transformer
阿米诺s6 小时前
python本学期所有代码!
开发语言·爬虫·python
蔡斯达纳6 小时前
Pycharm导入内置库或者第三方库时标红,no module named ‘xxx‘
ide·python·pycharm·bug