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传送门

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

目录
相关文章
|
1天前
|
Web App开发 IDE 测试技术
深入理解自动化测试框架Selenium的设计与实践
【4月更文挑战第27天】在软件开发周期中,确保代码质量和功能正确性至关重要。随着敏捷开发的普及和持续集成/持续部署(CI/CD)的实践,自动化测试已成为现代开发工作流程的核心部分。本文将探讨一个广泛使用的开源自动化测试工具——Selenium,并剖析其设计原理、架构以及在实际中的应用。我们将通过具体案例分析,展示如何有效利用Selenium进行跨浏览器测试,并讨论在真实环境中可能遇到的挑战及解决方案。
|
8天前
|
Web App开发 JavaScript 前端开发
深入理解自动化测试框架Selenium的设计与实现
【4月更文挑战第20天】 在软件测试领域,自动化测试已成为提升测试效率和确保产品质量的关键手段。Selenium作为一款广泛使用的开源自动化测试框架,其设计精巧且功能强大,为Web应用提供了一种灵活、高效的测试解决方案。本文将深入探讨Selenium的核心架构与实现细节,解析其如何通过模拟用户操作来执行测试用例,以及它如何适应不断变化的Web技术标准。通过对Selenium内部机制的剖析,旨在帮助测试工程师更好地掌握该工具,并在测试实践中发挥其最大效能。
|
10天前
|
监控 测试技术 数据安全/隐私保护
如何将代理IP集成到自动化测试框架中?
如何将代理IP集成到自动化测试框架中?
|
12天前
|
敏捷开发 监控 前端开发
深入理解自动化测试框架Selenium的架构与实践
【4月更文挑战第16天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加快迭代速度的关键手段。Selenium作为一种广泛使用的自动化测试工具,其开源、跨平台的特性使得它成为业界的首选之一。本文旨在剖析Selenium的核心架构,并结合实际案例探讨其在复杂Web应用测试中的高效实践方法。通过详细解读Selenium组件间的交互机制以及如何优化测试脚本,我们希望为读者提供深入理解Selenium并有效运用于日常测试工作的参考。
|
12天前
|
自然语言处理 测试技术 API
深入理解自动化测试框架Selenium的设计理念与实践
【4月更文挑战第15天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加速迭代的关键手段。Selenium作为一种广泛使用的自动化测试框架,提供了对多种浏览器和平台的支持,极大地促进了Web应用的功能测试。本文旨在剖析Selenium的核心设计理念,探讨其在实际项目中的应用,并指出常见的误区及最佳实践,以期帮助测试工程师更高效地利用Selenium进行测试工作。
|
14天前
|
监控 测试技术 API
深入理解自动化测试框架Selenium的设计与实现
【4月更文挑战第14天】在软件开发过程中,自动化测试是确保代码质量、减少人工重复劳动的关键步骤。Selenium作为一款广泛使用的自动化测试工具,提供了对多种浏览器和操作系统的支持。本文将探讨Selenium的核心组件及其架构设计,分析其如何通过WebDriver与浏览器交互,以及它如何支持多种编程语言进行脚本编写。同时,我们还将讨论Selenium Grid的作用以及它如何实现并行测试,以缩短测试周期并提高测试效率。
178 59
|
30天前
|
敏捷开发 设计模式 监控
深入理解自动化测试框架的设计原则
在软件开发的复杂多变环境中,自动化测试已成为确保产品质量和加速市场交付的关键步骤。本文将探讨自动化测试框架的设计原则,包括模块化、可扩展性、易用性和可靠性,旨在为软件测试工程师提供构建高效、健壮且易于维护的自动化测试系统的指导。通过分析设计模式的应用,我们将了解如何减少代码冗余,提高测试覆盖率,并适应快速变化的技术要求。
|
16天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
1月前
|
Web App开发 Python
在ModelScope中,你可以使用Python的浏览器自动化库
在ModelScope中,你可以使用Python的浏览器自动化库
17 2
|
1月前
|
存储 BI 数据处理
Python自动化 | 解锁高效办公利器,Python助您轻松驾驭Excel!
Python自动化 | 解锁高效办公利器,Python助您轻松驾驭Excel!

热门文章

最新文章