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月前
|
Web App开发 人工智能 JavaScript
主流自动化测试框架的技术解析与实战指南
本内容深入解析主流测试框架Playwright、Selenium与Cypress的核心架构与适用场景,对比其在SPA测试、CI/CD、跨浏览器兼容性等方面的表现。同时探讨Playwright在AI增强测试、录制回放、企业部署等领域的实战优势,以及Selenium在老旧系统和IE兼容性中的坚守场景。结合六大典型场景,提供技术选型决策指南,并展望AI赋能下的未来测试体系。
|
6月前
|
数据采集 人工智能 自然语言处理
Playwright MCP 浏览器自动化框架全面解析
Playwright MCP是微软推出的开源项目,结合Playwright与MCP协议,让AI通过结构化数据直接操作浏览器。告别传统视觉识别,实现高效、精准的网页自动化,广泛应用于测试、爬虫、办公自动化等场景,大幅提升效率与可靠性。
|
9月前
|
Web App开发 开发框架 .NET
Playwright 自动化测试系列(6)| 第三阶段:测试框架集成​指南:参数化测试 + 多浏览器并行执行
Pytest 与 Playwright 集成可提升自动化测试效率,支持参数化测试、多浏览器并行执行及统一报告生成。通过数据驱动、Fixture 管理和并行优化,显著增强测试覆盖率与执行速度,适用于复杂 Web 应用测试场景。
|
9月前
|
测试技术 API C++
Playwright 自动化测试系列(7)| 第三阶段:测试框架集成​​Page Object 模式
本课程详解Playwright测试框架中的Page Object模式,通过电商登录-下单实战演示PO架构设计与高级技巧,结合Pytest实现多用户测试。重点解析PO模式提升代码复用性、降低维护成本的核心价值,并提供常见问题解决方案,助力构建高可维护性的自动化测试体系。
|
安全 Java 测试技术
python接口自动化(三)--如何设计接口测试用例(详解)
上篇我们已经介绍了什么是接口测试和接口测试的意义。在开始接口测试之前,我们来想一下,如何进行接口测试的准备工作。或者说,接口测试的流程是什么?有些人就很好奇,接口测试要流程干嘛?不就是拿着接口文档直接利用接口 测试工具测试嘛。其实,如果只是三五个接口,你可以这么做一个临时的接口测试。但是,如果是上百个接口,或者,你们公司的这个项目,第一次做接口测试,那么,我们还是很有必要严格遵守接口测试的流程。
645 0
python接口自动化(三)--如何设计接口测试用例(详解)
|
测试技术
正交试验测试用例设计及工具推荐
在科研和生产实践中,人们往往要做许多次实验来进行某项研究。实验条件一般包括很多因素,当因素的值不同时,实验的结果也不一样。如果想把每个因素的每个值都要实验一遍,总实验数就等于各因素的值的个数的乘积,而这个数往往很大,超过了可接受的成本。 例如,假设某个实验由A,B,C,D四个因素,每个因素都有10个不同的取值,那么如果想把每个因素都考虑到,我们需要做 10*10*10*10=10000次实验。 为了减少实验数目,我们必须选出那些最有代表性的例子。于是,就要用到了正交表法(Orthogonal Array Testing Strategy)。
988 0
正交试验测试用例设计及工具推荐
|
算法 安全 测试技术
【软件测试】测试用例的设计方法
测试用例写的过于简单,则可能失去了测试用例的意义,设计过于简单的测试用例其实并没有真正的进行设计,只是把需要测试的功能模块记录下来而已,它的作用仅仅是在测试过程中作为一个简单的测试计划,提醒测试人员测试的主要功能包括哪些而已,测试用例设计的本质应该是在设计的过程中理解需求,检验需求,并把对软件系统的测试方法的思路记录下来,以便指导将来的测试
【软件测试】测试用例的设计方法
|
安全 中间件 测试技术
【面试高频】给你一句话需求,让你设计测试用例,该怎么做?
【面试高频】给你一句话需求,让你设计测试用例,该怎么做?
【面试高频】给你一句话需求,让你设计测试用例,该怎么做?
|
消息中间件 缓存 中间件
【测试基础】二、我好像真的不会设计“好的”测试用例
【测试基础】二、我好像真的不会设计“好的”测试用例
【测试基础】二、我好像真的不会设计“好的”测试用例
|
JSON 前端开发 测试技术
手把手带你设计接口自动化测试用例(一):提取接口信息并分析
手把手带你设计接口自动化测试用例(一):提取接口信息并分析
1011 0
手把手带你设计接口自动化测试用例(一):提取接口信息并分析
下一篇
开通oss服务