Pytest学习7-参数化

在测试过程中,参数化是必不可少的功能,本文就讨论下pytest的几种参数化方法

@pytest.mark.parametrize:参数化测试函数

     1.内置的pytest.mark.parametrize装饰器支持测试函数的参数化基本用法
     例如:
     @pytest.mark.parametrize("input,expect",[("3+5",8),("5+5",9),("4+9",12),("10+21",31)])
     def test_param(input,expect):
            assert eval(input)==expect
      结果:成功2个失败2个,但是需要注意的是,@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42)])里面的"test_input,expected"一定要和test_add(test_input,expected)当中的参数名称一致,否则,将会出错。
      
      2.它也可以标记单个测试实例在参数化,例如使用内置的mark.xfail  
        @pytest.mark.parametrize("input,expect",[("3+5",8),("5+5",9),("4+9",12),pytest.param("10+21",31,marks=pytest.mark.xfail)])
        def test_param(input,expect):
            assert eval(input)==expect

       3.参数组合测试:
        @pytest.mark.parametrize("x",[1,2,3,4])
        @pytest.mark.parametrize("y",[3,4])
        def test_param(x,y):
            print("参数组合%s,******,%s"%(x,y))
        结果会测试8次组合结果,如图:

猜你喜欢

转载自www.cnblogs.com/qixc/p/12168734.html