def run_case(self): '''执行所有的用例, 并把结果写入测试报告''' log_info.info("********************TEST START********************") try: all_case = self.add_case() #调用add_case获取suite if all_case != None: test_name = readConfig.Read_Config().get_info('Report', 'test_name') #获取测试人员姓名 report_title = readConfig.Read_Config().get_info('Report', 'report_title') #获取测试报告名称 fp = open(report_path + '\\' + report_title + nowtime + '.html','wb') #定义一个文件对象,给后面的HTMLTestRunner生成测试报告用,注意打开方式必须是wb runner = HTMLTestRunnerCN_py3.HTMLTestRunner(stream=fp, title=report_title, description="用例测试情况",tester=test_name) #生成HTML报告 runner.run(all_case) #执行用例 fp.close() else: log_info.info('---测试套件为空---') except Exception as ex: log_info.error(str(ex)) finally: log_info.info("*********************TEST END*********************") # 判断邮件发送的开关 if on_off == 'on': self.send_report() #发送测试报告 else: print("邮件发送开关关闭")
def host_headers(): headers = {} headers['x-key-hash'] = readConfig.Read_Config().get_info( 'HTTP', 'x-key-hash') #获取x-key-hash headers['Content-Type'] = 'application/json;charset=utf-8' #添加headers headers['Authorization'] = 'Bearer ' + get_token() #添加headers return headers
def get_token(): "获取登录token" login = readConfig.Read_Config() #类实例化 login_url = login.get_info('Login', 'login_host') #获取登录url headers = json.loads(login.get_info('Login', 'login_headers')) #登录headers data = json.loads(login.get_info('Login', 'login_account')) #登录账号、密码 r = requests.request('POST', login_url, json=data, headers=headers) #登录接口请求 return r.json()['access_token'] #返回登录token
# _*_ coding:utf-8 _*_ import unittest, os, time from Common import getPathInfo, readConfig, HTMLTestRunnerCN_py3,log,sendemail #from tomorrow import threads nowtime = time.strftime("%Y%m%d%H%M%S") log_info = log.Logger().get_logger() on_off=readConfig.Read_Config().get_info('Email','on_off') class All_Test(object): def __init__(self): global report_path path=getPathInfo.get_Path() report_path = os.path.join(path, 'Report') #测试报告存放路径 if not os.path.exists(report_path): #判断Logs路径是否存在 os.mkdir(report_path) #创建Logs文件 self.caselist_path = os.path.join(path, "caselist.txt") #配置执行哪些测试文件的配置文件路径 self.casepath = os.path.join(path, "TestCase") #测试用例路径 self.caselist = [] #定义一个空列表 def get_caselist(self): ''' 读取caselist.txt文件中的用例名称,并添加到caselist元素组 ''' fb = open(self.caselist_path) #打开文件 for value in fb.readlines(): #遍历fb所有数据 if value != '' and not value.startswith('#'): #如果data非空且不以#开头 self.caselist.append(value.replace('\n', '')) #读取每行数据会将换行转换为\n,去掉每行数据中的\n fb.close() #关闭文件 return self.caselist #返回caselist
# _*_ coding:utf-8 _*_ import unittest, paramunittest, json from Common import configHttp, readExcel, readConfig, getheader, getrandom, log excel = readExcel.readExcel('API_TestCase.xlsx') #获取excel文件 names = excel.get_sheetnames() #获取sheel testcase = excel.get_xlsx(names[2]) #获取指定shell的case baseurl = readConfig.Read_Config().get_info('HTTP', 'baseurl') #获取配置文件的baseurl headers = getheader.host_headers() #获取heders log_info = log.logger #导入log系统 @paramunittest.parametrized(*testcase) class testUserLogin(unittest.TestCase): def setParameters(self, NO, case_name, method, path, data, code): """ 从 excel 中获取用例 :param NO: 用例序号 :param case_name: 用例名称 :param method: HTTP方法 :param path: 路径 :param data: 参数 :param code: 响应 """ self.case_name = NO self.case_name = str(case_name) self.method = str(method) self.path = baseurl + str(path) self.data = json.loads(data) self.code = int(code) #字符串转字典类型