zoukankan      html  css  js  c++  java
  • 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:pycharmlearnvenvScriptspython.exe "D:pycharmPyCharm Community Edition 2019.3.3pluginspython-cehelperspycharm\_jb_pytest_runner.py" --target test_five.py::test_add
    Launching pytest with arguments test_five.py::test_add in D:pycharmlearnDurantpytest_learn	est_001
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:learnpythonpython3.7.1python.exe
    cachedir: .pytest_cache
    rootdir: D:pycharmlearnDurantpytest_learn	est_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:pycharmlearnvenvScriptspython.exe "D:pycharmPyCharm Community Edition 2019.3.3pluginspython-cehelperspycharm\_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:pycharmlearnDurantpytest_learn	est_001
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:learnpythonpython3.7.1python.exe
    cachedir: .pytest_cache
    rootdir: D:pycharmlearnDurantpytest_learn	est_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:pycharmlearnvenvScriptspython.exe "D:pycharmPyCharm Community Edition 2019.3.3pluginspython-cehelperspycharm\_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:pycharmlearnDurantpytest_learn	est_001
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:learnpythonpython3.7.1python.exe
    cachedir: .pytest_cache
    rootdir: D:pycharmlearnDurantpytest_learn	est_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:pycharmlearnvenvScriptspython.exe "D:pycharmPyCharm Community Edition 2019.3.3pluginspython-cehelperspycharm\_jb_pytest_runner.py" --target test_five.py::test_add
    Launching pytest with arguments test_five.py::test_add in D:pycharmlearnDurantpytest_learn	est_001
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.1, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:learnpythonpython3.7.1python.exe
    cachedir: .pytest_cache
    rootdir: D:pycharmlearnDurantpytest_learn	est_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
    知道、想到、做到、得到
  • 相关阅读:
    GB 51411-2020 金属矿山土地复垦工程设计标准
    GB/T 51413-2020 有色金属工业余热利用设计标准
    DL/T 1907.1-2018等最新电力行业标准
    GB 50205-2020 钢结构工程施工质量验收标准
    DL/T 5210.6-2019 电力建设施工质量验收规程 第6部分:调整试验
    GB/T 38640-2020 盲用数字出版格式
    GB 50325-2020 民用建筑工程室内环境污染控制标准(含条文说明)
    GB 50216-2019 铁路工程结构可靠性设计统一标准
    最新发布的国家标准下载(2020-5-21)
    bind、apply、call的区别
  • 原文地址:https://www.cnblogs.com/Durant0420/p/14051118.html
Copyright © 2011-2022 走看看