• pytest-参数化


    参数化

    pytest使用parametrize标记实现参数化

    @pytest.mark.parametrize(argnames,argvalues)
    
    parametrize()的第一个参数时用逗号分割的的字符串列表,第二个参数时一个值列表
    
    #!/usr/bin/python3
    #-*- conding:utf-8 -*-
    import pytest 
    import sys
    
    @pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)])
    def test_one(a,b):
        assert a == b
    
    ==================================== test session starts =====================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
    rootdir: /media/_dde_data/python
    collected 3 items                                                                            
    
    test_001.py FF.                                                                        [100%]
    
    ========================================== FAILURES ==========================================
    _______________________________________ test_one[1-2] ________________________________________
    
    a = 1, b = 2
    
        @pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)])
        def test_one(a,b):
    >       assert a == b
    E       assert 1 == 2
    
    test_001.py:8: AssertionError
    _______________________________________ test_one[a-b] ________________________________________
    
    a = 'a', b = 'b'
    
        @pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)])
        def test_one(a,b):
    >       assert a == b
    E       AssertionError: assert 'a' == 'b'
    E         - b
    E         + a
    
    test_001.py:8: AssertionError
    ================================== short test summary info ===================================
    FAILED test_001.py::test_one[1-2] - assert 1 == 2
    FAILED test_001.py::test_one[a-b] - AssertionError: assert 'a' == 'b'
    ================================ 2 failed, 1 passed in 0.20s =================================
    

    ids
    参数化装饰器有一个额外的参数ids,可以标识每一个测试用例,自定义测试数据结果的显示,为了增加可读性,我们可以标记每一个测试用例使用的测试数据是什么,适当的增加一些说明

    #!/usr/bin/python3
    #-*- conding:utf-8 -*-
    import pytest 
    import sys
    
    @pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)],ids=["1 != 2","a != b","False = False"])
    def test_one(a,b):
        assert a == b
    
    ==================================== test session starts =====================================
    platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
    cachedir: .pytest_cache
    rootdir: /media/_dde_data/python
    collected 3 items                                                                            
    
    test_001.py::test_one[1 != 2] FAILED                                                   [ 33%]
    test_001.py::test_one[a != b] FAILED                                                   [ 66%]
    test_001.py::test_one[False = False] PASSED                                            [100%]
    
    ========================================== FAILURES ==========================================
    ______________________________________ test_one[1 != 2] ______________________________________
    
    a = 1, b = 2
    
        @pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)],ids=["1 != 2","a != b","False = False"])
        def test_one(a,b):
    >       assert a == b
    E       assert 1 == 2
    E         +1
    E         -2
    
    test_001.py:8: AssertionError
    ______________________________________ test_one[a != b] ______________________________________
    
    a = 'a', b = 'b'
    
        @pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)],ids=["1 != 2","a != b","False = False"])
        def test_one(a,b):
    >       assert a == b
    E       AssertionError: assert 'a' == 'b'
    E         - b
    E         + a
    
    test_001.py:8: AssertionError
    ================================== short test summary info ===================================
    FAILED test_001.py::test_one[1 != 2] - assert 1 == 2
    FAILED test_001.py::test_one[a != b] - AssertionError: assert 'a' == 'b'
    ================================ 2 failed, 1 passed in 0.21s =================================
    
  • 相关阅读:
    wince串口打印信息的屏蔽与打开
    2440 6.0BSP 移植过程之OAL
    2440 6.0BSP移植过程之电池驱动
    2440 6.0BSP移植过程之SD卡驱动
    如何在wince6.0 模拟器上跑以前编译好的EVC MFC程序
    2440 休眠唤醒的实现过程(作者:wogoyixikexie@gliet)
    CETK的使用(USB连接方式)
    如何扩展ARM内存(OEMGetExtensionDRAM和pNKEnumExtensionDRAM函数可以解决问题)
    ZLG7290(wince下)驱动之不停执行同一动作的解决办法(作者:wogoyixikexie@gliet)
    2440 中断优先级问题(作者wogoyixikexie@gliet)
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13348226.html
Copyright © 2020-2023  润新知