3.9 run_main.py源码(兼容python2和3)

以下代码在python2和python3上都跑通过,python3只需注释掉上面红色框框区域代码就行(最后一步发送邮箱代码,我注释掉了)。

# coding=utf-8
import unittest
import time
import HTMLTestRunner
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
import os
####下面三行代码python2报告出现乱码时候可以加上####
import sys
reload(sys)
sys.setdefaultencoding('utf8') # 这个是优化版执行所有用例并发送报告,分四个步骤
# 第一步加载用例
# 第二步执行用例
# 第三步获取最新测试报告
# 第四步发送邮箱 (这一步不想执行的话,可以注释掉最后面那个函数就行)
def add_case(case_path, rule):
    '''加载所有的测试用例'''
    testunit = unittest.TestSuite()
    # 定义discover方法的参数
    discover = unittest.defaultTestLoader.discover(case_path,
                                                  pattern=rule,
                                                  top_level_dir=None)
    # discover方法筛选出来的用例,循环添加到测试套件中
    # for test_suite in discover:
    #     for test_case in test_suite:
    #         testunit.addTests(test_case)
    #         print testunit
    testunit.addTests(discover)  # 直接加载discover
    print(testunit)
    return testunit
def run_case(all_case, report_path):
    '''执行所有的用例, 并把结果写入测试报告'''
    now = time.strftime("%Y_%m_%d %H_%M_%S")
    report_abspath = os.path.join(report_path, now+"result.html")
    # report_abspath = "D:\\web_project\\report\\"+now+"result.html"
    fp = open(report_abspath, "wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                                           title=u'自动化测试报告,测试结果如下:',
                                           description=u'用例执行情况:')
    # 调用add_case函数返回值
    runner.run(all_case)
    fp.close()
def get_report_file(report_path):
    '''获取最新的测试报告'''
    lists = os.listdir(report_path)
    lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
    print (u'最新测试生成的报告: '+lists[-1])
    # 找到最新生成的报告文件
    report_file = os.path.join(report_path, lists[-1])
    return report_file
def send_mail(sender, psw, receiver, smtpserver, report_file):
    '''发送最新的测试报告内容'''
    # 读取测试报告的内容
    with open(report_file, "rb") as f:
        mail_body = f.read()
    # 定义邮件内容
    msg = MIMEMultipart()
    body = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    msg['Subject'] = u"自动化测试报告"
    msg["from"] = sender
    msg["to"] = psw
    # 加上时间戳
    # msg["date"] = time.strftime('%a, %d %b %Y %H_%M_%S %z')
    msg.attach(body)
    # 添加附件
    att = MIMEText(open(report_file, "rb").read(), "base64", "utf-8")
    att["Content-Type"] = "application/octet-stream"
    att["Content-Disposition"] = 'attachment; filename= "report.html"'
    msg.attach(att)
    # 登录邮箱
    smtp = smtplib.SMTP()
    # 连接邮箱服务器
    smtp.connect(smtpserver)
    # 用户名密码
    smtp.login(sender, psw)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
    print('test report email has send out !')
if __name__ == "__main__":
    # 测试用例的路径、匹配规则
    case_path = "D:\\test\\newp\\case"
    rule = "test*.py"
    all_case = add_case(case_path, rule)   # 1加载用例
    # 生成测试报告的路径
    report_path = "D:\\test\\newp\\report"
    run_case(all_case, report_path)        # 2执行用例
    # 获取最新的测试报告文件
    report_file = get_report_file(report_path)  # 3获取最新的测试报告
    #邮箱配置
    sender = "yoyo@xxx.com"
    psw = "xxx"
    # 收件人多个时,中间用逗号隔开,如'a@xx.com,b@xx.com'
    receiver = "yoyo@xxx.com"
    smtp_server = 'smtp.xxx.com'
    # send_mail(sender, psw, receiver, smtp_server, report_file)  # 4最后一步发送报告,需要发邮件就取消注释。

3.9 run_main.py源码(兼容python2和3)的更多相关文章

  1. daily news新闻阅读客户端应用源码(兼容iPhone和iPad)

    daily news新闻阅读客户端应用源码(兼容iPhone和iPad),也是一款兼容性较好的应用,可以支iphone和ipad的阅读阅读器源码,设计风格和排列效果很不错,现在做新闻资讯客户端的朋友可 ...

  2. Django框架base.py源码

    url.py文件 from django.conf.urls import url from django.contrib import admin from app_student import v ...

  3. Python逆向(四)—— Python内置模块dis.py源码详解

    一.前言 上一节我们对Python编译及反汇编做了讲解,大家知道dis模块可以将编译好的pyc文件中提取出来的PyCodeObject反汇编为可以阅读字节码形式.本节我们对dis模块中的源码进行详细的 ...

  4. r-cnn学习(四):train_faster_rcnn_alt_opt.py源码学习

    论文看的云里雾里,希望通过阅读其代码来进一步了解. 参考:http://blog.csdn.net/sloanqin/article/details/51525692 首先是./tools/train ...

  5. 深度学习入门-4.1 AND.py 源码分析

    源代码 ------------------------------------------------------------------------------------------------ ...

  6. 获得PyInstaller打包exe的py源码

     参考链接:https://laucyun.com/33359ed9f725529ac9b606d054c8459d.html way1:pyi-archive_viewer 提取pyc,uncomp ...

  7. python string.py 源码分析 三:maketrans

    l = map(chr, xrange(256)) #将ascii转为字符串 _idmap = str('').join(l) del l # Construct a translation stri ...

  8. python string.py 源码分析 二:capwords

    def capwords(s, sep=None): """capwords(s [,sep]) -> string Split the argument into ...

  9. python string.py 源码分析 一

    # Some strings for ctype-style character classification c风格字符串 whitespace = ' \t\n\r\v\f' #空白字符 \t 制 ...

随机推荐

  1. web service 上传file说明

    之前做过一个接口,PI发布WEB SERVICE给对方调,传附件到SAP... 接口中包含文件名称,文件类型,文件流... 1.SOAPUI新建项目: 文件内容的地方会自动带上一个ID,这个ID对应文 ...

  2. nvm 设置 nodejs 默认版本

    nvm 设置 nodejs 默认版本 windows 系统的版本管理软件是nodist mac系统的node版本管理根据是nvm 每次重启vscode软件后,nvm ls 看到的默认版本都会恢复到v5 ...

  3. 与WCAG相关的一些学习心得

    1.什么是 WCAG? WCAG全称Web Content Accessibility Guidelines 网页内容无障碍浏览准则,简单的说就是为了方便残障人士(包括低视患者,盲人,聋人,学习障碍, ...

  4. 【css3】画‘百分比圆’

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. web前端学习python之第一章_基础语法(一)

    web前端学习python之第一章_基础语法(一) 前言:最近新做了一个管理系统,前端已经基本完成, 但是后端人手不足没人给我写接口,自力更生丰衣足食, 所以决定自学python自己给自己写接口哈哈哈 ...

  6. 基于三层架构下的公共数据访问方法(Sqlite数据库)

    作者总结了一下,使用Winform的三层架构做窗体应用程序,在数据访问方面,有用到纯sql语句方法.参数方法.存储过程方法. 那么什么是三层架构呢? UI---存放Form窗体---(用户所关心的) ...

  7. Python-接口自动化(八)

    unittest单元测试框架(八)    (九)unittest 1.基本概念 python自带的unittest单元测试框架不仅可以适用于单元测试,也适用于WEB自动化测试用例的开发与执行,uint ...

  8. windows 10系统在右键中添加管理员打开cmd

    需要修改注册表内容,新建文件,后缀名改为reg,文件中粘贴下边的代码 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory ...

  9. Java多线程之线程状态总结

    概述 线程大家肯定不陌生,对于线程中的运行状态,自己经常搞混淆,这边按照下图记录下: 线程一般来说有如下几种状态: 新建,可运行,超时阻塞,等待阻塞,同步阻塞,死亡 yeild:当线程执行了yield ...

  10. fastJson遇到的问题

    概述 现在的代码开发中,json这种数据类型使用的是越来越多,因为它的存取速度都比较快,而且,使用起来非常的简单,今天工作的时候,我就遇到了一个关于json的生产问题,这个问题我之前确实还没有注意过, ...