zoukankan      html  css  js  c++  java
  • unittest测试框架 (下)

    目录

    1. unittest批量执行
    2. 生成html测试报告
    3. unittest之装饰器
    4. 自动发送邮件功能

    项目目录

     

     批量执行

    生成html测试报告 

    为了更好的展示测试报告,最好是生成 HTML格式的。

    unittest里面是不能生成 html格式报告的,需要导入一个第三方的模块:HTMLTestRunner

    http://tungwaiyip.info/software/HTMLTestRunner.html

    下载后手动拖到 python安装文件的Lib 目录下

    下载 Download 下的第二个文件test_HTMLTestRunner.py

    stream: 测试报告写入文件的存储区域 

    title: 测试报告的主题 

    description测试报告的描述

    修改引入HTMLTestRunner文件

      第94行:import StringIO 改为import io

      第539行:self.outputBuffer = StringIO.StringIO()

                改为:self.outputBuffer = io.StringIO()

      第631行:print >>sys.stderr, ' Time Elapsed: %s' % (self.stopTime-self.startTime)

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

      第642行:if not rmap.has_key(cls): 改为:if not cls in rmap:

      第766行:uo = o.decode(‘latin-1’)  改为:uo = o

      第772行:ue = e.decode(‘latin-1’)  改为:ue = e

     

     

    测试报告文件名:

    time.time():获取当前时间戳。

    time.ctime():当前时间的字符串形式。

    time.localtime():当前时间的struct_time形式。

    time.strftime():用来获得当前时间,可以将时间格式化为字符串。

    Python中时间日期格式化符号(区分大小写)如下页所示:

     

    now = time.strftime("%Y-%m-%d %H_%M_%S")    

    report_path = "C:\Users\laohu\workspace\pydev_test\case_piliang\report\" + now + "_result.html" 

      

    unittest装饰器 

    用 setUp与setUpClass 区别 

    • setup():每个测试 case运行前运行 
    • teardown():每个测试 case运行完后执行 
    • setUpClass():必须使用@classmethod 装饰器,所有case运行前只运行一次 
    • tearDownClass():必须使用@classmethod装饰器,所有case运行完后只运行一次
    • @是修饰符,classmethod 是python里的类方法 

    自动发邮件功能 

    自动发送邮件是自动化测试项目的重要需求之一。

    SMTP是简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。

    Python的smtplib模块提供了一种很方便的途径用来发送电子邮件。

    导入SMTP对象,具体使用方法。

    connect(host, port)方法参数说明如下:

    • host: 指定连接的邮箱服务器。
    • port:指定连接服务器的端口号。

    login(user, password)方法参数说明如下:

    • user:登录邮箱用户名。
    • password:登录邮箱密码。

    sendmail(from_addr, to_addrs, msg, …)方法参数说明如下:

    • from_addr:邮件发送者地址。
    • to_addrs:字符串列表,邮件发送地址。
    • msg:发送消息。
    • quit()方法:用于结束SMTP会话

    发送邮件的两种方式:

    方式一:自己邮箱的WEB页面,输入自己邮箱的用户名密码,打开发邮件页面,填写对方的邮箱地址及邮件标题与正文,完成后单击发送。

    方式二:下载安装客户端,填写邮箱账号、密码、邮箱服务器,一般的邮箱客户端会默认记下这些信息,后面与方法一相同。

    简单邮件发送验证

    # 引入

    import smtplib

    from email.mime.text import MIMEText

    from email.header import Header

    #发送邮箱服务器

    smtpserver ='smtp.sina.cn'

    #发送邮箱用户/密码

    user ='1234567890@sina.cn'

    password ='1234567890'

    #发送邮箱

    sender = '1234567890@sina.cn'

    #接收邮箱

    receiver = '987654321@qq.com'

    #发送邮箱主题

    subject = 'Python email test'

    #编写HTML类型的邮件正文

    msg = MIMEText("正文内容")

    msg['Subject'] = 'subject_test'

    msg['From'] = sender

    msg['To'] = receiver

    #连接发送邮件

    smtp =smtplib.SMTP()

    smtp.connect(smtpserver)

    smtp.login(user, password)

    smtp.sendmail(sender, receiver, msg.as_string())

    smtp.quit()

    发送带附件的邮件

    # 引入

    from email.header import Header

    from email.mime.text import MIMEText

    from email.mime.multipart import MIMEMultipart

    import smtplib, datetime

    def SendMailAttach():

        #发送邮箱服务器

        smtpserver = 'smtp.sina.cn'

        #发送邮箱用户/密码

        user = '1234567890@sina.cn'

        password = '1234567890'

        #发送邮箱

        sender = '1234567890@sina.cn'

    #接收邮箱

        receiver = '0987654321@sina.cn'

        msg = MIMEMultipart()

        att = MIMEText(open('D:\test\testTemplate.html', 'rb').read(), 'base64', 'gb2312')

        att["Content-Type"] = 'application/octet-stream'

        att["Content-Disposition"] = 'attachment; filename="testTemplate.html"'

        msg.attach(att)

        msg['From'] = sender

        msg['To'] = receiver

        msg['subject'] = Header('测试结果(' + str(datetime.date.today()) + ')','utf8')

        body = "Python test mail"

        msg.attach(MIMEText(body, 'plain'))

        smtp = smtplib.SMTP()

        smtp.connect(smtpserver)

        smtp.login(user,password)

        smtp.sendmail(msg['from'], msg['to'],msg.as_string())

        smtp.quit()

    SendMailAttach()

    查找最新的测试报告

     

    整合自动发邮件功能

      

      

     

  • 相关阅读:
    项目中libevent几个问题
    Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'dataSource' of bean class [com.liuyang.jdbc.PersonDao]: No property 'dataSource
    Can not issue data manipulation statements with executeQuery().
    Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut aaa
    VMware + LInux + Xshell 连接环境设置(心得体会)
    对于Serializable id类型的数据的测试
    SessionCacheTest03.testLoad Unrooted Tests initializationError
    java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing,
    java.lang.ExceptionInInitializerError Caused by: org.hibernate.InvalidMappingException: Unable to read XML
    Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource Caused by: org.hibernate.DuplicateMappingException: duplicate import: Person refers to both cn.itcast.
  • 原文地址:https://www.cnblogs.com/070727sun/p/12392228.html
Copyright © 2011-2022 走看看