def __init__(self): self.logger = MyLog() self.readconfig = ReadConfig() self.send_mail = SendEmail() # self.env = ResetEnv() self.is_send = self.readconfig.get_email("is_send") # 测试报告基本信息 self.testers = "Roman" self.title = "元丁接口测试报告" self.description = "正式/测试环境:Develop,IP地址:%s" % self.readconfig.get_base_url( ) # print(self.description) # 导入TestCase目录下的全部测试用例 self.discover = unittest.defaultTestLoader.discover(test_case_path, pattern='test*.py') # 导入指定测试用例列表文件 self.case_list_file = case_list_path self.case_list_list = [] # print(self.case_list_list) # 重置测试环境 self.is_env = True
def __init__(self): self.method = RunMethod() self.data = GetData() self.com_result = CompareResult() self.config = ReadConfig() self.depend = DependData() self.sendemail = SendEmail()
def run(): format_time = "%Y-%m-%d %H_%M_%S" report_dir = settings.BASE_DIR + '\\report' report_path = settings.BASE_DIR + '\\report\\%stest_sinaemail.html' % time.strftime(format_time) discover = unittest.defaultTestLoader.discover(settings.BASE_DIR + '\\core\\testCases', pattern='test*.py') fp = open(report_path, 'wb') runner = HTMLTestRunner( stream=fp, title='新浪邮箱测试报告', description='测试用例执行情况:' ) runner.run(discover) fp.close() email_op_obj = SendEmail('完成测试用例执行') # email_op_obj.add_text("test for send text") report = new_report(report_dir) email_op_obj.add_file_content(report) email_op_obj.send_email()
def get_test_cases(dirpath): # dirpath是存放测试用例的文件路径 test_cases = unittest.TestSuite() # 测试用例均使用"test_"开头命名 suites = unittest.defaultTestLoader.discover(dirpath, 'test_*.py', top_level_dir=dirpath) for suite in suites: test_cases.addTests(suite) return test_cases if __name__ == '__main__': cases = get_test_cases('../testcase') # 测试用例导入 now = time.strftime("%Y-%m-%d_%H_%M_%S") # 报告生成时间 test_reports_address = '../report' # 测试报告存放位置 filename = '../report/' + now + 'report.html' # 设置报告文件名 fp = open(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'Web自动化测试', description=u'详细测试结果如下:') runner.run(cases) fp.close() # 向指定邮箱发送测试报告的html文件 time.sleep(6) # 查找最新生成的测试报告地址 new_report_addr = SendEmail().acquire_report_address(test_reports_address) # 自动发送邮件 # SendEmail().send_email(new_report_addr)
#设置报告文件保存路径 report_path = os.path.dirname(os.path.abspath('.')) + '/report/' #获取系统当前时间 now = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) #设置报告名称格式 html_file = report_path + now + 'HTMLtemplate.html' fp = open(html_file, 'wb') #每次加载一个测试用例到测试套件中 # suite=unittest.TestSuite() # suite.addTest(TestRegister('test_register')) #一次性加载一个类文件下所有测试用例到suite中去 suite = unittest.TestSuite(unittest.makeSuite(TestBook)) #加载一个路径下所有的测试用例 # listcasedir = os.path.dirname(os.path.abspath('.'))+'/manage_web/' # suite=unittest.TestLoader().discover(listcasedir, pattern ='test*.py', top_level_dir = None) if __name__ == '__main__': #初始化一个HTMLTestRunnner实例对象,用来生成报告 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='患者端 测试报告', description='用例测试情况') runner.run(suite) fp.close() SendEmail(report_path).send_email()
class RunTest: def __init__(self): self.method = RunMethod() self.data = GetData() self.com_result = CompareResult() self.config = ReadConfig() self.depend = DependData() self.sendemail = SendEmail() def start_on_run(self): res = None result = None pass_num = [] fail_num = [] no_run_num = [] rows_count = self.data.get_case_lines() run_host = self.config.get_http() if run_host: self.data.write_run_env(1, run_host) print("总共 %s 行" % (rows_count)) print("总共 %s 个测试用例" % (rows_count - 2)) print("测试结果:") for i in range(3, rows_count + 1): #time.sleep(5) is_run = self.data.get_is_run(i) is_depend = self.data.get_is_depend(i) request_method = self.data.get_request_method(i) case_id = self.data.get_case_id(i) data = self.data.get_request_data(i) # print(case_id) # print("是否有依赖:%s"%is_depend) if is_run and is_depend: url = run_host + self.data.get_request_url(i) + self.depend.get_data_in_key(i, case_id) # print("编号%s的url:%s" % (case_id, url)) else: url = run_host + self.data.get_request_url(i) # print("编号%s的url:%s"%(case_id, url)) if case_id == "DEV-001" or case_id == "CMB-001": header = self.data.get_request_header(i) else: header = self.data.get_request_header(i, self.config.get_token()) expect_result = self.data.get_expect_result(i) if is_run: run_case_id = self.data.get_case_id(i) run_case_title = self.data.get_case_title(i) res = self.method.run_main(method=request_method, url=url, data=data, header=header) # print("====>>>") # print(res) # if i == 3: if case_id == "DEV-001": dict_json = json.loads(res) # 把json数据转换成字典对象 orc_token = dict_json['orchestrator_admin_token'] # print(dict_json) self.config.write_token(orc_token) # print(orc_token) if case_id == "CMB-001": dict_json = json.loads(res) # 把json数据转换成字典对象 ten_token = dict_json['tenant_admin_token'] # print(dict_json) self.config.write_token(ten_token) # print(orc_token) if self.com_result.is_contain(res, expect_result): print("编号%s(%s): 测试通过" % (run_case_id, run_case_title)) self.data.write_data_actual(i, 'Pass') self.data.write_data_run(i, res) pass_num.append(i) else: print("编号%s(%s): 测试不通过" % (run_case_id, run_case_title)) self.data.write_data_actual(i, 'Fail') self.data.write_data_run(i, res) fail_num.append(i) else: no_run_case_id = self.data.get_case_id(i) no_run_case_title = self.data.get_case_title(i) print("编号%s(%s): 不需要执行" % (no_run_case_id, no_run_case_title)) self.data.write_data_actual(i, 'NA') no_run_num.append(i) print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") print("%s 个测试用例被执行" % (len(pass_num+fail_num))) print("%s 个测试用例没有执行" % (len(no_run_num))) # 以邮件方式发送测试报告 self.sendemail.start_send(pass_num, fail_num)
class RunTest: def __init__(self): self.logger = MyLog() self.readconfig = ReadConfig() self.send_mail = SendEmail() # self.env = ResetEnv() self.is_send = self.readconfig.get_email("is_send") # 测试报告基本信息 self.testers = "Roman" self.title = "元丁接口测试报告" self.description = "正式/测试环境:Develop,IP地址:%s" % self.readconfig.get_base_url( ) # print(self.description) # 导入TestCase目录下的全部测试用例 self.discover = unittest.defaultTestLoader.discover(test_case_path, pattern='test*.py') # 导入指定测试用例列表文件 self.case_list_file = case_list_path self.case_list_list = [] # print(self.case_list_list) # 重置测试环境 self.is_env = True # self.is_env = self.env.delete_db() def get_case_list(self): """获取需要进行运行的测试用例列表""" fb = open(self.case_list_file) for i in fb.readlines(): data = str(i) if data != '' and not data.startswith('#'): self.case_list_list.append(data.replace('\n', '')) fb.close() # print(self.case_list_list) def set_test_suite(self): """设置添加测试套件""" self.get_case_list() test_suite = unittest.TestSuite() suite_module = [] for case in self.case_list_list: case_name = case.split('/')[-1] print(case_name + '.py') discover = unittest.defaultTestLoader.discover(test_case_path, pattern=case_name + '.py') suite_module.append(discover) if len(suite_module) > 0: for suite in suite_module: for test_name in suite: test_suite.addTest(test_name) else: return None return test_suite def run_test(self): """执行测试""" if self.is_env: try: test_suite = self.set_test_suite() # 获取测试套件 now = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(time.time())) # 获取当前日期时间 public_path = os.path.dirname(os.path.abspath(sys.argv[0])) # filename = public_path + "/report/" + now + "_report.html" # 保存的报告路径和名称 filename = public_path + "/report/" + "index.html" # 保存的报告路径和名称 print("测试报告目录:%s" % filename) fp = open(filename, 'wb') runner = HTMLTestRunner(stream=fp, tester=self.testers, title=self.title, description=self.description) if test_suite is not None: runner.run(test_suite) # 执行指定添加的测试用例套件 # runner.run(self.discover) # 执行TestCase目录下的全部测试用例 else: self.logger.info("Have no case to test.") except Exception as e: self.logger.error(str(e)) finally: self.logger.warning( "---------------All Test End---------------") fp.close() # 发送电子邮件 if self.is_send == 'yes': self.send_mail.send_email() self.logger.warning("测试报告已发送电子邮件!") elif self.is_send == 'no': self.logger.warning("测试报告不发送电子邮件!") else: self.logger.error("测试报告发送电子邮件为未知状态,请检查配置!") else: self.logger.warning("测试环境清理失败的,无法继续执行测试!!!")
response_fag = ry.read_node_data('CASE_CONF', 'CASE_FIELD', 'RESPONSE', 'IS_WRITE') result_col = ry.read_node_data('CASE_CONF', 'CASE_FIELD', 'RESULT', 'INDEX') response_col = ry.read_node_data('CASE_CONF', 'CASE_FIELD', 'RESPONSE', 'INDEX') # 向Execl写入结果 copy_data, table_data_w = operation_excel.copy_execl(index) for row, value in row_result_dic.items(): if result_fag: operation_excel.write_data(table_data_w, row, result_col, value) else: break for row, value in row_response_dic.items(): if response_fag: operation_excel.write_data(table_data_w, row, response_col, value) else: break operation_excel.save(copy_data) if __name__ == '__main__': time_str = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') test_suite = unittest.defaultTestLoader.discover(path + r'\\', pattern='test*.py') result = BeautifulReport(test_suite) result.report(filename='report_' + time_str + '.html', description='测试报告', report_path=path + r'\..\Report') print(result.FIELDS['testAll'], result.FIELDS['testFail'], result.FIELDS['testAll'] - result.FIELDS['testFail'], result.FIELDS['beginTime'], type(result.FIELDS['beginTime']), result.FIELDS['end_time']) # 发送邮件 send_email = SendEmail(description=u'测试报告', pass_num=result.FIELDS['testAll'] - result.FIELDS['testFail'], fail_num=result.FIELDS['testAll'], start_time=result.FIELDS['beginTime'], end_time=result.FIELDS['end_time'], continue_time=result.FIELDS['totalTime']) send_email.send_email()
# 执行test_Demo testDemo = [ '-v', './testCase/testDemo.py', '--html=%s' % report_path + '%s%s' % (now_time, test_demo) ] # 执行test_Demo2 testDemo2 = [ '-v', './testCase/testDemo2.py', '--html=%s' % report_path + '%s%s' % (now_time, test_demo2) ] # 执行testDemo&testDemo2 testDemo1_2 = [ '-v', './testCase/testDemo.py', './testCase/testDemo2.py', '--html=%s' % report_path + '%s%s' % (now_time, test001_002) ] # 执行所有case all_case = ['-v', './testCase', '--html=./report/' + '%s' % cases] if __name__ == '__main__': pytest.main(testDemo) # send mail sm = SendEmail() mail_subject = 'interface demo test' mail_content = 'Plz check' mail_att = './report/' + test_demo # sm.send(mail_subject, mail_content, mail_att)