pytest 裝飾器@pytest.mark.parametrize參數的參數化注意事項

總結:pytest 裝飾器@pytest.mark.parametrize 的實參建議使用列表list或元組tuple,若使用字典dict則需被元組或列表嵌套,詳情請往下看。

  • pytest 裝飾器@pytest.mark.parametrize 單一參數的參數化
  1. 支持dict 列表數據類型作爲實參,可傳入多條測試數據;
  2. 支持tuple 元組數據類型作爲實參,可傳入多條測試數據;
  3. 支持set 集合數據類型作爲實參,可傳入多條測試數據;
  4. 支持dict 字典數據類型作爲實參,但是不適合,因爲只輸出字典的key,不建議直接用字典作爲測試數據傳入
單一參數的參數化示例
import pytest


# # 使用裝飾器參數化,支持列表,左邊的"data" 是形參,右邊的數據是實參
# @pytest.mark.parametrize("data", ['a', 'b', 'c'])
# def test_singleParam_list(data):
#     print(data)
#
#
# # 使用裝飾器參數化,支持元組
# @pytest.mark.parametrize('data',(1, 2, 3, 4))
# def test_singleParam_tuple(data):
#     print(data)


# # 使用裝飾器參數化,支持dict 字典數據類型。但是不適合因爲只輸出字典的key
# # @pytest.mark.parametrize('data', {'a': 1, 'b': 2, 'c': 3})
# @pytest.mark.parametrize('data', {1: 'a', 2: 'b', 3: 'c'})
# def test_singleParam_dict(data):
#     print(data)
#     print(type(data))


# 使用裝飾器參數化,支持set 集合
@pytest.mark.parametrize('data', {1, 'v', 'd', 2})
def test_singleParam_set(data):
    print(data)


if __name__ == '__main__':
    pytest.main(["-sv", "./test_single_param.py"])
  •  pytest 裝飾器@pytest.mark.parametrize 多參數的參數化
  1. 裝飾器@pytest.mark.parametrize的形參可以一個,它可以自動解析實參返回的參數個數
  2. 多參數的參數化,需要用到數據類型的嵌套

元組tuple 支持嵌套元組tuple、列表list、集合set、字典dict;

列表list 支持嵌套列表list、元組tuple、集合set、字典dict;

集合set 支持嵌套元組tuple,不支持集合set、列表list、集合set,會報錯:TypeError: unhashable type;

字典dict 支持嵌套tuple、list、set、dict,但是輸出Key,不適合用在此處。

多參數的參數化示例
import pytest


# 元組tuple嵌套各種容器:list, tuple, set, dict
# # 元組tuple 嵌套列表list
# def return_tuple_dataList():
#     # 默認返回元組tuple
#     return ['aa', 123], ['bb', 234], ['cc', 345]
#
#
# # 使用函數返回的元組(元組tuple 嵌套列表list數據)傳入參數
# @pytest.mark.parametrize('data', return_tuple_dataList())
# def test_multipleParam_tuple_dataList(data):
#     # 循環獲取元組中的記錄,一次取一個列表的記錄
#     print("\n"+data[0])
#     print(str(data[1]))
#
#
# # 元組tuple 嵌套元組tuple
# def return_tuple_dataTuple():
#     # 默認返回元組tuple
#     return ('aa', 123), ('bb', 234), ('cc', 345)
#
#
# # 使用函數返回的元組(元組tuple 嵌套元組tuple)傳入參數
# @pytest.mark.parametrize('data', return_tuple_dataTuple())
# def test_multipleParam_tuple_dataTuple(data):
#     # 循環獲取元組中的記錄,一次取一個元組的記錄
#     print("\n"+data[0])
#     print(str(data[1]))
#
#
# # 元組tuple 嵌套集合set
# def return_tuple_dataSet():
#     # 默認返回元組tuple
#     return {'aa', 123}, {'bb', 234}, {'cc', 345}
#
#
# # 使用函數返回的元組(元組tuple 嵌套集合set)傳入參數
# @pytest.mark.parametrize('data', return_tuple_dataSet())
# def test_multipleParam_tuple_dataSet(data):
#     # 循環獲取元組中的記錄,一次取一個集合的記錄,集合是無序的不能通過索引取值
#     print("\n")
#     print(data)
#     for val in data:
#         print(val)


# # 集合set 嵌套元組tuple
# def return_set_dataTuple():
#     # 返回集合set
#     return {('dd', 123), ('ee', 234), ('gg', 345)}
#
#
# # 使用函數返回的集合set(集合set 嵌套元組tuple)數據傳入參數
# @pytest.mark.parametrize("data", return_set_dataTuple())
# def test_multipleParam_set_dataTuple(data):
#     # 循環獲取集合中的記錄,一次取一個元組記錄
#     print("\n"+data[0])
#     print(str(data[1]))


# # 集合set 不支持嵌套集合set
# def return_set_dataSet():
#     # 返回集合set
#     return {{'dd', 123}, {'ee', 234}, {'gg', 345}}
#
#
# # 集合set 不支持嵌套列表list
# def return_set_dataList():
#     # 返回集合set
#     return {['dd', 123], ['ee', 234], ['gg', 345]}
#
#
# # 集合set 不支持嵌套字典dict
# def return_set_dataDict():
#     # 返回集合set
#     return {{'name': 'dd', 'password': 123}, {'name': 'ee', 'password': 234}, {'name': 'gg', 'password': 345}}

#
# # 列表list 嵌套list
# def return_list_dataList():
#     # 返回列表list
#     return [['hh', 123], ['ii', 234], ['jj', 345]]
#
#
# @pytest.mark.parametrize('data', return_list_dataList())
# def test_multipleParam_list_dataList(data):
#     # 循環獲取列表中的記錄,一次取一個列表記錄
#     print("\n"+data[0])
#     print(data[1])
#
#
# # 列表list 嵌套元組tuple
# def return_list_dataTuple():
#     # 返回列表list
#     return [('hh', 123), ('ii', 234), ('jj', 345)]
#
#
# @pytest.mark.parametrize('data', return_list_dataTuple())
# def test_multipleParam_list_dataTuple(data):
#     # 循環獲取列表中的記錄,一次取一個元組記錄
#     print("\n"+data[0])
#     print(data[1])
#
#
# # 列表list 嵌套集合set
# def return_list_dataSet():
#     # 返回列表list
#     return [('hh', 123), ('ii', 234), ('jj', 345)]
#
#
# @pytest.mark.parametrize('data', return_list_dataSet())
# def test_multipleParam_list_dataSet(data):
#     # 循環獲取列表中的記錄,一次取一個列表記錄
#     print("\n"+data[0])
#     print(data[1])
#
#
# # 列表list 嵌套字典dict
# def return_list_dataDict():
#     # 返回列表list
#     return [{'name': 'hh', 'password': 123}, {'name': 'ii', 'password': 234}, {'name': 'jj', 'password': 345}]
#
#
# # 使用函數返回的列表list(列表list 嵌套字典dict)數據傳入參數
# @pytest.mark.parametrize('data', return_list_dataDict())
# def test_multipleParam_list_dataDict(data):
#     # 循環獲取列表中的記錄,一次取一個字典記錄
#     print("\n"+data['name'])
#     print(data['password'])


# 字典dict 嵌套字典dict
def return_dict_dataDict():
    # 返回列表list
    return {"user": {'name': 'hh', 'password': 123}, "info": {'name': 'ii', 'password': 234}, "msg": {'name': 'jj', 'password': 345}}


# 字典嵌套其他數據類型不適合作爲裝飾器的多參數的實參
# 使用函數返回的列表list(列表list 嵌套字典dict)數據傳入參數
@pytest.mark.parametrize('data', return_dict_dataDict())
def test_multipleParam_dict_dataDict(data):
    # 循環獲取字典中的記錄,一次取一個字典記錄
    print(data)     # 輸出字典的key
    print(type(data))


# 字典dict 嵌套列表list
def return_dict_dataList():
    # 返回列表list
    return {"user": ['hh', 123], "info": ['ii', 234], "msg": ['jj', 345]}


@pytest.mark.parametrize('data', return_dict_dataList())
def test_multipleParam_dict_dataList(data):
    # 循環獲取字典中的記錄,一次取一個字典記錄
    print(data)     # 輸出字典的key
    print(type(data))


if __name__ == '__main__':
    # -vs 展示打印內容和執行詳情
    pytest.main(["-vs", "./test_func2.py"])

 

能力有限,若有錯誤,歡迎各位大佬指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章