pytest之yaml格式测试用例读写封装

pytest之yaml格式测试用例读写封装

pytest之parametrize()实现数据驱动

@pytest.mark.parametrize("参数名",列表数据)

参数名:用来接收每一项数据,并作为测试用例的参数。

列表数据:一组测试数据。

python 复制代码
import pytest

@pytest.mark.parametrize("参数名", [列表数据])
def test_example(参数名):
    # 在这里编写测试用例,可以使用参数名作为测试数据
    pass
  • 第一种用法:列表
python 复制代码
class TestApi(CommonUtil):
    @pytest.mark.parametrize('caseinfo', ['百里','汤姆','一瑶'])
    def test_01_get_token(self,caseinfo):
        print("获取统一接口鉴权码"+caseinfo)
  • 第二种用法:字典列表
python 复制代码
class TestApi(CommonUtil):

    @pytest.mark.parametrize('arg1,arg2', [['name', '百里'], ['age', '18']])
    def test_01_get_token(self, arg1, arg2):
        print("获取统一接口鉴权码" + str(arg1)+ " "+ str(arg2))

YAML格式测试用例读/写/清除/封装

结构类型

python 复制代码
# 读取yaml
import os

import yaml


# 获取项目的根目录
def get_obj_path():
    # 获取当前文件的目录路径,并使用字符串 'common_01' 进行分割,返回分割后的第一个部分,即 'common_01' 之前的路径。
    return os.path.dirname(__file__).split('common_01')[0]


def read_yaml(yaml_path):
    # 使用 with 语句打开文件,并使用模式 'r' 以只读方式打开文件
    with open(get_obj_path()+yaml_path, mode='r', encoding='utf-8') as f:
        # 使用 yaml 模块的 load 函数从文件中读取数据,并使用 FullLoader 加载器
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        # 返回从文件中读取的数据
        return value


if __name__ == '__main__':
    print(read_yaml('testcases/user_manage/get_token.yaml'))

Maps类型

Map为字典,一对key:value键值对

键:(空格)值。

python 复制代码
# 示例 YAML 文件
person:
  name: John
  age: 30
  city: New York

数组类型

使用'-'表示列表。

python 复制代码
# Example YAML file: get_token.yaml
tokens:
  - token1
  - token2
  - token3

pytest+parametrize+yaml

test_api.py

python 复制代码
import pytest
import requests

from common_01.common_util import CommonUtil
from common_01.yaml_util import read_yaml


class TestApi(CommonUtil):
    session = requests.session()

    # 定义一个参数化测试函数
    @pytest.mark.parametrize('caseinfo', read_yaml('testcases/user_manage/get_token.yaml'))
    def test_01_get_token(self, caseinfo):
        # 打印测试用例信息
        print("获取统一接口鉴权码: ")
        print(caseinfo)
        # 打印请求信息
        name = caseinfo['name']
        method = caseinfo['request']['method']
        url = caseinfo['request']['url']
        data = caseinfo['request']['data']
        print(caseinfo['validate'])
        # 发送 HTTP 请求
        res = self.session.request(method=method, url=url, params=data)
        # 打印返回结果
        print(res.json())
        #断言
        result =res.json()
        assert 'access_token' in result

get_token.yaml

python 复制代码
-
  name: 获取统一的接口鉴权
  request:
    method: get
    url: https://api.weixin.qq.com/cgi-bin/token
    data:
      grant_type: client_credential
      appid: wx6b11b3efd1cdc290
      secret: 106a9c6157c4db5f6029918738f9529d
  validate: None
相关推荐
桌面运维家1 小时前
服务器进程异常监控:快速定位与排障实战指南
运维·服务器
@CLoudbays_Martin111 小时前
UniApp是否能够接入SDK游戏盾呢?
服务器·网络·网络协议·tcp/ip·安全
郝亚军2 小时前
ubuntu 22.04如何安装libmodbus
运维·服务器·ubuntu
李日灐2 小时前
< 6 > Linux 自动化构建工具:makefile 详解 + 进度条实战小项目
linux·运维·服务器·后端·自动化·进度条·makefile
计算机安禾2 小时前
【Linux从入门到精通】第34篇:搭建FTP与Samba——跨平台文件共享解决方案
linux·运维·服务器
乌恩大侠2 小时前
【AI-RAN】在空ubuntu服务器安装环境和生成TV,高达430G文件
服务器·人工智能·ubuntu·fpga开发·o-ru
日取其半万世不竭3 小时前
用 Netdata 实时监控服务器,比 Prometheus + Grafana 轻量得多
linux·服务器·网络·系统架构·负载均衡·zabbix·grafana
JiaWen技术圈3 小时前
内核子系统 nf_tables 深度解析
linux·服务器·安全·运维开发
计算机安禾3 小时前
【Linux从入门到精通】第32篇:Nginx入门——高性能Web服务器搭建
linux·服务器·nginx
ZenosDoron3 小时前
Linux 中,rm -r 和 -f
linux·运维·服务器