Pytest中参数化详解

Pytest中参数化详解1

'''
    import pytest

    def add(a, b):
        return a + b

    """参数为列表和元组书写方式一样"""
    # @pytest.mark.parametrize(
        # 'a,b,expect',
        # [
        #     [1,2,3],
        #     [3,4,7],
        #     [7,8,15],
        # ]
        # [
        #     (1, 2, 3),
        #     (3, 4, 7),
        #     (7, 8, 15),
        # ]
    # )
    # def test_add(a, b, expect):
    #     assert add(a, b)==expect

    """参数为字典的书写方式"""
    @pytest.mark.parametrize(
        'data',
        [
            {'a':1,'b':2,'expect':3},
            {'a':2,'b':3,'expect':5},
            {'a':3,'b':4,'expect':7},
        ]
    )
    def test_add_01(data):
        assert add(data['a'], data['b'])==data['expect']

    if __name__ == '__main__':
        pytest.main(['-s', '-v', 'Pytest中参数化详解(一).py'])
'''

Pytest中参数化详解2

'''
    import pytest

    def add(a, b):
        return a + b

    @pytest.mark.parametrize(
        'a,b,excepts',
        [
            pytest.param(1, 1, 2, id='1'), # id标识如果用中文,无法解码,最好是用英文及数字表示
            pytest.param(2, 2, 4, id='2'),
            pytest.param(3, 3, 6, id='3'),
        ]
    )
    def test_add(a, b, excepts):
        assert add(a, b)==excepts

    if __name__ == '__main__':
        pytest.main(['-v', '-s', 'Pytest中参数化详解(二).py'])
'''
while True: print('studying...')
原文地址:https://www.cnblogs.com/xuewei95/p/15129737.html