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

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

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

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

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

(4)断言必须使用 assert

2.pytest数据驱动

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

以下是一个简单的例子:

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()

result = eval("3 + 4")
print(result) # 输出:7

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

3.登录接口

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

test_login.py代码

# -*- 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包中创建)

代码如下:

# -*- 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'])

结果:

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布凡哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值