zoukankan      html  css  js  c++  java
  • 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...')
  • 相关阅读:
    jsp页面增加语音播报
    tomcat页面重定向跳转
    飘窗
    将回车键与页面ID绑定
    例35:十进制转二进制
    例33:求100-200间素数
    例30:尼科彻斯定理
    例29:哥德巴赫猜想
    例28:斐波那契数列
    例27:哈希查找
  • 原文地址:https://www.cnblogs.com/xuewei95/p/15129737.html
Copyright © 2011-2022 走看看