简介
1、indirect默认是False
2、如果设置成True,表示把被parametrize修饰器修饰的方法形参当函数执行(parametrize中参数名和这个形参同名),此时必须有被@pytest.fixture()修饰的和形参名同名的函数(可以对参数做一些加工处理),否则报错:fixture 'xxx' not found,xxx表示形参名;简单说,为True时,形参被当成是一个fixture函数
3、fixture修饰器中没有params参数
4、可以通过indirect指定间接参数
验证:indirect默认值是False
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
data = ["qzcsbj","ren","jack"]
@pytest.mark.parametrize("register", data)
def test_case(register):
print(f"register={register}")
结果:
indirect设置为False
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
data = ["qzcsbj","ren","jack"]
@pytest.mark.parametrize("register", data, indirect=False)
def test_case(register):
print(f"register={register}")
结果:和不指定indirect时结果一样,说明默认是False
反推:改为True,需要添加parametrize参数名同名fixture
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
data = ["qzcsbj","ren","jack"]
@pytest.mark.parametrize("register", data, indirect=True)
def test_case(register):
print(f"register={register}")
结果:报错,没fixture函数
添加固件,注意:此时fixture修饰器中没有params参数
此时data会传到register方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
@pytest.fixture()
def register(request):
print("当前返回参数是:",request.param)
return request.param
data = ["qzcsbj","ren","jack"]
@pytest.mark.parametrize("register", data, indirect=True)
def test_case(register):
print(f"register={register}")
结果:
传多个参数:一个fixture,数据用字典列表
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
@pytest.fixture()
def login(request):
param = request.param
print(f"当前返回参数是:uname={param['uname']},pwd={param['pwd']}")
return param
data = [
{"uname": "ren", "pwd": "123"},
{"uname": "qzcsbj", "pwd": "456"},
]
@pytest.mark.parametrize("login", data, indirect=True)
def test_case(login):
print(f"uname:{login['uname']},pwd:{login['pwd']}")
结果:
传多个参数:多个fixture
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
@pytest.fixture()
def uname(request):
uname = request.param
print(f"当前返回参数是:uname={uname}")
return uname
@pytest.fixture()
def pwd(request):
pwd = request.param
print(f"当前返回参数是:pwd={pwd}")
return pwd
data = [
("ren", "123"),
("qzcsbj", "456")
]
@pytest.mark.parametrize("uname,pwd", data, indirect=True)
def test_case(uname, pwd):
print(f"uname:{uname},pwd:{pwd}")
结果:
indirect指定间接参数
不指定间接参数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
# 有params参数
@pytest.fixture()
def fun(request):
print("返回的数据是:",request.param)
return request.param + "_666"
# 无params参数
@pytest.fixture
def fun2(request):
print("返回的数据是:", request.param)
return request.param + "_6"
@pytest.mark.parametrize("fun,fun2", [("qzcsbj", "ren"), ("jack","tom")], indirect=True)
def test_case(fun,fun2):
print(f"fun={fun}, fun2={fun2}")
结果:
指定间接参数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
# 有params参数
@pytest.fixture()
def fun(request):
print("返回的数据是:",request.param)
return request.param + "_666"
# 无params参数
@pytest.fixture
def fun2(request):
print("返回的数据是:", request.param)
return request.param + "_6"
@pytest.mark.parametrize("fun,fun2", [("qzcsbj", "ren"), ("jack","tom")], indirect=["fun"])
def test_case(fun,fun2):
print(f"fun={fun}, fun2={fun2}")
结果:
思考题:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
import pytest
# 有params参数
@pytest.fixture(params=[1, 2])
def fun(request):
print("返回的数据是:",request.param)
return request.param
@pytest.mark.usefixtures('fun')
def test_1(fun):
print("fun=", fun)
# 无params参数
@pytest.fixture
def fun2(request):
print("返回的数据是:", request.param)
return request.param + "_qzcsbj"
@pytest.mark.parametrize("fun2", ["a", "b"], indirect=True)
def test_2(fun2):
print("fun2=", fun2)
思考:为什么是如下结果?
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等