auto_test_dev/YiMao/scripts/YMService/test_06ADManagement.py

113 lines
6.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# !/usr/bin/python
# -*- coding: utf-8 -*-
import unittest,allure,ddt,time,json,requests,datetime
from datetime import datetime,timedelta
from configs.globalObj import LOGGER
from configs.globalParams import *
from YiMao.config.ServiceApiInfo import *
from commons.FileHandler import ReadExcel,Txt
from commons.AssertLib import _assert_result
ExlFilePath = GlobalPath['YMDataPath'] + '/YMHT_ApiTestCase_0.0.1.xlsx'
ExcelContent = ReadExcel().get_data(FilePath=ExlFilePath, SheetName='06-广告管理')
@ddt.ddt
class Test_ADManagement_Module(unittest.TestCase):
@allure.story("管理后台:广告管理模块")
@ddt.data(*ExcelContent)
def test_ad_management(self, data):
'''@Date:: 2024/5/20
@Desc::
管理后台 -> 广告管理模块接口
'''
''' step01- 前置数据、接口信息描述等 '''
allure.dynamic.title(data['CaseName'])
allure.dynamic.description("描述:广告管理 模块接口自动化测试")
caseModule_c = data['BusinessModule'] # 用例编号
caseNO_c = data['NO.'] # 用例编号
caseName_c = data['CaseName'] # 用例名称
requestType_c = str(data['RequestType']) # 请求方式
expStaCode_c = data['ExpStatusCode'] # 预期的状态码
expRespJson_c = eval(data['RespJson']) # 预期的返回体
LOGGER.info(f">>> 开始执行->{caseModule_c}接口自动化测试 <<<\n")
LOGGER.info(f"---用例编号为:{caseNO_c}")
LOGGER.info(f"---用例名称为:{caseName_c}")
LOGGER.info(f"---请求方式为:{requestType_c}")
# LOGGER.info(f"---请求Header为{HEADERS}")
LOGGER.info(f"---预期接口返回码为:{expStaCode_c}")
LOGGER.info(f"---预期返回体为:{expRespJson_c}\n")
''' step02- 请求接口 '''
if requestType_c == 'POST':
# 新建、查询、复制
timeStamp = str(int(time.time()))
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
LOGGER.info(f"---请求接口为:{api_url}")
payload_c = json.dumps(eval(data['Payload']), indent=4, ensure_ascii=False) # 请求体为json格式
LOGGER.info(f"---请求体为:{payload_c}\n")
response = requests.request(method=requestType_c, url=api_url, headers=HEADERS, data=payload_c)
rspJson = response.json()
LOGGER.info(f"---接口返回状态码为:: {response.status_code}")
LOGGER.info(f"---接口返回体为:: {json.dumps(response.json(), indent=4, ensure_ascii=False)}\n")
if "查询广告" not in caseName_c:
if "草稿" in caseName_c or "复制" in caseName_c:
ActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("06_ADDrawID.txt", str(ActID))
if not writeResult:
LOGGER.error(f"---写入 广告管理(新建)ID 失败!")
else:
ActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("06_CreateADID.txt", str(ActID))
if not writeResult:
LOGGER.error(f"---写入 广告管理(草稿)ID 失败!")
else:
# 编辑、删除
if "修改广告" in caseName_c:
# 取一个已发布的广告ID
ActIDList = Txt().read_txt("06_CreateADID.txt")
ActID = ActIDList[0]
LOGGER.info(f"---存为草稿的 广告管理ID 为:{ActID}")
if ActID not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
last_api_url = api_url.replace("{ADsID}", ActID)
LOGGER.info(f"---请求接口为:{last_api_url}")
payload_c = json.dumps(eval(data['Payload']), indent=4, ensure_ascii=False) # 请求体为json格式
LOGGER.info(f"---请求体为:{payload_c}\n")
time.sleep(0.5)
response = requests.request(method=requestType_c, url=last_api_url, headers=HEADERS, data=payload_c)
rspJson = response.json()
LOGGER.info(f"---接口返回状态码为:: {response.status_code}")
LOGGER.info(f"---接口返回体为:: {json.dumps(response.json(), indent=4, ensure_ascii=False)}\n")
else:
if requestType_c == "PUT":
ActIDList = Txt().read_txt("06_CreateADID.txt")
LOGGER.info(f"---新建并发布的 广告管理ID list为{ActIDList}")
else:
ActIDList = (Txt().read_txt("06_CreateADID.txt") +
Txt().read_txt("06_ADDrawID.txt"))
LOGGER.info(f"---存为草稿&复制的 广告管理ID list为{ActIDList}")
for i in range(len(ActIDList)):
# 暂停、重启、结束、删除
ActId = ActIDList[i]
if ActId not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
last_api_url = api_url.replace("{ADsID}", str(ActId))
LOGGER.info(f"---请求接口为:{last_api_url}")
payload_c = json.dumps(eval(data['Payload']), indent=4, ensure_ascii=False) # 请求体为json格式
LOGGER.info(f"---请求体为:{payload_c}\n")
time.sleep(0.5)
response = requests.request(method=requestType_c, url=last_api_url, headers=HEADERS,
data=payload_c)
rspJson = response.json()
LOGGER.info(f"---接口返回状态码为:: {response.status_code}")
LOGGER.info(f"---接口返回体为:: {json.dumps(response.json(), indent=4, ensure_ascii=False)}\n")
''' step03- 断言处理 '''
assert _assert_result(rspJson, expRespJson_c, LOGGER) == True, AssertionError(f"---断言失败!")
LOGGER.info(f">>> {caseModule_c}接口请求 & 断言执行结束 <<<\n\n")
if __name__ == '__main__':
unittest.main(verbosity=2)