pytest【参数化测试 二】

@pytest.mark.parametrize:参数化测试功能

使用内置的pytest.mark.parametrize装饰器为测试函数启用参数的参数化。下面是字符串/元组/列表/字典等示例,该功能实现检查某些输入是否导致期望的输出

注意:parametrize里面传的参数均为序列类型的数据,传小数或整数时会报类型错误

获取单个参数化

import pytest


@pytest.mark.parametrize('a', '5')
def test_01_fourteen(a):
    print(a)


@pytest.mark.parametrize('a', (5, 6))
def test_02_fourteen(a):
    print(a)


@pytest.mark.parametrize('a', [5, 5])
def test_03_fourteen(a):
    print(a)


@pytest.mark.parametrize('a', {'5': 5})
def test_04_fourteen(a):
    print(a)


if __name__ == '__main__':
    pytest.main(['-s', 'test_fourteen.py'])

运行后的结果如下:

STING\BlogPosts\ReadPytest\ch1>pytest -s -v test_fourteen.py
================================================================================== test session starts ===================================================================================
collected 6 items                                                                                                                                                                         

test_fourteen.py::test_01_fourteen[5] 5
PASSED
test_fourteen.py::test_02_fourteen[5] 5
PASSED
test_fourteen.py::test_02_fourteen[6] 6
PASSED
test_fourteen.py::test_03_fourteen[50] 5
PASSED
test_fourteen.py::test_03_fourteen[51] 5
PASSED
test_fourteen.py::test_04_fourteen[5] 5
PASSED

=================================================================================== 6 passed in 0.03s ====================================================================================

获取多个参数化的组合

import pytest


@pytest.mark.parametrize('a, b', [(5+5, 10), (10+10, 20)])
def test_fourteen(a, b):
    assert a == b


if __name__ == '__main__':
    pytest.main(['-s', 'test_fourteen.py'])

运行后的结果如下:

(python_env) F:\TESTING\BlogPosts\ReadPytest\ch1>pytest -s -v test_fourteen.py
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

test_fourteen.py::test_fourteen[10-10] PASSED
test_fourteen.py::test_fourteen[20-20] PASSED

=================================================================================== 2 passed in 0.06s ====================================================================================

堆叠 parametrize装饰器获取参数组合:

import pytest


class TestTwelve:

    @pytest.mark.parametrize('actual', [1, 2])
    @pytest.mark.parametrize('expect', [3, 4])
    def test_twelve(self, actual, expect):
        print(actual, expect)

运行后的结果如下:

(python_env) F:\TESTING\BlogPosts\ReadPytest\ch1>pytest -s -v test_twelve.py
================================================================================== test session starts ===================================================================================
collected 4 items                                                                                                                                                                         

test_twelve.py::TestTwelve::test_twelve[3-1] 1 3
PASSED
test_twelve.py::TestTwelve::test_twelve[3-2] 2 3
PASSED
test_twelve.py::TestTwelve::test_twelve[4-1] 1 4
PASSED
test_twelve.py::TestTwelve::test_twelve[4-2] 2 4
PASSED

=================================================================================== 4 passed in 0.07s ====================================================================================

运行后与设定参数的测试actual=1/expect=3,actual=2/expect=3, actual=1/expect=4,并actual=2/expect=4在装饰的顺序排出参数。


也可以在参数化中标记各个测试实例,例如使用内置的mark.xfail:

import pytest


@pytest.mark.parametrize(
    'test, expected',
    [('3+5', 8), ('2+4', 6), pytest.param('6*9', 42, marks=pytest.mark.xfail)]
)
def test_thirteen(test, expected):
    assert eval(test) == expected


if __name__ == '__main__':
    pytest.main(['-s', 'test_thirteen.py'])

运行后的结果如下:

(python_env) F:\TESTING\BlogPosts\ReadPytest\ch1>pytest -s -v test_thirteen.py
================================================================================== test session starts ===================================================================================
collected 3 items                                                                                                                                                                         

test_thirteen.py::test_thirteen[3+5-8] PASSED
test_thirteen.py::test_thirteen[2+4-6] PASSED
test_thirteen.py::test_thirteen[6*9-42] XFAIL

============================================================================== 2 passed, 1 xfailed in 0.06s ==============================================================================

先前设置失败的参数,现在显示未“xfailed (预期失败)”测试。


parametrize中的id选项,每次运行时pytest将构建一个字符串,该字符串是参数化测试中每组值的测试id,没指定id时pytest会自动创建,指定后会显示你指定的id名称

import pytest


@pytest.mark.parametrize('a, b', [(1, 2), (3, 4)], ids=['a', 'b'])
def test_fourteen(a, b):
    print(a, b)


if __name__ == '__main__':
    pytest.main(['-s', 'test_fourteen.py'])

运行后的结果如下:

(python_env) F:\TESTING\BlogPosts\ReadPytest\ch1>pytest -s -v test_fourteen.py
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

test_fourteen.py::test_fourteen[a] 1 2
PASSED
test_fourteen.py::test_fourteen[b] 3 4
PASSED

=================================================================================== 2 passed in 0.06s ====================================================================================

当然也可以为单个测试设置参数化的id值

import pytest


@pytest.mark.parametrize(
    'a, b, expected',
    [pytest.param(5, 8, 40, id='first'),
     pytest.param(6, 9, 50, id='second')
     ]
)
def test_thirteen(a, b, expected):
    product = a * b
    assert product == expected


if __name__ == '__main__':
    pytest.main(['-s', 'test_thirteen.py'])

运行后的结果如下:

BlogPosts\ReadPytest\ch1>pytest -s -v test_thirteen.py
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

test_thirteen.py::test_thirteen[first] PASSED
test_thirteen.py::test_thirteen[second] FAILED

======================================================================================== FAILURES ========================================================================================
_________________________________________________________________________________ test_thirteen[second] __________________________________________________________________________________

a = 6, b = 9, expected = 50

    @pytest.mark.parametrize(
        'a, b, expected',
        [pytest.param(5, 8, 40, id='first'),
         pytest.param(6, 9, 50, id='second')
         ]
    )
    def test_thirteen(a, b, expected):
        product = a * b
>       assert product == expected
E       assert 54 == 50
E         +54
E         -50

test_thirteen.py:20: AssertionError
================================================================================ short test summary info =================================================================================
FAILED test_thirteen.py::test_thirteen[second] - assert 54 == 50
============================================================================== 1 failed, 1 passed in 0.10s ===============================================================================

上面简介了第二部分的参数化在测试中的基础使用,总结如有不当之处,还请提出疑义,后面会加以修改,多谢阅读!

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值