其他分享
首页 > 其他分享> > 006、参数化parametrize

006、参数化parametrize

作者:互联网

参数化parametrize

  pytest.mark.parametrize 装饰器可以实现测试用例参数化

  parametrize里面的参数a,b,c要和 测试用例test_01 里面的 a,b,s 要一样。

 

示例代码如下:

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/16 17:21
import pytest


def sum(c, d):
    return c + d


# parametrize里面的参数a,b,c要和 测试用例test_01 里面的 a,b,s 要一样。
@pytest.mark.parametrize('a, b, s', [(1, 2, 3), (1, 2, 4)])
def test_01(a, b, s):
    assert sum(a, b) == s
View Code

 

如果测试输入的参数有多个时候,   列表和元组都可以存放数据。

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/16 17:21
import pytest


def login(username, paswd):
    print(username)
    print(paswd)
    return 'sucess'


@pytest.mark.parametrize('username, paswd, excepted',
    [('sky', '123', 'sucess'), ('jack', '456', 'fail')]
)


def test_01(username, paswd, excepted):
    result = login(username, paswd)
    assert result == excepted


@pytest.mark.parametrize('username, paswd, excepted',
    [['sky', '123', 'sucess'], ['jack', '456', 'fail']]
)


def test_02(username, paswd, excepted):
    result = login(username, paswd)
    assert result == excepted
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>pytest -s .\test_02.py
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd
plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0
collected 4 items

test_02.py sky
123
.jack
456
Fsky
123
.jack
456
F

========================================================================================= FAILURES =========================================================================================
__________________________________________________________________________________ test_01[jack-456-fail] __________________________________________________________________________________

username = 'jack', paswd = '456', excepted = 'fail'

    @pytest.mark.parametrize('username, paswd, excepted',
        [('sky', '123', 'sucess'), ('jack', '456', 'fail')]
    )


    def test_01(username, paswd, excepted):
        result = login(username, paswd)
>       assert result == excepted
E       AssertionError: assert 'sucess' == 'fail'
E         - fail
E         + sucess

test_02.py:31: AssertionError
__________________________________________________________________________________ test_02[jack-456-fail] __________________________________________________________________________________

username = 'jack', paswd = '456', excepted = 'fail'

    @pytest.mark.parametrize('username, paswd, excepted',
        [['sky', '123', 'sucess'], ['jack', '456', 'fail']]
    )


    def test_02(username, paswd, excepted):
        result = login(username, paswd)
>       assert result == excepted
E       AssertionError: assert 'sucess' == 'fail'
E         - fail
E         + sucess

test_02.py:43: AssertionError
================================================================================= short test summary info ==================================================================================
FAILED test_02.py::test_01[jack-456-fail] - AssertionError: assert 'sucess' == 'fail'
FAILED test_02.py::test_02[jack-456-fail] - AssertionError: assert 'sucess' == 'fail'
=============================================================================== 2 failed, 2 passed in 0.20s ================================================================================

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>
View Code

 

输入参数很多时,可以放到 testinput 字典

# -*- coding:utf-8 -*-
# @Author:  Sky
# @Email:   2780619724@qq.com
# @Time:    2021/7/16 17:21
import pytest


# 用户名,密码,验证码
def login(username, paswd, veri_code):
    print(username)
    print(paswd)
    print(veri_code)
    return 'sucess'


@pytest.mark.parametrize('test_input, excepted',
    [[{'username' : 'sky', 'paswd' : '123', 'veri_code' : '123'}, 'sucess'],
     [{'username' : 'jack', 'paswd' : '123', 'veri_code' : '123'}, 'fail']]
)
def test_02(test_input, excepted):
    result = login(test_input['username'], test_input['paswd'],  test_input['veri_code'])
    assert result == excepted
View Code

执行结果如下:

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>pytest -s .\test_02.py
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd
plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0
collected 2 items

test_02.py sky
123
123
.jack
123
123
F

========================================================================================= FAILURES =========================================================================================
________________________________________________________________________________ test_02[test_input1-fail] _________________________________________________________________________________

test_input = {'paswd': '123', 'username': 'jack', 'veri_code': '123'}, excepted = 'fail'

    @pytest.mark.parametrize('test_input, excepted',
        [[{'username' : 'sky', 'paswd' : '123', 'veri_code' : '123'}, 'sucess'],
         [{'username' : 'jack', 'paswd' : '123', 'veri_code' : '123'}, 'fail']]
    )
    def test_02(test_input, excepted):
        result = login(test_input['username'], test_input['paswd'],  test_input['veri_code'])
>       assert result == excepted
E       AssertionError: assert 'sucess' == 'fail'
E         - fail
E         + sucess

test_02.py:22: AssertionError
================================================================================= short test summary info ==================================================================================
FAILED test_02.py::test_02[test_input1-fail] - AssertionError: assert 'sucess' == 'fail'
=============================================================================== 1 failed, 1 passed in 0.18s ================================================================================

D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>
View Code

 

 

标签:username,excepted,parametrize,paswd,fail,123,参数,006,test
来源: https://www.cnblogs.com/qq-2780619724/p/15021704.html