Python+requests+unittest接口自动化测试框架--邮件发送测试报告

自己写的接口自动化测试框架,可发送详细测试报告,不喜欢啰嗦,根据以下步骤,可以快速实现接口自动化测试,有不理解的,欢迎留言讨论,感谢各位老师同学指导批评。

1、以下是readConfig读取配置文件方法

#encoding:utf-8

import os
import configparser

def getConfig(section,key):
    config = configparser.ConfigParser()
    #获取配置文件的真实路径
    path = os.path.split(os.path.realpath(__file__))[0] + '\config.ini'
    #print("获取配置文件的真实路径为:"+path)
    config.read(path,encoding="utf-8")
    return config.get(section,key)

2、引入所需模块

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
import HTMLTestRunner
import time
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import readConfig  #为自定义读取配置文件方法

3、正文代码

# 用例路径
case_path = "D:\\workspace\\interface_test\\testCase"
print(case_path)

# 报告存放路径
report_path = os.path.join(os.getcwd(),'report')
print(report_path)

#定义发送邮件
def new_report(testreport):
    lists = os.listdir(testreport)
    lists.sort(key=lambda fn:os.path.getmtime(testreport+'\\'+fn)) #获取一个文件中的最近访问时间的文件
    file_new = os.path.join(testreport,lists[-1])
    print("==========获取最近时间生成的报告文件路径===========>"+file_new)
    return file_new

def send_mail(new_report,filename):
    sender = readConfig.getConfig("EMAIL","sender") #读取配置文件中发件人
    sendpwd = readConfig.getConfig("EMAIL","mail_pass") #读取配置文件中发件人密码
    receiver = readConfig.getConfig("EMAIL","receiver") #读取配置文件中收件人

    f = open(new_report,'rb') #获取报告文件
    body_main = f.read()

    # 邮件标题
    msg = MIMEMultipart()
    msg['Subject'] = Header('接口自动化测试报告','utf-8')
    msg['From'] = sender
    msg['To'] = receiver

    #邮件内容
    text = MIMEText(body_main,'html','utf-8')
    msg.attach(text)

    #发送邮件
    att = MIMEApplication(open(filename,'rb').read())
    att['Content-Type'] = 'application/octet-stream'
    att.add_header('Content-Disposition','attachment',filename = filename)
    msg.attach(att)

    try:
        smtp = smtplib.SMTP()
        smtp.connect(readConfig.getConfig("EMAIL","mail_host"))
        smtp.login(sender,sendpwd)
        smtp.sendmail(sender,receiver.split(","),msg.as_string())
        print('mail has been send successfully')
    except Exception as e:
        print(e)


def creatsuite():  #创建测试用例集
    testsuite = unittest.TestSuite()
    discover = unittest.defaultTestLoader.discover(
        start_dir=case_path,
        pattern="test*.py",
        top_level_dir=None,
    )
    for allcase in discover:
        for case in allcase:
            testsuite.addTests(case)
    return testsuite

if __name__ == "__main__":
    # 1、按照一定格式获取当前时间
    now = time.strftime('%Y-%m-%d_%H-%M-%S',time.localtime(time.time()))

    # 2、html报告文件路径
    filename = os.path.join(report_path,now+"result.html")
    print("测试报告路径==========================>",filename)

    # 3、打开一个文件,将result写入此file中
    fp = open(filename,'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
                            title="chen测试用例",
                            description="测试情况,如下:",
                            verbosity=2)
    # 4、调用add_case函数返回值
    runner.run(creatsuite())
    fp.close()

    # 5、执行发送邮件
    new_report = new_report(report_path)
    send_mail(new_report,filename)

你可能感兴趣的:(Python学习)