【pytest】parametrize获取参数的几种常用形式

一、直接在标签上传参:

一个参数多个值

@ pytest.mark.parametrize("参数", (参数值1, 参数值2, 参数值3))

示例:

import pytest
# 单个参数的情况
@pytest.mark.parametrize("a", (1, 2, 3, 4))
def test_add(a):
    print("\na的值")

结果:

============================= test session starts =============================
collecting ... collected 4 items

test3.py::test_add[1] PASSED                                             [ 25%]
a的值

test3.py::test_add[2] PASSED                                             [ 50%]
a的值

test3.py::test_add[3] PASSED                                             [ 75%]
a的值

test3.py::test_add[4] PASSED                                             [100%]
a的值


============================== 4 passed in 0.10s ==============================

Process finished with exit code 0

多个参数多个值的情况

# 单个参数的情况
# 格式:@pytest.mark.parametrize("参数",参数值)
@pytest.mark.parametrize("a", (1, 2, 3, 4))
def test_add(a):
    print("\na的值")


# 多个参数多个值的情况
# 格式:@pytest.mark.parametrize("参数a, 参数b",([a1, b1],[a2,b2],[a3, b3]))
@pytest.mark.parametrize("b,c,d", ([1, 2, 3], [4, 5, 6], [7, 8, 0]))
def test_add2(b, c, d):
    print('\nb,c,d的值分别为:', f'{b}, {c}, {d}')

二、数据结构

列表(list)形式

# 单个参数的情况
data1 = [1, 2, 3, 4]


@pytest.mark.parametrize("a", data1)
def test_add(a):
    print('\na的值:', a)


# 多个参数多个值得情况

data2 = [
    (1, 2, 3),
    (4, 5, 6),
    (0, 8, 9)
]


@pytest.mark.parametrize("a,b,c", data2)
def test_add2(a, b, c):
    print('\na,b,c的值分别为:', f'{a},{b},{c}')

输出

============================= test session starts =============================
collecting ... collected 7 items
parametrize02_list.py::test_add[1] PASSED                                [ 14%]
a的值: 1
parametrize02_list.py::test_add[2] PASSED                                [ 28%]
a的值: 2
parametrize02_list.py::test_add[3] PASSED                                [ 42%]
a的值: 3
parametrize02_list.py::test_add[4] PASSED                                [ 57%]
a的值: 4
parametrize02_list.py::test_add2[1-2-3] PASSED                           [ 71%]
a,b,c的值分别为: 1,2,3
parametrize02_list.py::test_add2[4-5-6] PASSED                           [ 85%]
a,b,c的值分别为: 4,5,6
parametrize02_list.py::test_add2[0-8-9] PASSED                           [100%]
a,b,c的值分别为: 0,8,9
============================== 7 passed in 0.05s ==============================
Process finished with exit code 0

字典(dictionary)形式

import pytest

userinfo = [
    {"username": "小李", "password": "123456"},
    {"username": "大白", "password": "894561"}
]


@pytest.mark.parametrize('info', userinfo)
def test_add(info):
    print(info)

输出:

============================= test session starts =============================
collecting ... collected 2 items

parametrize03_dic.py::test_add[info0] PASSED                             [ 50%]{'username': '小李', 'password': '123456'}

parametrize03_dic.py::test_add[info1] PASSED                             [100%]{'username': '大白', 'password': '894561'}

元组(tuple)形式

# 元组形式:
import pytest

data1 = [(1, 2, 3), (4, 5, 6)]


@pytest.mark.parametrize("a,b,c", data1)
def test_case1(a, b, c):
    print(a, b, c)

输出

============================= test session starts =============================
collecting ... collected 2 items

parametrize04_tuple.py::test_case1[1-2-3] PASSED                         [ 50%]1 2 3

parametrize04_tuple.py::test_case1[4-5-6] PASSED                         [100%]4 5 6


============================== 2 passed in 0.03s ==============================

Process finished with exit code 0

三、json形式

四、文件(txt、csv、Excel 文件)

五、数据库:读取数据库文件要导入pymysql库

六、配置conftest.py文件

相关推荐
唐古乌梁海9 小时前
【pytest】编写自动化测试用例命名规范README
自动化·pytest
xing25169 小时前
pytest下allure
开发语言·python·pytest
IT求学人1 天前
pytest运行用例的常见方式及参数
pytest
开源优测2 天前
什么是pytest.ini及如何在Pytest中应用以提升配置效率
pytest
摸鱼仙人~3 天前
ImportError: cannot import name ‘FixtureDef‘ from ‘pytest‘
conda·pytest·fastapi
唐古乌梁海3 天前
【pytest-jira】自动化用例结合jira初版集成思路
pytest·jira
小码哥说测试5 天前
高效执行自动化用例:分布式执行工具pytest-xdist实战!
自动化测试·软件测试·功能测试·jmeter·pytest·postman·测试工程师
予早5 天前
pytest asyncio 支持插件 pytest-asyncio
pytest