def send_mail(sub, content, job): mail_conf_path = os.path.join(Get.base_dir(), 'Conf', 'Email.ini') cf = ConfigParser() cf.read(mail_conf_path, 'utf-8') mail_to = cf.items(job) mail_to_list = [] for k, v in mail_to: mail_to_list.append(v) mail_host = cf.get('base', 'host') mail_user = cf.get('base', 'user') mail_pass = cf.get('base', 'pass') # mail_postfix = cf.get('base', 'postfix') me = "<" + mail_user + ">" msg = MIMEText(content, _subtype='html', _charset='gb2312') msg['Subject'] = sub msg['From'] = me msg['To'] = ";".join(mail_to_list) msg['CC'] = '*****@*****.**' try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user, mail_pass) s.sendmail(me, mail_to_list, msg.as_string()) s.close() return True except Exception as e: print(str(e)) return False
def get_jenkins_obj(): jenkins_conf_path = os.path.join(Get.base_dir(), 'Conf', 'Jenkins.ini') cf = ConfigParser() cf.read(jenkins_conf_path, 'utf-8') return jenkins.Jenkins(cf.get('base', 'host'), username=cf.get('base', 'username'), password=cf.get('base', 'password'))
def __init__(self): db_conf_path = os.path.join(Get.base_dir(), 'Conf', 'DB.ini') cf = ConfigParser() cf.read(db_conf_path, 'utf-8') server = cf.get('mysql', 'server') db = cf.get('mysql', 'db') user = cf.get('mysql', 'user') password = cf.get('mysql', 'password') self.conn = pymysql.connect(host=server, port=3306, user=user, passwd=password, database=db, charset='UTF8') self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
print('Automation Test Script') print( '==============================================================================' ) x, y, z = Jenkins.get_job_info(job_name) jenkins_ls = [('Job Name', job_name), ('Version', x), ('Builder', y), ('Build Finish Time', z)] print('Get jenkins info successfully...') test_suit = unittest.TestLoader().loadTestsFromTestCase( Mappings.test_suite[job_name]) total_test = unittest.TestSuite([test_suit]) timeStampArr = time.localtime(time.time()) folder = Get.base_dir() + '/Html/' + time.strftime('%Y-%m-%d', timeStampArr) + '/' os.makedirs(folder, exist_ok=True) file = folder + time.strftime('%H-%M-%S') + '.html' with open(file, 'w', encoding='utf-8') as f: HTMLTestRunner(stream=f, title='BVT Test Report', description=u'').run(total_test, jenkins_ls) print('Finish to run test case...') print('Generate report successfully...') with open(file, 'r') as f: content = f.read() if Mail.send_mail('Automation Script Report', content, job_name): print('Email is sent to builder...')