在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>

在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>

  print >> sys.stderr, ' Time Elapsed: %s' % (self.stopTime-self.startTime) 或print(sys.stderr, ' Time Elapsed: %s' % (self.stopTime-self.startTime)) 修改为 sys.stderr.write(' Time Elapsed: %s ' % (self.stopTime - self.startTime))

如下图这样

# -*- coding: UTF-8 -*-
import unittest
from BSTestRunner import BSTestRunner
import time
import smtplib                           #发送邮件模块
from email.mime.text import MIMEText    #定义邮件内容
from email.header import Header         #定义邮件标题
import os
import sys

path='D:\AUTO\kyb_testProject\'
sys.path.append(path)
别忘了指定路径 不然jekins 执行不了


def send_mail(reports): f=open(reports,'rb') mail_content = f.read() f.close() smtpserver = 'smtp.qq.com' user = '3481073579@qq.com' password = 'ethxyyouqtficiha' sender = '3481073579@qq.com' receives = ['huzy@southgnss.com', ''] subject = 'kyb_自动化测试报告' #content = '<html><h1 style="color:red">我要自学网,自学成才!</h1></html>' msg = MIMEText(mail_content, 'html', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = sender msg['To'] = ','.join(receives) smtp = smtplib.SMTP_SSL(smtpserver, 465) smtp.helo(smtpserver) smtp.ehlo(smtpserver) smtp.login(user, password) print("Start send email...") smtp.sendmail(sender, receives, msg.as_string()) smtp.quit() print("Send email end!") def latest_report(report_dir): lists = os.listdir(report_dir) print(lists) lists.sort(key=lambda fn: os.path.getatime(report_dir + '\' + fn)) print("the latest report is " + lists[-1]) file = os.path.join(report_dir, lists[-1]) print(file) return file if __name__ == '__main__': report_dir= '../reports' test_dir = '../test_case' discover = unittest.defaultTestLoader.discover(test_dir, pattern="test_login.py") now=time.strftime("%Y-%m-%d %H_%M_%S") report_name=report_dir+'/'+now+'result.html' with open(report_name,'wb') as f: runner=BSTestRunner(stream=f,title="Test Report",description="kyb Android app test report") runner.run(discover) latest_report=latest_report(report_dir) send_mail(latest_report)
原文地址:https://www.cnblogs.com/zhenyu1/p/12613418.html