import unittest标签:__,封装,res,unittest,item,test,import,data From: https://www.cnblogs.com/huanyingnidedaolai/p/16732699.html
from tools.http_rquests import HttpRequests
from tools.get_data import GetCookie
from ddt import ddt,data
from tools.do_excel import DoExcel
from conf.project_path import *
from tools.my_log import Log
my_logger = Log()
test_data = DoExcel.get_data(test_case_path)
@ddt
class TestHttpRequests(unittest.TestCase):
def setUp(self) -> None:
pass
def tearDown(self) -> None:
pass
@data(*test_data)
def test_api(self,item):
res = HttpRequests.http_requests(item["url"],eval(item["header"]),eval(item["data"]),item["method"],getattr(GetCookie,"Cookie"))
if res.cookies:#利用反射存储cookie值
setattr(GetCookie,"Cookie",res.cookies)
try:
self.assertEqual(item["expected"],res.status_code)
TestResult = "成功"
except AssertionError as e:
TestResult = "失败"
my_logger.info("请查看日志,执行出错的结果是{0}".format(e),)
raise e
finally:
DoExcel.write_back(test_case_path,item["sheet_name"],item["case_id"]+1,str(res.json()),TestResult)
my_logger.info("获取的结果{0}".format(res.status_code))
if __name__ == '__main__':
TestHttpRequests().test_api()