zoukankan      html  css  js  c++  java
  • pytest之参数化parametrize的使用

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

    import pytest
    test_datas = [
        (11, 22, 33),
        (22, 33, 55)
    ]
    
    datas_dict = [
        {"a": 1, "b": 2, "c": 3},
        {"a": 11, "b": 22, "c": 33},
        {"a": 111, "b": 222, "c": 333},
    ]
    
    # 方式一:直接写
    @pytest.mark.parametrize("a, b, c", [(1, 2, 3), (4, 5, 9)])
    def test_add01(a, b, c):
        res = a + b
        assert res == c
    
    # 方式二:参数为列表中嵌套元组
    @pytest.mark.parametrize("data", test_datas)
    def test_add02(data):
        res = data[0] + data[1]
        assert res == data[2]
    
    # 方式三:参数为列表中嵌套字典
    @pytest.mark.parametrize("data", datas_dict)
    def test_add03(data):
        res = data["a"] + data["b"]
        assert res == data["c"]
    

      

  • 相关阅读:
    排序算法之归并
    RequestAndResponse
    Jsp相关
    会话技术
    MVC设计思想
    FileRecv VNCViewer 使用方法
    Go语言string包详解
    Go语言字符串
    Go语言fmt包详解
    编写第一个Go程序
  • 原文地址:https://www.cnblogs.com/lexus168/p/13776279.html
Copyright © 2011-2022 走看看