遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(18):parametrize中indirect用法(间接参数)

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

简介

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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 : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

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)

  

思考:为什么是如下结果?

 

posted @ 2024-02-23 21:57  全栈测试笔记  阅读(176)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end