zoukankan      html  css  js  c++  java
  • pytest之@pytest.mark.parametrize实例讲解

    在测试用例的前面加上:
    @pytest.mark.parametrize("参数名",列表数据)
    参数名:用来接收每一项数据,并作为测试用例的参数。
    列表数据:一组测试数据。

    #====参数为列表嵌套字典====
    import pytest
    
    def secend(c,b):
        add= int(b)+int(c)
        print(add)
        return add
    
    @pytest.mark.parametrize('case',[{'b':'34','c':'56'},{'b':'39','c':'56'}])
    def test_1(case):
        pp=case
        tt=secend(**case)
        print("测试账号:%s" % tt)
    
    
    if __name__ == "__main__":
        pytest.main(["-s", "1.py"])
    

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.5.4, pytest-5.1.3, py-1.7.0, pluggy-0.13.1
    rootdir: C:Usersheguanghuapackageuntitled_web
    plugins: allure-pytest-2.8.5, PyTestReport-0.2.1, forked-1.0.2, html-1.22.0, metadata-1.7.0, rerunfailures-7.0, xdist-1.28.0
    collected 2 items
    
    1.py 90
    测试账号:90
    .95
    测试账号:95
    .
    
    ============================== 2 passed in 0.05s ==============================
    
    #====参数为列表嵌套元组====
    import pytest
    test_datas = [
        (11, 22, 33),
        (22, 33, 55)
    ]
    
    @pytest.mark.parametrize("data", test_datas)
    def test_add02(data):
        res = data[0] + data[1]
        print(res)
        assert res == data[2]
    
    if __name__=="__main__":
        pytest.main(['-s','1.py'])
    

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.5.4, pytest-5.1.3, py-1.7.0, pluggy-0.13.1
    rootdir: C:Usersheguanghuapackageuntitled_web
    plugins: allure-pytest-2.8.5, PyTestReport-0.2.1, forked-1.0.2, html-1.22.0, metadata-1.7.0, rerunfailures-7.0, xdist-1.28.0
    collected 2 items
    
    1.py 33
    .55
    .
    
    ============================== 2 passed in 0.05s ==============================
    
    #====参数为列表嵌套列表====
    import pytest
    test_datas = [
        [11, 22, 33],
        [22, 33, 55]
    ]
    
    @pytest.mark.parametrize("data", test_datas)
    def test_add02(data):
        res = data[0] + data[1]
        print(res)
        assert res == data[2]
    
    if __name__=="__main__":
        pytest.main(['-s','1.py'])
    

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.5.4, pytest-5.1.3, py-1.7.0, pluggy-0.13.1
    rootdir: C:Usersheguanghuapackageuntitled_web
    plugins: allure-pytest-2.8.5, PyTestReport-0.2.1, forked-1.0.2, html-1.22.0, metadata-1.7.0, rerunfailures-7.0, xdist-1.28.0
    collected 2 items
    
    1.py 33
    .55
    .
    
    ============================== 2 passed in 0.05s ==============================
    
  • 相关阅读:
    网页加载进度条
    【转载】通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
    前端知识复习:Html DIV 图文混排(文字放在图片下边)
    NOPI导出Excel
    C# 发送邮件
    DataSetToList 和 DataTableTolist 转换
    一个修改版的PHP ajax Tree树形菜单
    你的站为什么百度无动于衷——10年老站长的SEO肺腑之言 .
    关于php使用phpqrcode生成二维码的完整源码下载
    php结合phpqrcode生成带图片LOGO的二维码
  • 原文地址:https://www.cnblogs.com/hghua/p/13266380.html
Copyright © 2011-2022 走看看