• 预期结果 参数化parametrize


    1、pytest.mark.parametrize装饰器可以实现测试用例参数化。

    语法:
    1). @pytest.mark.parametrize("参数名",["01_值1","02_值2"])
    2). @pytest.mark.parametrize("参1,参2",[("01_参1的值","01_参2的值"),("02_参1的值","02_参2的值")])
    注意:
    1. 参数名:
    1). 多个参数名时,参数与参数之间使用逗号分隔,但是注意所有的参数必须为一个字符串!
    2. 参数值:
    1). 单个参数名的值,格式必须为列表,如:["admin"," admin ","admin123"]
    2). 多个参数值:格式为列表,列表内嵌套元祖,如:[("第一轮参1值","第一轮参2值"),("第二轮参1值","第二轮参2值")]
    3. 调用的参数名必须和设置的参数化引用名称一样。

    2、实例:

    import pytest
    
    @pytest.mark.parametrize("req,expect",
                             [("3+5",8),
                              ("1+1",2),
                               ("8-1",7)
                             ])
    def test11(req,expect):
        assert eval(req)==expect
    
    if __name__ == '__main__':
        pytest.main(["-s","text_01.py"])

    运行结果:

    参数组合:1.若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器

    import pytest
    @pytest.mark.parametrize("x", [0, 1])
    @pytest.mark.parametrize("y", [2, 3])
    def test_foo(x, y):
        print("测试数据组合:x->%s, y->%s" % (x, y))
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_canshu1.py"])

    运行结果:

    3、有时候功能变动,用例就会失败,这种预料之类会失败的用例可以标记为忽视,暂不运行。使用内置的mark.xfai

    import pytest
    
    @pytest.mark.parametrize("req,expect",
                             [("3+5",8),
                              ("1+1",2),
                              pytest.param("6 * 9", 42, marks=pytest.mark.xfail)#用例失败,标记忽视
                             ])
    def test11(req,expect):
        assert eval(req)==expect
    
    if __name__ == '__main__':
        pytest.main(["-s","text_01.py"])

    运行结果:

  • 相关阅读:
    @pytest.mark.parametrize使用笔录
    Python常用模块/函数
    Python代码驱动facebook-Wda
    pytest简述--pytest.ini、allure、logging、assert的使用
    终端命令启动webDriverAgent
    Wda自动化环境搭建[IOS]
    windows10常用终端命令+adb shell
    Python-unittest框架+HtmlTestRunner
    appium+python Android UI自动化环境搭建(windows10)
    调用百度AI实现人脸检索
  • 原文地址:https://www.cnblogs.com/guo2733/p/10530578.html
Copyright © 2020-2023  润新知