pytest+yaml+allure框架接口自动化测试教程

本文档详细介绍了如何结合pytest、yaml和allure框架进行接口自动化测试。内容涵盖pytest配置、接口请求封装、yaml用例编写、数据读取、用例执行、参数化测试以及前后置用例的实现,旨在提供一个完整的接口自动化测试解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

框架简介:使用pytest库,去运行测试用例,用yaml做数据驱动,allure生成测试报告。

测试报告效果
在这里插入图片描述
在这里插入图片描述

项目目录结构
在这里插入图片描述

一、pytest.ini文件
pytest的配置文件,用来配置运行规则和运行命令。
addopts 为运行命令,命令之间用空格隔开.。-vs:表示显示详细调试信息;./temps 为allure测试报告生成目录 ;–clean 为清除历史报告。
testpaths 填写测试用例的目录。
python_files、python_classes、python_functions分别定义了文件名、类名、函数名的规则,只有满足规则才会被执行。

[pytest]
addopts = -vs --alluredir ./temps --clean-alluredir
testpaths = ./test_cases
python_files = test_*.py
python_classes = Test*
python_functions = test_*

二、封装基本接口请求方法
封装GET和POST请求,根据method去执行并返回res。

import requests
import json


class RequestMethod:
    session = requests.session()  # 会话

    def all_send_request(self, method, url, data, headers, **kwargs):
        method = str(method).lower()
        if method == "get":
            res = RequestMethod.session.request(method=method, url=url, params=data, **kwargs)
        elif method == "post":
            str_data = json.dumps(data
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值