python自动化之pytest框架以及数据驱动(第五天)

1.pytest框架需要遵循的规则

(1).py 测试文件必须以test 开头(或者以 test结尾)

(2)测试类必须以Test开头,并且不能有 init 方法

(3)测试方法必须以test 开头

(4)断言必须使用 assert

2.pytest数据驱动

在pytest中,数据驱动测试通常使用pytest.mark.parametrize标记实现。这个标记允许你向测试函数提供多组输入参数和预期结果,从而实现对一组数据进行测试的目的。

以下是一个简单的例子:

python 复制代码
import pytest

@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 54)])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

这个测试test_eval有两个参数test_inputexpected,使用 pytest.mark.parametrize 标记,我们给这两个参数提供了三组值。test_input 是要被 eval 函数执行的表达式,expected 是预期的计算结果。这样,这个测试实际上会运行三次,每次运行都使用一组不同的参数。

eval()是Python的一个内置函数,它可以将一个字符串当作Python代码来执行,并返回执行结果。这意味着你可以用它来动态地运行你创建的或者用户输入的Python代码。

例如,你可以像这样使用eval()

python 复制代码
result = eval("3 + 4")
print(result) # 输出:7

运行上述测试,Pytest会对每一组参数执行一次测试函数,这样就能够很方便地进行数据驱动的测试。

3.登录接口

新建test_login模块(test_case包中创建)

test_login.py代码

python 复制代码
# -*- coding: utf-8 -*-
# @File : test_login.py
# @Time : 2024/3/7 12:51
# @Author : syq
# @Email : 1721169065@qq.com
# @Software: PyCharm
import pytest
from lib.apiLib.login import Login
from tools.excelControl import get_data_excel
class Test_Login:
    @pytest.mark.parametrize("respData,resExcept",get_data_excel('../data/外卖系统接口测试用例-V1.5.xls','登录模块','Login'))
    def test_login(self,respData,resExcept):
        login=Login()
        resReal=login.login(respData)
        assert resReal['code']==resExcept['code']
if __name__ == '__main__':
    pytest.main(['test_login.py','-s'])

结果:

4.商铺接口

新建test_shop模块(test_case包中创建)

代码如下:

python 复制代码
# -*- coding: utf-8 -*-
# @File : test_shop.py
# @Time : 2024/3/9 20:19
# @Author : syq
# @Email : 1721169065@qq.com
# @Software: PyCharm
import pytest
from tools.excelControl import get_data_excel
from lib.apiLib.shop import Shop
from lib.apiLib.login import Login

class Test_Shop:
    #获取token,token值只需要获取一次
    def setup_class(self):
        self.token=Login().login({"username":"ct0909","password":"89254"},getToken=True)
        self.shop=Shop(self.token)
    @pytest.mark.parametrize("respData,resReq",get_data_excel('../data/外卖系统接口测试用例-V1.5.xls','我的商铺','listshopping'))
    def test_shop_list(self,respData,resReq):
        resReal=self.shop.shop_list(respData)
        if "code" in resReal:
            assert resReal['code']==resReq['code']
        else:
            assert resReal['error']==resReq['error']
    #更新商铺
    @pytest.mark.parametrize("respData,resReq",get_data_excel('../data/外卖系统接口测试用例-V1.5.xls','我的商铺','updateshopping'))
    def test_shop_update(self,respData,resReq):
        shopId=self.shop.shop_list({"page":1,"limit":1})['data']['records'][0]['id']
        image_path=self.shop.file_update('温州修改.png','../data/温州修改.png','image.png')
        resReal=self.shop.shop_update(respData,shopId,image_path)
        assert resReal['code']==resReq['code']
if __name__ == '__main__':
    pytest.main(['test_shop.py','-s'])

结果:

相关推荐
shushangyun_12 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉12 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
dayuOK630712 天前
写作卡壳怎么办?我的“5分钟启动法”
人工智能·职场和发展·自动化·新媒体运营·媒体
志栋智能12 天前
超自动化巡检:如何选择适合你的起点?
运维·自动化
HackTwoHub12 天前
Sqli-Scanner SQL注入SKILL自动化挖掘SQL注入,零依赖自动化SQL注入挖掘,赏金猎人
数据库·人工智能·sql·web安全·网络安全·自动化·系统安全
csdndeyeye12 天前
拆解AI投简历插件:塔塔网申的技术逻辑和实测数据
人工智能·自动化·秋招·ai投简历插件·ai找工作·求职助手·应届生就业
小白学大数据12 天前
Python + 大模型行业资讯自动化摘要流水线完整工程实现方案
开发语言·python·自动化
Super Scraper12 天前
如何批量抓取 TikTok 数据而不被封锁?完整指南
爬虫·ai·自动化·抖音·tiktok·ai agent
曦尧12 天前
GitHub - jwasham/coding-interview-university: 一份完整的计算机科学学习计划,助你成为软件工程师。· GitHub
ai·自动化
天空属于哈夫克312 天前
打造私域闭环:CRM 如何驱动企微外部客户触达
自动化·企业微信·api