def write_data(sheet_name, row, col, value, red=False): workbook1 = load_workbook(get_config('DATABASE', 'data_address')) sheet = workbook1[sheet_name] sheet.cell(row, col).value = value if red == False: pass else: sheet.cell(row, col).font = Font(color=colors.RED) #sheet.cell(row,col).fill=PatternFill("solid", fgColor="FF0000") workbook1.save(get_config('DATABASE', 'data_address'))
def read_data(sheet_name, case_id): # 打开excel workbook1 = load_workbook(get_config('DATABASE', 'data_address')) # otherr(test_data) sheet1 = workbook1[sheet_name] print(sheet1) test_case = [] #用来存储每一行数据,也就是一条测试用例 test_case.append(sheet1.cell(case_id + 1, 1).value) test_case.append(sheet1.cell(case_id + 1, 2).value) test_case.append(sheet1.cell(case_id + 1, 3).value) test_case.append(sheet1.cell(case_id + 1, 4).value) test_case.append(sheet1.cell(case_id + 1, 5).value) test_case.append(sheet1.cell(case_id + 1, 6).value) test_case.append(sheet1.cell(case_id + 1, 7).value) test_case.append(sheet1.cell(case_id + 1, 8).value) test_case.append(sheet1.cell(case_id + 1, 9).value) return test_case #将读取到的用例返回
def send_email(reporthtml, resultxlsx): #创建一个邮件 msg = MIMEMultipart() #邮件标题 msg['Subject'] = Header('接口自动化测试报告', 'utf-8') msg['from'] = get_config('EMAIL', 'sender') # 发送邮件的人 msg['to'] = get_config('EMAIL', 'receiver') #邮件正文内容 content = MIMEText(open(reporthtml, 'rb').read(), 'html', 'utf-8') #将邮件内容添加到邮件 msg.attach(content) #添加xlsx附件 attacxlsx = MIMEApplication(open(resultxlsx, 'rb').read()) attacxlsx.add_header('Content-Disposition', 'attachment', filename=resultxlsx) msg.attach(attacxlsx) #添加html附件 attachhtml = MIMEApplication(open(reporthtml, 'rb').read()) attachhtml.add_header('Content-Disposition', 'attachment', filename=reporthtml) msg.attach(attachhtml) ''' attachhtml = MIMEText(open(reporthtml,'rb').read(), 'base64', 'utf-8') attachhtml['Content-Type'] = 'application/octet-stream' attachhtml["Content-Disposition"] = 'attachment;filename="APIReport.html"' msg.attach(attachhtml)''' try: s = smtplib.SMTP_SSL(get_config('EMAIL', 'serverip'), get_config('EMAIL', 'serverport')) #ssl加密方式登录邮箱 s.login(get_config('EMAIL', 'username'), get_config('EMAIL', 'password')) # 这里的to_address是真正需要发送的到的mail邮箱地址需要的是一个list s.sendmail(msg['from'], msg['to'], msg.as_string()) print('%s----发送邮件成功' % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) except Exception as err: print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) print(err)
from public import HTMLTestRunner import time import os import unittest from public import sendEmail from base.read_config import get_config import shutil suite = unittest.TestSuite() discover = unittest.defaultTestLoader.discover(get_config( 'DATABASE', 'testcase_address'), pattern='test*.py', top_level_dir=None) for test_case in discover: for case_name in test_case: suite.addTest(case_name) print(case_name) print(test_case) if __name__ == "__main__": now = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time())) day = time.strftime('%Y-%m-%d', time.localtime(time.time())) tdreport = get_config('DATABASE', 'report_address') + day reportfile = tdreport + "\\" + now + "_result.html" tdresult = get_config('DATABASE', 'result_address') + day if os.path.exists(tdreport): fp = open(reportfile, "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, verbosity=2, title="自动化接口测试报告", description="接口测试用例执行情况") runner.run(suite) fp.close()
def count_case(sheet_name): workbook1 = load_workbook(get_config('DATABASE', 'data_address')) sheet = workbook1[sheet_name] max_row = sheet.max_row #统计测试用例的行数 return max_row