美文网首页
pytest的参数化

pytest的参数化

作者: Chaweys | 来源:发表于2021-05-19 22:47 被阅读0次

    #coding=utf-8
    import pytest
    '''
    pytest参数化
    '''
    
    def add(a,b):
        return a+b
    
    """
    pytest参数化用@pytest.mark.parametrize表示:
    a和b代表要参数化的变量名,result是期望结果的参数化的变量名
    所有的数据必须用列表括起元祖 [(),(),()]
    或是列表括起的列表 [[],[],[]]
    
    注:@pytest.mark.parametrize()装饰器接收两个参数,
    第一个参数是字符串的形式标识用例函数的参数,
    第二个参数以元祖的形式传递测试数据() 或者是 列表括起的元祖的形式[(),(),()]
    """
    @pytest.mark.parametrize(
        'a,b,result',
        [
            (1,2,3),
            (4,5,9),
            ('he','llo','hello')
        ]
    )
    
    
    改进:
    resultList= [
            (1,2,3),
            (4,5,9),
            ('he','llo','hello')
        ]
    或:
    resultList= [
            [1,2,3],
            [4,5,9],
            ['he','llo','hello']
        ]
    
    @pytest.mark.parametrize(
        'a,b,result',
         resultList
    )
    
    
    #多个参数的情况:resultList是外层列表,元素为列表或元祖,其中每个子元素和参数名变量一一对应起来
    #测试用例的传参或者引用参数名必须是上述参数化的的变量名,预期结果引用必须是上述参数化的预期结果变量名
    def test_add(a,b,result): #a,b,result接收装饰器传入的数据
        print("{0}-{1}->{2}".format(a,b,result))
        assert add(a,b)==result
    
    if __name__ == '__main__':
        pytest.main(["-vs","test_py18.py"])
    
    """
    结果:
    G:\Python38\python.exe E:/ProjectStudy/HDCZU_Study/Test/test_py18.py
    ============================= test session starts =============================
    platform win32 -- Python 3.8.0, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- G:\Python38\python.exe
    cachedir: .pytest_cache
    rootdir: E:\ProjectStudy\HDCZU_Study\Test
    plugins: forked-1.3.0, parallel-0.0.10, xdist-2.2.1
    collecting ... collected 3 items
    
    test_py18.py::test_add[1-2-3] 1-2->3
    PASSED
    test_py18.py::test_add[4-5-9] 4-5->9
    PASSED
    test_py18.py::test_add[he-llo-hello] he-llo->hello
    PASSED
    
    ============================== 3 passed in 0.11s ==============================
    
    Process finished with exit code 0
    
    """
    

    #coding=utf-8
    import pytest
    '''
    pytest参数化
    '''
    
    def add(a,b):
        return a+b
    
    #如果是单个参数,则参数化不需要[[ ], [ ]]或者[(),()],只需要一层[ ]或( )
    @pytest.mark.parametrize(
        'a',
        (1,2,3,4,5),
    )
    
    #单个参数的情况
    def test_add(a):
        assert add(a,1)==a + 1
    
    if __name__ == '__main__':
        pytest.main(["-vs","test_py18.py"])
    """
    结果:
    G:\Python38\python.exe E:/ProjectStudy/HDCZU_Study/Test/test_py18.py
    ============================= test session starts =============================
    platform win32 -- Python 3.8.0, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- G:\Python38\python.exe
    cachedir: .pytest_cache
    rootdir: E:\ProjectStudy\HDCZU_Study\Test
    plugins: forked-1.3.0, parallel-0.0.10, xdist-2.2.1
    collecting ... collected 5 items
    
    test_py18.py::test_add[1] PASSED
    test_py18.py::test_add[2] PASSED
    test_py18.py::test_add[3] PASSED
    test_py18.py::test_add[4] PASSED
    test_py18.py::test_add[5] PASSED
    
    ============================== 5 passed in 0.09s ==============================
    
    Process finished with exit code 0
    
    """
    

    读取yml文件参数化

    #coding=utf-8
    import pytest
    import yaml
    
    def add(a,b):
        return a+b
    
    
    """
    读取yaml文件参数化
    testYaml.yml内容
    - [1,2,3]
    - [4,5,9]
    - ["he","llo","hello"]
    
    读取后的结果:
    [[1, 2, 3], [4, 5, 9], ['he', 'llo', 'hello']]
    """
    with open("testYaml.yml","r") as f:
        data=f.read()
        resultList=yaml.load(data,Loader=yaml.FullLoader)
        print(resultList)
    
    @pytest.mark.parametrize(
        'a,b,result',
         resultList
    )
    
    #测试用例的传参或者引用参数名必须是上述参数化的的变量名,预期结果引用必须是上述参数化的预期结果变量名
    def test_add(a,b,result): #a,b,result接收装饰器传入的数据
        print("{0}-{1}->{2}".format(a,b,result))
        assert add(a,b)==result
    
    
    if __name__ == '__main__':
        pytest.main(["-vs","test_py18.py"])
    
    """
    platform win32 -- Python 3.8.0, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- G:\Python38\python.exe
    cachedir: .pytest_cache
    rootdir: E:\ProjectStudy\HDCZU_Study\Test
    plugins: forked-1.3.0, parallel-0.0.10, xdist-2.2.1
    collecting ... [[1, 2, 3], [4, 5, 9], ['he', 'llo', 'hello']]
    collected 3 items
    
    test_py18.py::test_add[1-2-3] 1-2->3
    PASSED
    test_py18.py::test_add[4-5-9] 4-5->9
    PASSED
    test_py18.py::test_add[he-llo-hello] he-llo->hello
    PASSED
    
    ============================== 3 passed in 0.09s ==============================
    """
    

    参数化标记参数

    参数化,标记参数:
    #coding=utf-8
    import pytest
    
    @pytest.mark.parametrize(
        'test_input,expect',
        [("3+5",8),
         ("2+4",6),
        pytest.param("6*9",42,marks=pytest.mark.xfail),
        pytest.param("6*6",36,marks=pytest.mark.skip),
        ]
    )
    def test_param(test_input,expect):
        assert eval(test_input) == expect
    
    if __name__ == '__main__':
        pytest.main(["-s","test_param.py"])
        
    """
    结果:
    collected 4 items
    
    test_param.py ..xs
    
    =================== 2 passed, 1 skipped, 1 xfailed in 0.30s ===================
    """
    

    相关文章

      网友评论

          本文标题:pytest的参数化

          本文链接:https://www.haomeiwen.com/subject/rbvmjltx.html