pytest学习系列_参数化详解

一、前言

  在我们做接口自动化的时候,比如,一个登陆接口,通常会对用户名和密码这两个参数做不同的参数组合输入,来验证。但是我们不想写多个测试方法,那会显得重复儿不够优雅,那有没有解决方案呢,当然是有的,使用pytest的参数化就可以很好的解决。

二、本质:

  测试步骤一致,测试数据不同

三、定义

  pytest的参数化是对列表中的对象循环,然后一一的赋值,对象主要有三种,分别是列表、元组和字典,此外,还有一种自定义id形式

四、列表形式参数化

复制代码
import pytest

'''
是对列表中的对象循环,然后一一的赋值
对象:
列表
元组
字典
'''
def add(a,b):
    return a + b

#列表
@pytest.mark.parametrize('a,b,expect',[
    [1,1,2],
    [2,2,4],
    [3,3,6],
    [4,4,8]
])
def test_add(a,b,expect):
    assert add(a,b) == expect

if __name__ == '__main__':
    pytest.main(["-s","-v","test_parametrize.py"])
复制代码

 输出结果:

复制代码
Testing started at 15:27 ...
D:\pycharm\learn\venv\Scripts\python.exe "D:\pycharm\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --target test_five.py::test_add
Launching pytest with arguments test_five.py::test_add in D:\pycharm\learn\Durant\pytest_learn\test_001

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:\learn\python\python3.7.1\python.exe
cachedir: .pytest_cache
rootdir: D:\pycharm\learn\Durant\pytest_learn\test_001
collecting ... collected 4 items

test_five.py::test_add[1-1-2] PASSED                                     [ 25%]
test_five.py::test_add[2-2-4] PASSED                                     [ 50%]
test_five.py::test_add[3-3-6] PASSED                                     [ 75%]
test_five.py::test_add[4-4-8] PASSED                                     [100%]

============================== 4 passed in 0.02s ==============================

Process finished with exit code 0
复制代码

 五、元组形式参数化

复制代码
import pytest

'''
是对列表中的对象循环,然后一一的赋值
对象:
列表
元组
字典
'''
def add(a,b):
    return a + b

#元组
@pytest.mark.parametrize('a,b,expect',[
    (1,1,2),
    (2,2,4),
    (3,3,6),
    (4,4,8)
])
def test_add(a,b,expect):
    assert add(a,b) == expect

if __name__ == '__main__':
    pytest.main(["-s","-v","test_parametrize.py"])
复制代码

 输出结果:

复制代码
Testing started at 15:26 ...
D:\pycharm\learn\venv\Scripts\python.exe "D:\pycharm\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/pycharm/learn/Durant/pytest_learn/test_001/test_five.py
Launching pytest with arguments D:/pycharm/learn/Durant/pytest_learn/test_001/test_five.py in D:\pycharm\learn\Durant\pytest_learn\test_001

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:\learn\python\python3.7.1\python.exe
cachedir: .pytest_cache
rootdir: D:\pycharm\learn\Durant\pytest_learn\test_001
collecting ... collected 4 items

test_five.py::test_add[1-1-2] PASSED                                     [ 25%]
test_five.py::test_add[2-2-4] PASSED                                     [ 50%]
test_five.py::test_add[3-3-6] PASSED                                     [ 75%]
test_five.py::test_add[4-4-8] PASSED                                     [100%]

============================== 4 passed in 0.03s ==============================

Process finished with exit code 0
复制代码

六、字典形式参数化 

复制代码
import pytest

'''
是对列表中的对象循环,然后一一的赋值
对象:
列表
元组
字典
'''
def add(a,b):
    return a + b

#字典
@pytest.mark.parametrize('data',[
    {'a':1,'b':1,'expect':2},
    {'a':4,'b':4,'expect':8}
])
def test_add(data):
    assert add(data['a'],data['b']) == data['expect']

if __name__ == '__main__':
    pytest.main(["-s","-v","test_parametrize.py"])
复制代码

输出结果:

复制代码
Testing started at 15:24 ...
D:\pycharm\learn\venv\Scripts\python.exe "D:\pycharm\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/pycharm/learn/Durant/pytest_learn/test_001/test_five.py
Launching pytest with arguments D:/pycharm/learn/Durant/pytest_learn/test_001/test_five.py in D:\pycharm\learn\Durant\pytest_learn\test_001

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:\learn\python\python3.7.1\python.exe
cachedir: .pytest_cache
rootdir: D:\pycharm\learn\Durant\pytest_learn\test_001
collecting ... collected 2 items

test_five.py::test_add[data0] PASSED                                     [ 50%]
test_five.py::test_add[data1] PASSED                                     [100%]

============================== 2 passed in 0.02s ==============================

Process finished with exit code 0
复制代码

七、自定义id参数化

复制代码
import pytest


'''
是对列表中的对象循环,然后一一的赋值
对象:
列表
元组
字典
'''
def add(a,b):
    return a + b

@pytest.mark.parametrize('a,b,expect',[
    pytest.param(1, 1, 2, id='one'),
    pytest.param(2, 2, 4, id='two'),
    pytest.param(3, 3, 6, id='three'),
    pytest.param(4, 4, 8, id='four')
])
def test_add(a,b,expect):
    assert add(a,b) == expect


if __name__ == '__main__':
    pytest.main(["-s","-v","test_parametrize.py"])
复制代码

输出结果:

复制代码
Testing started at 15:21 ...
D:\pycharm\learn\venv\Scripts\python.exe "D:\pycharm\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --target test_five.py::test_add
Launching pytest with arguments test_five.py::test_add in D:\pycharm\learn\Durant\pytest_learn\test_001

============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:\learn\python\python3.7.1\python.exe
cachedir: .pytest_cache
rootdir: D:\pycharm\learn\Durant\pytest_learn\test_001
collecting ... collected 4 items

test_five.py::test_add[one] PASSED                                       [ 25%]
test_five.py::test_add[two] PASSED                                       [ 50%]
test_five.py::test_add[three] PASSED                                     [ 75%]
test_five.py::test_add[four] PASSED                                      [100%]

============================== 4 passed in 0.02s ==============================

Process finished with exit code 0
复制代码

 

posted @   未来可期_Durant  阅读(481)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示