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
相关推荐
坤昱29 分钟前
cfs调度类深入解刨——最新内核细节分析2
linux·服务器·cfs·cfs调度·eevdf调度·eevdf·kernel 7.1
艾莉丝努力练剑36 分钟前
【Linux:文件】Ext系列文件系统进阶
linux·运维·服务器·c++·文件系统·文件io·ext
海市公约37 分钟前
Linux核心基础命令与权限管理实战指南
linux·运维·服务器·vim·权限管理·系统监控·命令行
mixboot2 小时前
Linux 进程工作目录查看利器:pwdx 命令详解
linux·运维·服务器
之歆3 小时前
Day16_JavaScript 轮播图与事件工程实战(下篇)
服务器·开发语言·前端·javascript·网络·性能优化
zhz52145 小时前
服务器等保加固实施报告
运维·服务器·信创·国密·等保
s_w.h6 小时前
【 linux 】文件系统
linux·运维·服务器·算法·bash
程序员小远7 小时前
系统性能指标全解析
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
早睡身体真不戳7 小时前
【无标题】
java·服务器·windows
小快说网安8 小时前
云服务器抗 DDoS 只靠基础防护够吗?
运维·服务器·ddos