zoukankan      html  css  js  c++  java
  • pytest 之数据驱动参数化:pytest.mark.parametrize

      在测试用例的前面加上:

    @pytest.mark.parametrize("参数名", 列表数据)  # (mark 译:马尔科、parametrize 译:普软木踹丝)

      参数名:用来接收每一项数据,并作为测试用例的参数。

      列表数据:一组测试数据,元祖、字典、列表。

    方式一:

      @pytest.mark.parametrize('参数名', [数据1, 数据2, 数据3......])

    import pytest
    
    
    @pytest.mark.parametrize('demo', [1, 2, 3, 4])
    def test_demo(demo):
        print(f'测试数据为:{demo}')
        assert demo in [0, 1, 2, 3, 4, 5]

      执行结果:

    方式二:

      @pytest.mark.parametrize("参数1,参数2", [(数据1, 数据2), (数据1, 数据2)])

    import pytest
    
    
    @pytest.mark.parametrize("a,b,c", [(1, 3, 4), (10, 35, 45), (22.22, 22.22, 44.44)])
    def test_add(a, b, c):
        res = a + b
        print(f"测试数据为:{res}")
        assert res == c

      执行结果:

    方式三:(笛卡尔积)

    • 组合参数化:多组参数,依次组合。
      • 使用多个@pytest.mark.parametrize  
    import pytest
    
    
    @pytest.mark.parametrize('test1', [1, 2])
    @pytest.mark.parametrize('test2', [3, 4])
    def test_demo(test1, test2):
        print(f'测试数据为:{test1}和{test2}')
        assert (test1, test2) in [(1, 3), (1, 4), (2, 3), (2, 4), (5, 6)]

      执行结果:

    *******请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/shouhu/,谢谢!!******* 

  • 相关阅读:
    Fence Repair(POJ 3253)
    Saruman's Army(POJ 3069)
    Best Cow Line(POJ 3617)
    一往直前!贪心法
    最基础的“穷竭搜索”
    Lake Counting(POJ 2386)
    Ants(POJ 1852)
    热身题
    分布式锁的三种实现方式
    Redis在实际开发中面临的问题
  • 原文地址:https://www.cnblogs.com/shouhu/p/12392744.html
Copyright © 2011-2022 走看看