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

 

 

 

 

# 多个fixture
@pytest.fixture(scope="module")
def input_user(request):
  user = request.param print("登录账户:%s" % user)
   return user
 
@pytest.fixture(scope="module")
def input_psw(request):
  psw = request.param print("登录密码:%s" % psw)
  return psw data = [ ("name1", "pwd1"), ("name2", "pwd2") ]
 
 
 
@pytest.mark.parametrize("input_user,input_psw", data, indirect=True)
def test_more_fixture(input_user, input_psw):
   print("fixture返回的内容:", input_user, input_psw)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


.

posted @ 2020-11-03 11:25  西西cc  阅读(432)  评论(0编辑  收藏  举报