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 @ 2020-11-28 00:21  未来可期_Durant  阅读(441)  评论(0编辑  收藏  举报