• Pyhton3+AirTest+[002]+小程序UI自动化之Unittest+AirTest融合框架介绍


     

    一、目的

    1. 使用unittest的目的是为了批量执行测试用例

    二、框架

    1. 目录
    common:放一些公共类,例如:HTMLTestRunner等
    conf:放一些数据库,固定的设置和元素定位路径等
    data:放一些测试数据
    lib:同common
    loctor:读取conf/setting下的内容封装
    page:拿到loctor中的内容
    page_element:封装一些页面元素的定位等操作
    log:日志
    report:测试html报告
    testCase:测试用例目录
    utils:工具类
    caseList:测试用例执行顺序文件设置
    runCase:主入口
    1. 主入口
    #!/usr/bin/python3.8
    # -*- coding: utf-8 -*-
    # @Time    : 2021/7/17 10:48
    # @Author  : csjin
    # @File    : runCase.py
    
    import unittest
    from common import HTMLTestRunner_cn
    from  conf.settings import *
    
    #定义一个类AllTest
    class AllTest():
    
        # 初始化一些参数和数据
        def __init__(self):
    
            global resultPath
            # result/report.html
            resultPath = os.path.join(report_path, "report.html")
    
            # 配置执行哪些测试文件的配置文件路径
            self.caseListFile = os.path.join(root_path, "caseList.txt")
    
            # 真正的测试断言文件路径
            self.caseFile = os.path.join(root_path, "testCase")
    
            self.caseList = []
    
        def set_case_list(self):
              """
              读取caselist.txt文件中的用例名称,并添加到caselist元素组
              :return:
              """
              fb = open(self.caseListFile,encoding="utf-8")
              for value in fb.readlines():
                    data = str(value)
                    if data != '' and not data.startswith("#"):  # 如果data非空且不以#开头
                          self.caseList.append(data.replace("
    ", ""))  # 读取每行数据会将换行转换为
    ,去掉每行数据中的
    
              fb.close()
              print(self.caseList)
    
        def set_case_suite(self):
              """
    
              :return:
              """
              # 通过set_case_list()拿到caselist元素组
              self.set_case_list()
              # 创建测试套件
              test_suite = unittest.TestSuite()
              suite_module = []
    
              # 从caselist元素组中循环取出case
              for case in self.caseList:
                    # 通过split函数来将aaa/bbb分割字符串,-1取后面,0取前面
                    case_name = case.split("/")[-1]
                    # 打印出取出来的名称
                    # print(case_name + ".py")
                    # 批量加载用例,第一个参数为用例存放路径,第二个参数为规则
                    discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=case_name + '.py',top_level_dir=None)
                    # 将discover存入suite_module元素组
                    suite_module.append(discover)
                    # print('suite_module:'+str(suite_module))
              # 判断suite_module元素组是否存在元素
              if len(suite_module) > 0:
                    # 如果存在,循环取出元素组内容,命名为suite
                    for suite in suite_module:
                          # 从discover中取出test_name,使用addTest添加到测试集
                          for test_name in suite:
                                test_suite.addTest(test_name)
              else:
                    print('测试套件中无可执行的测试用例')
                    return None
              return test_suite
    
        def run(self):
              """
              run test
              :return:
              """
    
              print("*********TEST START*********")
              try:
    
                    # 调用set_case_suite获取test_suite
                    suit = self.set_case_suite()
                    # 判断test_suite是否为空
                    if suit is not None:
    
                          # 打开result/report.html测试报告文件,如果不存在就创建
                          fp = open(resultPath, 'wb')
                          # 调用HTMLTestRunner
                          runner = HTMLTestRunner_cn.HTMLTestRunner(stream=fp, title=start_mini_app+'小程序测试报告', description='测试用例执行结果')
                          # 通过HTMLTestRunner的run()方法来运行测试套件中的测试用例,并写入测试报告
                          runner.run(suit)
                    else:
                          print("Have no appCommon to test.")
              except Exception as ex:
    
                    print(str(ex))
    
    
              finally:
    
                    print("
    "+"*********TEST END*********")
                    fp.close()
              # # 判断邮件发送的开关
              # if on_off == 'on':
              #       send_mail.send_Mail(mail_path, "Sales Interface Test")
              # else:
              #       print("邮件发送开关配置关闭,请打开开关后可正常自动发送测试报告")
    
    
    
    if __name__ == '__main__':
          AllTest().run()
    1. caseList用例执行策略
    #这里存放要运行的case,带#号的测试用例不会执行
     
    # 打开小程序
    test_init_start_app/test_init_start_app
    当有些人一出生就有的东西,我们要为之奋斗几十年才拥有。但有一样东西,你一辈子都不会有,那就是我们曾经一无所有。
  • 相关阅读:
    Docker实践之03-Dockerfile指令详解
    Docker实践之02-使用镜像及定制
    通过Hack方式实现SDC中Stage配置联动刷新
    多级部门查询性能问题解决方案
    Docker实践之01-入门介绍
    从阿里腾讯2朵云产品中学到的用户体验
    HttpClient在多线程环境下踩坑总结
    一次对JDK进行"减肥"的记录
    北京西站如何进站接人
    多实例集群部署下的图片上传和访问
  • 原文地址:https://www.cnblogs.com/chushujin/p/15023824.html
Copyright © 2020-2023  润新知