微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pytest fixture 传参数 request的详细使用

 

注意点:******************(pytest fixture不能夸py文件使用)

传参的时候必须要写request,底下引用要使用request.param

下面调用时候需要这样写:@pytest.mark.parametrize("test_send_code",datas,indirect=True)

单个参数传递示例:

datas = ['13568741324', ]
@pytest.fixture()
def test_send_code(self,request):
tel = request.param
return Customer(self.cookies).send_code(tel)
@pytest.mark.parametrize("test_send_code",datas,indirect=True)
def test_addCustomer(self,test_send_code):
a = test_send_code
print(f'手机号是----->{a}')
if __name__ == '__main__':
pytest.main(['test_addCustomer.py','-s'])
运行结果:

rootdir: C:\Users\xibo.zhu\work\wrok_basic\XB\jyb-cms\testcases\testcaseAPI
plugins: allure-pytest-2.8.19
collected 1 item

test_addCustomer.py 手机号是----->13568741324

 

 

 多个参数传递示例:

import pytest
class Test01():
@pytest.fixture()
def casedatas(self,request):
param = request.param
print(f"账号是{param['username']},密码是{param['pwd']}")
return param

data=[{'username':'xibo','pwd':'123'},{'username':'xueyao','pwd':'456'}]
@pytest.mark.parametrize("casedatas",data,indirect=True)
def test_Datas(self,casedatas):
print(f'我需要的账号是{casedatas["username"]},密码是{casedatas["pwd"]}')

if __name__ == '__main__':
pytest.main(['test01.py','-s'])

  

  

collected 2 items

test01.py 账号是xibo,密码是123
我需要的账号是xibo,密码是123
.账号是xueyao,密码是456
我需要的账号是xueyao,密码是456
.

============================== 2 passed in 0.28s ==============================

 

 不需要传递参数示例:

  

    @pytest.fixture()
def datas01(self):
print('不需要传递参数')

def test01(self,datas01):
print('我前面应该还是一句话。')


if __name__ == '__main__':
pytest.main(['test01.py','-s'])

collected 1 item

test01.py 不需要传递参数
我前面应该还是一句话。
.

============================== 1 passed in 0.22s ==============================

 

 

 

 具体内容可看:https://www.cnblogs.com/poloyy/p/12685948.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐