Arthur-Wu committed this file on 2024-11-15

This commit is contained in:
ieemoo
2024-11-15 17:35:57 +08:00
parent c871e12b16
commit 298268c133
20 changed files with 595 additions and 18 deletions

View File

@ -0,0 +1,5 @@
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @Author:: Arthur Wu
# @Date:: 2024/11/15-16:28
# @Description::

View File

@ -0,0 +1,61 @@
# !/usr/bin/python
# -*- coding: utf-8 -*-
import unittest,allure,ddt,time,json,requests
from configs.globalObj import LOGGER
from configs.globalParams import *
from YiMao.config.ServiceApiInfo import *
from commons.FileHandler import ReadExcel
from commons.AssertLib import _assert_result
ExlFilePath = GlobalPath['YMDataPath'] + '/YMHT_ApiTestCase_0.0.1.xlsx'
ExcelContent = ReadExcel().get_data(FilePath=ExlFilePath, SheetName='01-超市管理')
@ddt.ddt
class Test_MarketManagementModule(unittest.TestCase):
def setUp(self) -> None:
self.timestamp = int(time.time())
@allure.story('[管理平台] 超市管理模块')
@ddt.data(*ExcelContent)
def test_market_system_parameters(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- 请求接口 '''
time.sleep(1)
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[data['BusinessModule']] + "/64" # 请求地址
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()
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")
if __name__ == '__main__':
unittest.main(verbosity=2)

View File

@ -0,0 +1,145 @@
# !/usr/bin/python
# -*- coding: utf-8 -*-
import unittest,allure,ddt,time,json,requests
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='02-门店管理')
@ddt.ddt
class Test_StoreManagementModule(unittest.TestCase):
def setUp(self) -> None:
self.timestamp = int(time.time())
@allure.story('[管理平台] 门店管理模块')
@ddt.data(*ExcelContent)
def test_store_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 data['BusinessModule'] == '删除门店系统参数':
time.sleep(0.5)
sysSettingID = 97
storeId = 65
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[data['BusinessModule']] # 请求地址
last_api_url = api_url.replace("{StoreID}", str(sysSettingID))
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")
elif data['BusinessModule'] == '修改与删除促销商品白名单':
GoodsWhiteListIDList = Txt().read_txt("GoodsWhiteListID.txt")
LOGGER.info(f"---促销商品白名单ID列表为{GoodsWhiteListIDList}")
if requestType_c == "DELETE":
for i in range(len(GoodsWhiteListIDList)):
if GoodsWhiteListIDList[i] not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[
data['BusinessModule']] # 请求地址
last_api_url = api_url.replace("{GoodsWhiteListID}", str(GoodsWhiteListIDList[i]))
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(1)
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")
else:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[data['BusinessModule']] # 请求地址
last_api_url = api_url.replace("{GoodsWhiteListID}", str(GoodsWhiteListIDList[-2]))
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")
elif data['BusinessModule'] == '修改与删除重量放通白名单':
WeightWhiteListIDList = Txt().read_txt("WeightWhiteListID.txt")
LOGGER.info(f"---删除重量放通白名单ID列表为{WeightWhiteListIDList}")
if requestType_c == "DELETE":
for j in range(len(WeightWhiteListIDList)):
if WeightWhiteListIDList[j] not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[
data['BusinessModule']] # 请求地址
last_api_url2 = api_url.replace("{WeightWhiteListID}", str(WeightWhiteListIDList[j]))
LOGGER.info(f"---请求接口为:{last_api_url2}")
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_url2, headers=HEADERS, data=payload_c)
rspJson = response.json()
rspJsonFormat = json.dumps(response.json(), indent=4, ensure_ascii=False)
LOGGER.info(f"---删除第 {j+1} 条数据,接口返回状态码为:: {response.status_code}")
LOGGER.info(f"---第 {j+1} 条数据,接口返回体为:: {rspJsonFormat}\n")
else:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[data['BusinessModule']] # 请求地址
last_api_url = api_url.replace("{WeightWhiteListID}", str(WeightWhiteListIDList[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")
else:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[data['BusinessModule']] # 请求地址
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()
rspJsonFormat = json.dumps(response.json(), indent=4, ensure_ascii=False)
LOGGER.info(f"---接口返回状态码为:: {response.status_code}")
LOGGER.info(f"---接口返回体为:: {rspJsonFormat}\n")
if data['BusinessModule'] == '新建促销商品白名单':
time.sleep(0.5)
goodsWhiteListID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("GoodsWhiteListID.txt", str(goodsWhiteListID))
if not writeResult:
LOGGER.error(f"---写入 新建促销商品白名单ID 失败!")
elif data['BusinessModule'] == '新建重量放通白名单':
time.sleep(0.5)
weightWhiteListID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("WeightWhiteListID.txt", str(weightWhiteListID))
if not writeResult:
LOGGER.error(f"---写入 新建重量放通白名单ID 失败!")
''' step03: 断言处理 '''
assert _assert_result(rspJson, expRespJson_c, LOGGER) == True, f"---断言失败!"
LOGGER.info(f">>> {data['BusinessModule']}接口请求 & 断言执行结束 <<<\n\n")
if __name__ == '__main__':
unittest.main(verbosity=2)

View File

@ -0,0 +1,110 @@
# !/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.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)

View File

@ -0,0 +1,112 @@
# !/usr/bin/python
# -*- coding: utf-8 -*-
import unittest,allure,ddt,json,requests
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='04-抽奖活动')
@ddt.ddt
class Test_LotteryDraw_Module(unittest.TestCase):
@allure.story("[管理后台] 抽奖活动模块")
@ddt.data(*ExcelContent)
def test_lottery_draw(self, data):
'''@Date:: 2024/10/25
@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 = {}
if caseModule_c in ["新建抽奖活动", "查询抽奖活动"]:
# 新建、查询
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 caseModule_c == "新建抽奖活动":
if "存为草稿" in caseName_c:
LottreyActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("04_LottreyDrawID.txt", str(LottreyActID))
if not writeResult:
LOGGER.error(f"---写入 抽奖活动(新建)ID 失败!")
else:
LottreyActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("04_CreateLottreyActID.txt", str(LottreyActID))
if not writeResult:
LOGGER.error(f"---写入 抽奖活动(草稿)ID 失败!")
else:
# 编辑抽奖活动状态
if "存为草稿" in caseName_c:
lotteryActIDList = Txt().read_txt("04_LottreyDrawID.txt")
LOGGER.info(f"---新建抽奖活动 存为草稿的活动ID list为{lotteryActIDList}")
elif requestType_c == "DELETE":
lotteryActIDList = (Txt().read_txt("04_CreateLottreyActID.txt") +
Txt().read_txt("04_LottreyDrawID.txt"))
LOGGER.info(f"---新建并发布的抽奖活动ID list为{lotteryActIDList}")
else:
lotteryActIDList = Txt().read_txt("04_CreateLottreyActID.txt")
LOGGER.info(f"---新建并发布的抽奖活动ID list为{lotteryActIDList}")
if requestType_c == "DELETE" or caseModule_c == "编辑抽奖活动状态":
# 编辑抽奖活动状态、删除抽奖活动
for i in range(len(lotteryActIDList)):
lotteryId = lotteryActIDList[i]
if lotteryId not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
last_api_url = api_url.replace("{LotteryActID}", str(lotteryId))
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:
# 编辑抽奖活动、复制抽奖活动
lotteryId = lotteryActIDList[0]
if lotteryId not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
last_api_url = api_url.replace("{LotteryActID}", str(lotteryId))
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, AssertionError(f"---断言失败!")
LOGGER.info(f">>> {caseModule_c}接口请求 & 断言执行结束 <<<\n\n")
if __name__ == '__main__':
unittest.main(verbosity=2)

View File

@ -0,0 +1,126 @@
# !/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.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='05-优惠券活动')
@ddt.ddt
class Test_CouponPromotion_Module(unittest.TestCase):
def setUp(self) -> None:
self.timestamp = int(time.time())
@allure.story("管理后台:优惠券活动模块")
@ddt.data(*ExcelContent)
def test_coupon_promotion(self, data):
'''@Date:: 2024/5/20
@Desc::
管理后台 -> 优惠券活动模块接口
'''
LOGGER.info(f"---用例data信息为{data}")
''' 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":
# 新建、查询
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 caseModule_c == "新建优惠券活动":
if "存为草稿" in caseName_c:
CouponActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("05_CouponDrawID.txt", str(CouponActID))
if not writeResult:
LOGGER.error(f"---写入 优惠券活动(新建)ID 失败!")
elif "复制" in caseName_c:
CouponActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("05_CopyCouponActID.txt", str(CouponActID))
if not writeResult:
LOGGER.error(f"---写入 优惠券活动(新建)ID 失败!")
else:
CouponActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("05_CreateCouponActID.txt", str(CouponActID))
if not writeResult:
LOGGER.error(f"---写入 优惠券活动(草稿)ID 失败!")
elif caseName_c == "修改并发布优惠券活动":
ActIDList = (Txt().read_txt("05_CreateCouponActID.txt"))
LOGGER.info(f"---新建并发布的 优惠券活动ID list为{ActIDList}")
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
last_api_url = api_url.replace("{CouponID}", ActIDList[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")
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("05_CreateCouponActID.txt"))
LOGGER.info(f"---新建并发布的 优惠券活动ID list为{ActIDList}")
else:
ActIDList = (Txt().read_txt("05_CopyCouponActID.txt") +
Txt().read_txt("05_CouponDrawID.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("{CouponID}", 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")
@classmethod
def tearDownClass(cls):
LOGGER.info(f">>> 优惠券活动模块接口自动化测试所有用例执行完毕 <<<\n")
LOGGER.info(f">>> 开始执行清理-优惠券活动模块-测试数据 <<<\n")
ActIDList = (Txt().read_txt("05_CreateCouponActID.txt") +
Txt().read_txt("05_CouponDrawID.txt") +
Txt().read_txt("05_CopyCouponActID.txt")) #
LOGGER.info(f"---待删除的优惠券活动ID list为{ActIDList}")
res = PGSQL().del_coupon_activity_data(ActIDList) #
LOGGER.info(f"---清除自动化测试数据执行结果为:{res}")
LOGGER.info(f">>> 清理-优惠券活动模块-测试数据完毕 <<<\n")
if __name__ == '__main__':
unittest.main(verbosity=2)

View File

@ -0,0 +1,112 @@
# !/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)

View File

@ -0,0 +1,100 @@
# !/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='07-版本管理')
@ddt.ddt
class Test_VersionManagement_Module(unittest.TestCase):
@allure.story("管理后台:版本管理模块")
@ddt.data(*ExcelContent)
def test_version_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':
# 新建、查询
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:
ActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("07_CreateVersionManagementID.txt", str(ActID))
if not writeResult:
LOGGER.error(f"---写入 版本管理(草稿)ID 失败!")
else:
if requestType_c == "PUT":
ActIDList = Txt().read_txt("07_CreateVersionManagementID.txt")
LOGGER.info(f"---新建并发布的 版本管理ID list为{ActIDList}")
ActID = ActIDList[0]
if ActID not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
last_api_url = api_url.replace("{VersionID}", 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:
ActIDList = Txt().read_txt("07_CreateVersionManagementID.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("{VersionID}", 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)

View File

@ -0,0 +1,106 @@
# !/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'
Content = ReadExcel().get_data(
FilePath=GlobalPath['YMDataPath'] + '/YMHT_ApiTestCase_0.0.1.xlsx',
SheetName='08-升级任务'
)
@ddt.ddt
class Test_UpgradeTask_Module(unittest.TestCase):
@allure.story("管理后台:升级任务模块")
@ddt.data(*Content)
def test_upgrade_task(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':
# 新建、查询
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:
ActID = rspJson["data"]['id']
writeResult = Txt().append_write_txt("08_CreateUpgradeTaskID.txt", str(ActID))
if not writeResult:
LOGGER.error(f"---写入 升级任务(草稿)ID 失败!")
else:
if requestType_c == "PUT":
# 编辑
ActIDList = Txt().read_txt("08_CreateUpgradeTaskID.txt")
LOGGER.info(f"---新建的 升级任务ID list为{ActIDList}")
ActID = ActIDList[0]
if ActID not in [None, '']:
api_url = api_env[ProCfgData['ExecutionEnv']] + ManagementPlatformApi_zh[caseModule_c] # 请求地址
last_api_url = api_url.replace("{UpgradeID}", 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:
ActIDList = Txt().read_txt("08_CreateUpgradeTaskID.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("{UpgradeID}", 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)