# -*- coding: utf-8 -*-
import requests,datetime,time,types,hashlib
import murl,json
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def result(testcase_autono,auto_result,varchar_log,auto_remark):
try:
autotest_version = str(str(datetime.date.today()) + "_" + str(datetime.datetime.today().hour))
r = requests.post(murl.ResultUrl + '/autoresult/add',data={'project_name':'foscloud',\
'autotest_version':autotest_version,\
'testcase_autono':testcase_autono,\
'auto_result':auto_result,\
'varchar_log':varchar_log,\
'jpg_url':'',\
'its_bug':'',\
'code_version':'',\
'auto_remark':auto_remark,\
})
if '"errorcode":''' in r.text:
return True
else:
return False
except Exception,e:
return False
def dateToTime(date):
return str(int(time.mktime(time.strptime(date,'%Y%m%d'))))
def dateAddOne(str, addDay = 1):
try:
date = datetime.datetime.strptime(str,'%Y-%m-%d')
except:
date = datetime.datetime.strptime(str,'%Y%m%d')
date = date + datetime.timedelta(days = 1)
return date.strftime('%Y%m%d')
def md5(str):
m = hashlib.md5()
m.update(str)
return m.hexdigest()
def getResult():
try:
autotest_version = str(str(datetime.date.today()))
r = requests.get(murl.ResultUrl +'/autoresult/foscloud?project_name=foscloud&autotest_version=%s' % (autotest_version))
return r.text
except:
return False
def strBodyEmail(strResult):
# HtmlStr =
str = ''
PassCount = 0
FailCount = 0
WainCount = 0
try:
# if 1:
jsonResult = json.loads(strResult)
for data in jsonResult:
autotest_version = data['autotest_version']
autotest_autono = data['autotest_autono']
auto_result = data['auto_result']
auto_remark = data['auto_remark']
if 'Pass' in auto_result:
PassCount = PassCount + 1
color = '#00FF00'
elif 'Fail' in auto_result:
FailCount = FailCount + 1
color = '#FF0000'
else:
WainCount = WainCount + 1
color = '#FFFF00'
varchar_log = json.dumps(data['varchar_log'])
if len(varchar_log) > 100:
varchar_log = varchar_log[0:100]
title ='
' \'
%s' \'
%s' \'
%s' \'
%s' %('date','api','result','use time(s)')# if 'Fail' in auto_result:
strTR ='
' \'
%s' \'
%s' \'
%s' \'
%s' \'
response%s' % \(autotest_version,autotest_autono,color,auto_result,auto_remark,varchar_log)
str = str + strTR
sendEmailBodyStr = """
( title,str)
# print sendEmailBodyStr
return {"total":{'PassCount':PassCount,'FailCount':FailCount,'WainCount':WainCount},'html':sendEmailBodyStr}
# return sendEmailBodyStr
except:
return False
def sendEmail():
mailto_list=murl.mailto_list #
# mail_host = ""
# mail_user = ""
#
# mail_pass=""
# mail_postfix="" #发件箱的后缀
mail_host = ""
mail_user = ""
mail_pass=""
mail_postfix="" #发件箱的后缀
body = strBodyEmail(getResult())
if body:
mail_body = body['html']
mail_title=unicode(" test result %s %s" % (datetime.datetime.now(), body['total']))
me="foscam test team"+"" % (mail_user)
msg = MIMEText(mail_body, _subtype='html', _charset='utf-8')
msg['Subject'] = mail_title #设置主题
msg['From'] = me
msg['To'] = ";".join(mailto_list)
try:
s = smtplib.SMTP()
s.connect(mail_host) #连接smtp服务器
s.login(mail_user,mail_pass) #登陆服务器
s.sendmail(me, mailto_list, msg.as_string()) #发送邮件
s.close()
return True
except Exception, e:
print str(e)
return False
生成后大概长这个样子
标签: