auto_test_dev/YiMao/scripts/test_03SampleDistributionActivity.py
2024-11-11 17:16:31 +08:00

111 lines
6.0 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.ApiInfo import *
from commons.FileHandler import ReadExcel,Txt
from commons.PgSqlLib import PGSQL
from commons.AssertLib import _assert_result
ExlFilePath = GlobalPath['YMDataPath'] + '/YMHT_ApiTestCase_0.0.1.xlsx'
ExcelContent = ReadExcel().get_data(FilePath=ExlFilePath, SheetName='03-派样活动')
@ddt.ddt
class Test_SampleDistributionActivity_Module(unittest.TestCase):
@allure.story('[管理平台] 派样活动模块')
@ddt.data(*ExcelContent)
def test_sample_distribution_activity(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- 请求接口
执行复制接口时,存储对应 rspJson["data"]["id"],为删除接口使用
'''
if data['BusinessModule'] in ["新建派样活动", "复制派样活动", "查询派样活动"]:
# 新建、查询、复制
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[data['BusinessModule']] # 请求地址
LOGGER.info(f"---请求接口为:{api_url}")
# LOGGER.info(f"---处理前,请求体为:{data['Payload']}\n")
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")
time.sleep(0.5)
if data['BusinessModule'] == "新建派样活动":
if "存为草稿" in caseName_c:
SampleActivityID = rspJson["data"]['id']
writeResult = Txt().append_write_txt(
"03_DraftOfActivityID.txt", str(SampleActivityID)
)
if not writeResult:
LOGGER.error(f"---写入 派样活动(新建)ID 失败!")
else:
SampleActivityID = rspJson["data"]['id']
writeResult = Txt().append_write_txt(
"03_CreateSampleActivityID.txt", str(SampleActivityID)
)
if not writeResult:
LOGGER.error(f"---写入 派样活动(草稿)ID 失败!")
elif data['BusinessModule'] == "复制派样活动":
SampleActivityID = rspJson["data"]['id']
# writeResult = Txt().append_write_txt("03_CopySampleActivityID.txt", str(SampleActivityID))
writeResult = Txt().append_write_txt(
"03_CreateSampleActivityID.txt", str(SampleActivityID)
)
if not writeResult:
LOGGER.error(f"---写入 派样活动(复制)ID 失败!")
else:
sampleActIDList = Txt().read_txt("03_CreateSampleActivityID.txt")
LOGGER.info(f"---新建并发布的派样活动ID list为{sampleActIDList}")
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[data['BusinessModule']] # 请求地址
last_api_url = api_url.replace("{SampleActivityID}", str(sampleActIDList[0]))
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")
response = requests.request(method=requestType_c, url=last_api_url, headers=HEADERS, data=payload_c)
rspJson = response.json()
rspJsonFormat = json.dumps(response.json(), indent=4, ensure_ascii=False)
LOGGER.info(f"---接口返回状态码为:: {response.status_code}")
LOGGER.info(f"---接口返回体为:: {rspJsonFormat}\n")
''' step03- 断言处理 '''
assert _assert_result(rspJson, expRespJson_c, LOGGER) == True, f"---断言失败!"
LOGGER.info(f">>> {data['BusinessModule']}接口请求 & 断言执行结束 <<<\n\n")
@classmethod
def tearDownClass(cls):
LOGGER.info(f">>> 派样活动模块接口自动化测试所有用例执行完毕 <<<\n")
LOGGER.info(f">>> 开始执行清理-派样活动模块-测试数据 <<<\n")
sampActIDList = (Txt().read_txt("03_CreateSampleActivityID.txt") +
Txt().read_txt("03_DraftOfActivityID.txt"))
LOGGER.info(f"---待删除的派样活动ID list为{sampActIDList}")
PGSQL().del_sample_activity_data(sampActIDList)
LOGGER.info(f">>> 清理-派样活动模块-测试数据完毕 <<<\n")
if __name__ == '__main__':
unittest.main(verbosity=2)