首页 > 其他 > 详细

Pytest学习7-参数化

时间:2020-01-08 23:23:13      阅读:87      评论:0      收藏:0      [点我收藏+]

在测试过程中,参数化是必不可少的功能,本文就讨论下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次组合结果,如图:

技术分享图片

Pytest学习7-参数化

原文:https://www.cnblogs.com/qixc/p/12168734.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!