pytest_参数化parametrize

前言

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

parametrizing

1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子

import pytest

@pytest.mark.parametrize(
    "test_input, expected",
    [
        ("3+5", 8),
        ("5+7", 12),
        ("9/3", 3),
        ("6*9", 42)
    ]
)
def test_eval(test_input, expected):
    print("______测试用例_______")
    assert eval(test_input) == expected

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

运行结果

============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: D:\python_auto\s14\pytest_learn, inifile:
collected 4 items

Parametrize_01.py ______测试用例_______
.______测试用例_______
.______测试用例_______
.______测试用例_______
F

================================== FAILURES ===================================
______________________________ test_eval[6*9-42] ______________________________

test_input = '6*9', expected = 42

    @pytest.mark.parametrize(
        "test_input, expected",
        [
            ("3+5", 8),
            ("5+7", 12),
            ("9/3", 3),
            ("6*9", 42)
        ]
    )
    def test_eval(test_input, expected):
        print("______测试用例_______")
>       assert eval(test_input) == expected
E       AssertionError: assert 54 == 42
E        +  where 54 = eval('6*9')

Parametrize_01.py:18: AssertionError
===================== 1 failed, 3 passed in 0.07 seconds ======================

 

在这个例子中设计的,只有一条输入/输出值的简单测试功能。和往常一样

函数的参数,你可以在运行结果看到在输入和输出值

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

import pytest

@pytest.mark.parametrize(
    "test_input, expected",
    [
        ("3+5", 8),
        ("5+7", 12),
        ("9/3", 3),
        pytest.param("6*9", 42,
        marks=pytest.mark.xfail),
    ]
)
def test_eval(test_input, expected):
    print("______测试用例_______")
    assert eval(test_input) == expected

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

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: D:\python_auto\s14\pytest_learn, inifile:
collected 4 items

Parametrize_02.py ______测试用例_______
.______测试用例_______
.______测试用例_______
.______测试用例_______
x

===================== 3 passed, 1 xfailed in 0.10 seconds =====================

标记为失败的用例就不运行了,直接跳过显示xfailed

参数组合

1.若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器

import pytest

@pytest.mark.parametrize("x", [0,1])
@pytest.mark.parametrize("y", [2,3])
def test_eval(x, y):
    print("______测试用例_______")
    print("测试数据组合: x -> %s, y -> %s"% (x, y))

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

运行结果

============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: D:\python_auto\s14\pytest_learn, inifile:
collected 4 items

Parametrize_03.py ______测试用例_______
测试数据组合: x -> 0, y -> 2
.______测试用例_______
测试数据组合: x -> 1, y -> 2
.______测试用例_______
测试数据组合: x -> 0, y -> 3
.______测试用例_______
测试数据组合: x -> 1, y -> 3
.

========================== 4 passed in 0.05 seconds ===========================

这将运行测试,参数设置为x=0/y=2,x=1/y=2,x=0/y=3,x=1/y=3组合参数。

 

 

作者:含笑半步颠√

博客链接:https://www.cnblogs.com/lixy-88428977

声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。

转载于:https://www.cnblogs.com/lixy-88428977/p/9672579.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值