Python3-接口自動化-9-接口自動化中枚舉Enum的應用

1、Enum結構app

  • 從enum導入Enum,並讓自定義的枚舉類繼承Enum
  • 枚舉類內的標籤最好用大寫字母來表示常量
from enum import Enum, unique
 
@unique # @unique裝飾器能夠幫助咱們檢查保證沒有重複值
class StoreApproachEnum(Enum):
def __new__(cls, english, chinese, status,text):
obj = object.__new__(cls)
obj.english = english
obj.chinese = chinese
obj.status = status
obj.text = text
return obj
 
SHOWAILOCK = "showAiLock", "智能鎖", "1", "該門店有"
SHOWDELIVERHOME = "showDeliverHome", "配送到家", "1", "該門店有"
SHOWORDER = "showOrder", "訂單", "1", "該門店有"
SHOWOUTDOORBUY = "showOutDoorBuy", "自助購", "1", "該門店有"
SHOWSELFBUY = "showSelfBuy", "智能鎖", "1", "該門店有"
SHOWSELFCHECKOUT = "showSelfCheckOut", "自由購", "1", "該門店有"
SHOWSUPERHI = "showSuperHi", "起嗨", "1", "該門店有"
STATUS = "status", "啓用", "1", "該門店"

2、應用測試

2.1 打印全部標籤spa

for i in StoreApproachEnum:
print(i.name)
 
# ----------------------------
SHOWAILOCK
SHOWDELIVERHOME
SHOWORDER
SHOWOUTDOORBUY
SHOWSELFBUY
SHOWSELFCHECKOUT
SHOWSUPERHI
STATUS

 

2.2 打印枚舉值code

for i in StoreApproachEnum:
# print(i.name)
print(i.chinese)
#-----------------------
智能鎖
配送到家
訂單
自助購
智能鎖
自由購
起嗨
啓用

 

2.3 打印枚舉值orm

for i in StoreApproachEnum:
# print(i.name)
# print(i.chinese)
if i.status == "1":
print(i.text+i.chinese)
 
#---------------------
該門店有智能鎖
該門店有配送到家
該門店有訂單
該門店有自助購
該門店有智能鎖
該門店有自由購
該門店有起嗨
該門店啓用

 

 

3、接口自動化中的應用blog

3.1 枚舉定義文件py
from enum import Enum, unique
 
@unique # @unique裝飾器能夠幫助咱們檢查保證沒有重複值
class StoreApproachEnum(Enum):
def __new__(cls, english, chinese):
obj = object.__new__(cls)
obj.english = english
obj.chinese = chinese
return obj
 
SHOWAILOCK = "showAiLock", "智能鎖"
SHOWDELIVERHOME = "showDeliverHome", "配送到家"
SHOWORDER = "showOrder", "訂單"
SHOWOUTDOORBUY = "showOutDoorBuy", "自助購"
SHOWSELFBUY = "showSelfBuy", "智能鎖"
SHOWSELFCHECKOUT = "showSelfCheckOut", "自由購"
SHOWSUPERHI = "showSuperHi", "起嗨"
STATUS = "status", "門店狀態"

 

3.2 解析響應中調用
def parse_store_approach_from_homePage(resp=None):

    """
    門店配置數據解析
    :param resp:
    :return:
    """
    store_approach_dict = {}
    resCode = resp["code"]
    if resCode == "0000" and resp["data"]:
        store_approach_dict["showAiLock"] = resp["data"]["showAiLock"]
        store_approach_dict["showDeliverHome"] = resp["data"]["showDeliverHome"]
        store_approach_dict["showOrder"] = resp["data"]["showOrder"]
        store_approach_dict["showOutDoorBuy"] = resp["data"]["showOutDoorBuy"]
        store_approach_dict["showSelfBuy"] = resp["data"]["showSelfBuy"]
        store_approach_dict["showSelfCheckOut"] = resp["data"]["showSelfCheckOut"]
        store_approach_dict["showSuperHi"] = resp["data"]["showSuperHi"]
        store_approach_dict["status"] = resp["data"]["status"]
        # 拿到字典中的keys
        for i in list(store_approach_dict.keys()):
            # 拿到enum文件中的標籤
            for j in e:
                # 判斷字典中的key值若是等於enum的標籤
                if i.upper() == j.name:
                    # 刪除字典中key對應的值,並賦給新的key,再次添加到字典中
                    store_approach_dict[j.chinese] = store_approach_dict.pop(i)

                    # print(store_approach_dict)
        return store_approach_dict

    else:
        return None
3.3 測試類中調用解析響應方法
result = parse_wx_test_data.parse_store_approach_from_homePage(r_dict)
for k,v in result.items():
# print(k,v)
logger.info("{}-{}-{}-{}-{}{}".format(storeID[0],storeID[1],storeID[2],storeID[3],"啓用了" if v else "未開啓",k))
print("{}-{}-{}-{}-{}{}".format(storeID[0],storeID[1],storeID[2],storeID[3],"啓用了" if v else "未開啓",k),end=".")
print()

輸出:繼承

1-北京超市-66666-沁山水店-啓用了智能鎖.1-北京超市-66666-沁山水店-啓用了配送到家.1-北京超市-66666-沁山水店-啓用了訂單.1-北京超市-66666-沁山水店-未開啓自助購.1-北京超市-66666-沁山水店-啓用了自由購.1-北京超市-66666-沁山水店-未開啓起嗨.1-北京超市-66666-沁山水店-啓用了門店狀態.
相關文章
相關標籤/搜索