Appium自动化框架从0到1之 执行测试用例& 生成测试报告&发送邮件

简介: Appium自动化框架从0到1之 执行测试用例& 生成测试报告&发送邮件

1.运行测试用例&生成测试报告


直接上代码:

TestRunnerToReport.py

# -*- coding: utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-7-10
"""
import unittest
import HTMLTestRunner
import  time
import logging
import sys
'''
这个文件,只执行以下两个步骤:
1.执行用例
2.保存报告
'''
#测试用例文件路径
test_dir = '../test_case'
#测试报告文件路径
report_dir = '../report/'
#获取当前时间
now = time.strftime('%Y-%m-%d %H_%M_%S',time.localtime(time.time()))
#生成测试报告文件格式
report_name = report_dir + now +'test_report.html'
#加载测试用例
# discover = unittest.defaultTestLoader.discover(test_dir,pattern='test_login.py')
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
#生成并运行测试用例
with open(report_name,'wb') as f:
    runner = HTMLTestRunner.HTMLTestRunner(stream=f, title=" xxx项目移动APP Test Report", description=" Andriod app Test Report")
    logging.info('start run testcase')
    runner.run(discover)


2.运行测试用例&生成报告&邮件发送


2.1邮箱信息配置


2.1.1在yal文件中配置邮箱信息


email.yaml

smtp_server: smtp.qq.com
port: 465
#sender: carl_dj@xx.com
#psw: carl_dj
sender: carl_dj@xx.com
psw: carl_dj
subject: xxx项目APP自动化测试报告
receiver: xiaoyu1@xx.com,xiaoyu2@xx.com

2.2.2配置全局变量


把所有的地址信息都写在一个python文件中,用到的时候,直接读取

golbalparm.py

# -*- coding:utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-7-10
"""
import os
#获取项目根目录地址
 cur_path = os.path.dirname(os.path.abspath('.'))
# print(cur_path)
#测试用例地址
test_case_path = os.path.join(cur_path,'test_case')
# print(test_case_path)
#线上数据文件地址
formal_data_path = os.path.join(cur_path,'data\\formal_data\\')
# print(formal_data_path)
#测试数据文件地址
test_data_path = os.path.join(cur_path,'data\\test_data\\')
# print(test_data_path)
#截图地址
# test_screenshot_path = os.path.join(cur_path,'report\\img\\')
# print(test_screenshot_path)
#测试报告地址
test_report_path = os.path.join(cur_path,'report\\testreport')
# print(test_report_path)
#日志地址
test_log_path = os.path.join(cur_path,'report\\logs')
# print(test_log_path)
#保存截图地址
test_img_path = os.path.join(cur_path,'report\\img')
print(test_img_path)
#浏览器驱动地址
dirver_path = os.path.join(cur_path,'driver')
# print(dirver_path)
#config文件地址
config_path = os.path.join(cur_path,'config')
# print(config_path)


如果有os模块不太明白的,可以直接百度。


2.2通过邮件发送测试报告


这个.py文件包含4个步骤:

>>第一步:加载用例

>>第二步:执行用例

>>第三步:获取最新的测试报告

>>第四步:发送邮件

接下来,我们就直接看代码:

TestRunnerToEmail.py

# -*- coding:utf-8 -*-
"""
@ auth : carl_DJ
@ time : 2020-7-10
"""
import os
import unittest
import time
import HTMLTestRunner
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
from config import globalparam
from public.common.log import Logger
from public.common.get_email_parameter import GetEmailParameter
"""
执行用例并发送报告,分四个步骤:
第一步:加载用例
第二步:执行用例
第三步:获取最新的测试报告
第四步:发送邮件
"""
# 获取当前脚本的真实路径
# cur_path =os.path.dirname(os.path.abspath('.'))
log = Logger(logger='run').getlog()
def add_case( rule='test*.py'):
    '''
    作用:加载所有测试用例
    :param caseName:
    :param rule:
    :return:
    '''
    test_suite = globalparam.test_case_path
    discov = unittest.defaultTestLoader.discover(test_suite,pattern=rule)
    return discov
    # case_path = os.path.join(globalparam.test_case_path,caseName)
    # if not os.path.exists(case_path):os.mkdir(case_path)
    # 定义discover方法的参数(执行全部用例)
    # discover = unittest.defaultTestLoader.discover(case_path, pattern=rule, top_level_dir=None)
    # return discover
def run_case(all_case, reportName=''):
    '''
    作用:执行所有的用例,并把执行结果写入HTML测试报告中
    :param all_case:
    :param reportName:
    :return:
    '''
    now = time.strftime('%Y-%m-%d_%H_%M_%S', time.localtime(time.time()))
    report_path = os.path.join(globalparam.test_report_path,reportName)
    if not os.path.exists(report_path):os.mkdir(report_path)
    report_abspath = os.path.join(report_path, now + 'xx项目web自动化测试报告.html')
    with open(report_abspath, 'wb') as fp:
        runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='xx项目web自动化测试报告', description='用例执行情况')
        runner.run(all_case)
def get_report_file(report_path):
    '''
    作用: 获取最新的测试报告
    :param report_path:
    :return:
    '''
    lists = os.listdir(report_path)
    lists.sort(key=lambda fn:os.path.getmtime(os.path.join(report_path, fn)))
    print('最新测试报告:' + lists[-1])
    # 找到最新的测试报告文件
    report_file = os.path.join(report_path, lists[-1])
    return report_file
def send_mail(subject, sender, psw, receiver, smtpserver, report_file, port):
    """
    作用:将最新的测试报告通过邮件进行发送
    :param sender:发件人
    :param psw:QQ邮箱授权码
    :param receiver:收件人
    :param smtpserver:QQ邮箱服务
    :param report_file:
    :param port:端口
    :return:
    """
    with open(report_file, 'rb') as f:
        mail_body = f.read()
    # 定义邮件内容
    msg = MIMEMultipart()
    body = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    msg['Subject'] = subject
    msg['from'] = sender
    msg['to'] = ','.join(receiver)
    msg.attach(body)
    # 添加附件
    att = MIMEText(open(report_file, 'rb').read(), 'base64', 'utf-8')
    att["Content-Type"] = "application/octet-stream"
    att["Content-Disposition"] = 'attachment; filename= "WebUIAutoFramework_report.html"'
    msg.attach(att)
    try:
        smtp = smtplib.SMTP_SSL(smtpserver, port)
    except:
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver, port)
    # 用户名和密码
    smtp.login(sender, psw)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
    log.info('测试报告邮件发送成功')
if __name__ == '__main__':
    # 加载用例
    all_case = add_case()
    # 执行用例
    run_case(all_case)
    # 获取最新的测试报告
    report_path =globalparam.test_report_path
    # report_path = os.path.join(config.cur_path,'report\\testreport')
    report_file = get_report_file(report_path)
    # 邮箱配置
    subject = GetEmailParameter().email_parameter()['subject']
    sender = GetEmailParameter().email_parameter()['sender']
    psw = GetEmailParameter().email_parameter()['psw']
    smtp_server = GetEmailParameter().email_parameter()['smtp_server']
    port = GetEmailParameter().email_parameter()['port']
    receiver = GetEmailParameter().email_parameter()['receiver'].split(',')
    send_mail(subject, sender, psw, receiver, smtp_server, report_file, port)



嗯,本来,邮件发送的这个代码不想在这里展示,因为selenium框架从0到1 的教程中已经写了,

奈何小鱼又考虑到为各位大佬节省时间和精力,同时也能做出对比,就在写一次!


哦对了,接下来,我就把github上的源码地址,share出来:github传送门

接下来,就要多多练习哦,争取早日成为大神!!

目录
相关文章
|
8天前
|
机器学习/深度学习 人工智能 算法
探索软件测试中的自动化与人工智能
【5月更文挑战第31天】本文将深入探讨自动化和人工智能如何革新软件测试领域,通过实例分析它们如何提高测试效率和准确性,并预测未来发展趋势。
|
4天前
|
机器学习/深度学习 人工智能 算法
探索软件测试的未来:AI与自动化的融合
【6月更文挑战第3天】随着人工智能和自动化技术的不断进步,软件测试领域正经历着一场革命。本文将探讨这两种技术如何相互补充,提高测试效率和质量,以及它们对未来软件测试实践的潜在影响。
|
4天前
|
测试技术 Python
【Python自动化测试】:Unittest单元测试与HTMLTestRunner自动生成测试用例的好帮手
【Python自动化测试】:Unittest单元测试与HTMLTestRunner自动生成测试用例的好帮手
3 0
|
4天前
|
机器学习/深度学习 人工智能 算法
探索软件测试的新时代:AI驱动的自动化
【6月更文挑战第4天】随着人工智能技术的不断进步,软件测试领域正经历着一场革命。本文将探讨AI如何改变传统的软件测试方法,提高测试效率和准确性,以及这一趋势对测试工程师未来技能要求的影响。
17 6
|
4天前
|
Java 测试技术 数据处理
Java一分钟之-TestNG:高级测试框架
【6月更文挑战第4天】TestNG是Java的高级测试框架,扩展了JUnit,支持数据驱动、参数化、测试分组、依赖和并行测试,提高自动化测试效率。本文介绍了TestNG的核心特性,如`@DataProvider`和`@Parameters`注解,以及常见问题和解决策略,如正确使用测试生命周期方法和处理数据驱动测试中的数据。通过示例展示了如何进行数据驱动测试,帮助读者更好地理解和应用TestNG。
24 0
Java一分钟之-TestNG:高级测试框架
|
4天前
|
敏捷开发 数据管理 测试技术
软件测试中的自动化策略与实践
【6月更文挑战第4天】本文深入探讨了在现代软件开发生命周期中,自动化测试的重要性及其实施策略。通过分析自动化测试的优势、挑战以及最佳实践,旨在为读者提供一套全面的自动化测试解决方案,以提升软件质量并加速交付速度。
|
4天前
|
XML Web App开发 Java
【软件测试】关于Web自动化测试
【软件测试】关于Web自动化测试
|
4天前
|
存储 数据管理 测试技术
构建Python构建自动化测试框架(原理与实践)
当谈到软件质量保证时,自动化测试是一个不可或缺的步骤。Python作为一种简单易学的编程语言,具有丰富的测试框架和库,使得构建自动化测试框架变得相对简单。本文将介绍如何使用Python构建自动化测试框架,包括选择合适的测试框架、编写测试用例、执行测试和生成报告等方面。
构建Python构建自动化测试框架(原理与实践)
|
5天前
|
Java 测试技术 Web App开发
《手把手教你》系列技巧篇(六十二)-java+ selenium自动化测试-RemoteWebDriver让你的代码与测试分离(远程测试)
【6月更文挑战第3天】本文介绍了在没有本地浏览器的情况下,如何使用RemoteWebDriver进行远程自动化测试。RemoteWebDriver分为客户端和服务端,客户端运行测试代码,服务端启动服务。服务端需要安装JDK、浏览器和对应的WebDriver,并启动selenium-server-standalone.jar。客户端通过URL连接到服务端,并指定预期的浏览器类型。这样,客户端的测试代码就能远程控制服务端的浏览器执行自动化测试。RemoteWebDriver的优点包括跨平台和浏览器测试、提高测试稳定性以及使测试环境和执行代码的机器分离。
22 3
|
5天前
|
Java 测试技术
Java一分钟之-JUnit测试框架:断言与测试套件
【6月更文挑战第3天】本文介绍了JUnit在Java单元测试中的应用,包括断言基础如`assertEquals`、`assertTrue`等,用于验证代码预期结果;利用`@Suite`创建测试套件以组合多个测试;并讨论了常见问题及解决方法,如忽略测试、错误断言、异常处理和保持测试简洁。理解并熟练运用这些概念能提升测试代码的质量和效率。
27 2

热门文章

最新文章