zoukankan      html  css  js  c++  java
  • pytest六:parametrize-参数化


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

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


    import pytest

    @pytest.mark.parametrize('test_input,expected',
    [('3+5', 8),
    ('2+4', 6),
    ('6*9', 42)]
    )
    def test_eval(test_input, expected):
    assert eval(test_input) == expected

    if __name__=='__main__':
    pytest.main()


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

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

    @pytest.mark.parametrize('test_input,expected',
    [('3+5', 8),
    ('2+4', 6),
    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()





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

    这将运行测试,参数设置为 x=0/y=2,x=1/y=2,x=0/y=3,x=1/y=3 组合参数。
    import pytest
    @pytest.mark.parametrize('x', [0, 1])
    @pytest.mark.parametrize('y', [2, 3])

    def test_foo(x, y):
    print(f' 测试数据组合:x->{x}, y->{y}')

    if __name__=='__main__':
    pytest.main()


  • 相关阅读:
    156. Binary Tree Upside Down
    155. Min Stack
    154. Find Minimum in Rotated Sorted Array II
    153. Find Minimum in Rotated Sorted Array
    汉诺塔问题
    算法——二分搜索
    linux内核编程helloworld(中级)
    linux内核入门(1)——基本简介和编译
    linux网络编程概念(一)
    linux配置防火墙
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/9680598.html
Copyright © 2011-2022 走看看