Python自动化框架设计unittest+report

上一篇 / 下一篇  2017-12-04 14:42:28 / 个人分类:自动化测试

1、测试用例
例:test_case.py文件
importunittest

classMyTestCase(unittest.TestCase):
deftest1(self):
self.assertEqual(1,1,'TEST1,相等!')
print('=====================test1第一条记录===========================')

deftest2(self):
self.assertEqual(2,2,'TEST2,相等!')
print('=====================test2第二条记录===========================')
deftest3(self):
self.assertEqual(2,2,'TEST3,相等')
print('=====================test3第三条记录===========================')

if__name__ =='__main__':
unittest.main()
2、生成测试报告,自动发送邮件代码
例:TestRunner_ToAutoEmail_Report.py
# coding=utf-8
'''
Created on 2017-6-7
@author: Wangjinjun
Project:1、执行测试用例 2、生成测试报告 3、邮件自动发送测试报告
'''
importunittest
fromHTMLTestRunnerimportHTMLTestRunner
importtime
importos
importsmtplib
fromemail.mime.textimportMIMEText
fromemail.mime.multipartimportMIMEMultipart
fromemail.headerimportHeader

# 取最新测试报告
defnew_file(test_report_dir):
#取出test_report_dir目录下所有文件,返回list列表
lists = os.listdir(test_report_dir)
# sort按key的关键字进行排序,lambda的入参fn为lists列表的元素,获取文件的最后修改时间
# 最后对lists元素,按文件修改时间大小从小到大排序
lists.sort(key=lambdafn:os.path.getatime(test_report_dir+'\\'+fn))
#获取最新文件的绝对路径
file_path = os.path.join(test_report_dir,lists[-1])
returnfile_path

#发送邮件,发送最新测试报告
defsend_email(newfile):
#打开文件
f =open(newfile,'rb')
#读取文件内容
mail_body = f.read()
#关闭文件
f.close()

#发送邮箱服务器
smtpserver ='smtp.qiye.163.com'
# 发送邮箱用户名/密码
user ='***@163.com'
password ='*****'
# 发送邮箱
sender ='***@163.com'
# 多个接收邮箱,单个收件人的话,直接是receiver='XXX@126.com'
receiver = ['***@163.com','***@163.com']
# 发送邮件主题
subject ='【测试报告】:'+ now +'TestReport.html'
msg = MIMEMultipart('mixed')

msg_html1 = MIMEText(mail_body,'html','utf-8')
msg.attach(msg_html1)

msg_html = MIMEText(mail_body,'html','utf-8')
msg_html["Content-Disposition"] ='attachment; filename="TestReport.html"'
msg.attach(msg_html)

msg['From'] ='***@163.com <***@163.com>'
# 单个收件人 msg['To'] = 'XXX@doov.com.cn'
# 多个收件人
msg['To'] =";".join(receiver)
msg['Subject'] = Header(subject,'utf-8')

# 连接发送邮件
smtp = smtplib.SMTP()
smtp.connect(smtpserver,25)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()


if__name__ =='__main__':
print('=====AutoTest Start======')
# 1.执行测试用例,生成测试报告
# 测试用例路径
test_dir ='E:\\Python_Work\\HJ_TEST\\HJ_TestCase'
# 测试报告的路径
test_report_dir ='E:\\Python_Work\\HJ_TEST\\Report'

discover = unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py')
now = time.strftime('%Y-%m-%d_%H_%M_%S_')
filename = test_report_dir +'\\'+ now +'TestReport.html'
fp =open(filename,'wb')
# 报告内容
runner = HTMLTestRunner(stream=fp,title=u'测试报告',description=u'用例执行情况:')
runner.run(discover)
fp.close()

# 2.取最新测试报告
new_report = new_file(test_report_dir)
# 3.发送邮件,发送最新测试报告html
send_email(new_report)


TAG: Report Unittest unittest 自动化脚本

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-24  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 6250
  • 日志数: 4
  • 建立时间: 2017-11-29
  • 更新时间: 2017-12-04

RSS订阅

Open Toolbar