遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】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

1
2
3
4
5
6
7
8
9
10
11
12
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
#!/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方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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,数据用字典列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/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指定间接参数

不指定间接参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/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}")

  

结果:

 

指定间接参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/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}")

 

结果:

 

思考题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/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 @   全栈测试笔记  阅读(237)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2022-02-23 答疑记录:jmeter从返回的html中提取指定内容
浏览器标题切换
浏览器标题切换end
点击右上角即可分享
微信分享提示