上周笔记是学习框架的包结构和封装了一个接口请求方法中都会使用的一个请求和发送方法;
这次学习安装pytest参数化
现在pycharm中的terminal中安装
输入命令:
下载requests的命令
Windows机下载命令
pip install requests -i https://pypi.douban.com/simple/
mac下载命令 :
Python3 -m pip install requests -i https://pypi.douban.com/simple/
下载pytest的命令
添加豆瓣源
Windows
pip install pytest -i https://pypi.douban.com/simple/
mac :
Python3 -m pip install pytest -i https://pypi.douban.com/simple/
(如果pip 安装不成功
可以通过pycharm安装,可以在pycharm中的file–settings–project,项目名下的python
interpreter中点击‘+’然后在搜索框中去添加要的pytest)
切换pytest环境,在pycharm–file–settings–tools–python–integrated tools中右侧的Testing下Default test runner下拉框中选择pytest即可
pytest参数化
第一种方式:
1,构造测试数据
2,引用pytest装饰器方法 @pytest.mark.parametrize
class TestMtx:
test_data = [['有效数据',14014,1,200],['num数量0',14014,0,400],['skuid不存在',124324,1,500]]
# 把测试数据传递 (参数变量,test_data)
@pytest.mark.parametrize('casename,sku_id,num,expect_code',test_data)
def test_buy_now(self,casename,sku_id,num,expect_code):
# 调用登录方法
login()
# 调用需要参数的接口方法
resp = testparams(sku_id=sku_id,num=num)
code = resp.status_code
text = resp.text
# 断言,判断得到的code与期待的code是否一致
assert code == expect_code
print(f'状态码:{code}')
print(f'响应数据字符串:{text}')
第二种方式:
笛卡尔积方式:
同时添加多个@pytest.mark.parametrize装饰器,多个parametrize的左右元素
相互组合
import pytest
from lesson0312.db_stu import DBUtil
from lesson0312.mtxshops import buyer_login, buy_now, create_trade, add_carts
class TestCreateTrade:
# 笛卡尔积参数化场景:参数组合场景较多-有效组合 不适合所有接口场景数据
# 测试用例名称、客户端、购买方式、响应状态码
# PC and BUY_NOW,'PC','BUY_NOW',200
# PC and CART,'PC','CART',200
client_data = ['PC','WAP','NATIVE','REACT','MINI']
way_data = ['BUY_NOW','CART']
expect_code = [200]
@pytest.mark.parametrize('client',client_data)
@pytest.mark.parametrize('way',way_data)
@pytest.mark.parametrize('expect_code',expect_code)
def test_create_trade(self,client,way,expect_code,get_buyer_login):
# 创建交易接口 登录-立即购买
# buyer_login()
# 创建交易前 立即购买或添加购物车
if way == 'BUY_NOW':
buy_now()
elif way == 'CART':
add_carts()
resp = create_trade(client=client,way=way)
code = resp.status_code
text = resp.text
print(code)
print(text)
assert code == expect_code
print('状态码200校验通过')