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
    知道、想到、做到、得到
  • 相关阅读:
    【Navicat】查看历史执行的SQL
    什么是webpack模块化构建工具
    靠边的列表如果没有设置margin-left:20px,那么是看不到列表序号的。
    在博客园中复制代码到网页中,有时候会存在异常,如下:
    / WebAPP开发与小程序 / 步骤一 · 4-5 地图搜索与poi结合(2)
    忘记样式属性对应的值时,可以使用以下方法进行操作
    //点击按钮加减音频音量到最小会出现bug什么意思???
    组件化网页开发 3步骤 / 20门课
    position:absolute 按钮左右分布:left:0 和 right:0 以及雪碧图
    查看引入的文件是否成功
  • 原文地址:https://www.cnblogs.com/Durant0420/p/14051118.html
Copyright © 2011-2022 走看看