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)
相关阅读:
- selenium webdriver(python篇)学习(三)----unittest学习 (dj7491916, 2012-11-03)
- Python unittest 组织testcase执行顺序 (junhui18, 2014-7-15)
- 工作中如何写好report (qqcaomei, 2015-9-29)
- python运用selenium和unittest实现自动化测试后发送测试报告邮件 (wyy1221, 2016-4-01)
- python入门之 使用xlrd模块读写Excel文件的方法 (xuquan, 2016-5-04)
- Python的Unittest单元测试使用详解 (zaza9084, 2017-1-25)
- 如何编写优秀的bug report-致测试初学者 (丫头sunny, 2017-3-03)
- python 单元测试——unittest (资深梦游, 2017-8-09)
- 测试用例的初始化和结束函数 (资深梦游, 2017-8-10)
- 【Appium+Pyhton+unittest】如何只执行一个测试用例 (资深梦游, 2017-10-16)
收藏
举报
TAG:
Report
Unittest
unittest
自动化脚本