zoukankan      html  css  js  c++  java
  • Python3+Selenium Web自动化测试案例分享⑺——执行用例及GitHub源码地址

    本章节主要讲解caselist.txt、config.ini、Main.py文件,以及展示测试报告、日志等。

    一、caselist.txt

    caselist存放需要执行的case名称(TestCase目录),不需要执行的时候就在case名称前加上"#"号注释掉,这样可以选择性的执行用例。

    二、config.ini

    config文件存放一些配置文件,格式如下:

    三、Main.py

    这是测试执行控制的方法,也是执行测试用例的入口,生成HTML可视化的测试报告。

    # _*_ coding:utf-8 _*_
    import unittest, os, time
    from Public import getPathInfo, readConfig, HTMLTestRunnerCN_py3,log
    #from tomorrow import threads
    
    nowtime = time.strftime("%Y%m%d%H%M%S")                          #定义当前时间变量
    log_info =log.logger                                             #定义log变量
    
    class All_Test(object):
        def __init__(self):
            global report_path
            path=getPathInfo.get_Path()
            report_path = os.path.join(path, 'Report')               #测试报告存放路径
            if not os.path.exists(report_path):                      #判断报告路径是否存在
                os.mkdir(report_path)                                #创建报告文件
            self.caselist_path = os.path.join(path, "caselist.txt")  #配置执行哪些测试文件的配置文件路径
            self.casepath = os.path.join(path, "TestCase")           #测试用例路径
            self.caselist = []                                       #定义一个空列表
    
        def get_caselist(self):
            '''
            读取caselist.txt文件中的用例名称,并添加到caselist元素组
            '''
            fb = open(self.caselist_path)                            #打开文件
            for value in fb.readlines():                             #遍历fb所有数据
                if value != '' and not value.startswith('#'):        #如果data非空且不以#开头
                    self.caselist.append(value.replace('
    ', ''))    #读取每行数据会将换行转换为
    ,去掉每行数据中的
    
            fb.close()                                               #关闭文件
            return self.caselist                                     #返回caselist
    
        def add_case(self):
            '''
            添加测试用例
            '''
            self.get_caselist()                                      #获得testcase列表
            suite = unittest.TestSuite()                             #创建测试套件
            suite_module = []                                        #定义一个列表
            for case in self.caselist:
                discover = unittest.defaultTestLoader.discover(self.casepath, pattern=case + '.py',top_level_dir=None)  # 加载测试用例
                suite_module.append(discover)                        #将测试用例添加到列表中
            if len(suite_module) > 0:                                #判断suite_module是否存在元素,列表才能判断长度,suite不能判断
                for i in suite_module:                               #如果存在,循环取出元素组内容,命名为suite
                    for j in i:
                        suite.addTest(j)                             #取出suite_module,使用addTest添加到测试集
                return suite                                         #返回测试集
            else:
                return None
    
        #@threads(6)
        def run_case(self):
            '''执行所有的用例, 并把结果写入测试报告'''
            log_info.info("######################### TEST START #########################")
            try:
                all_case = self.add_case()                                                      #调用add_case获取suite
                if all_case != None:
                    test_name = readConfig.Read_Config().get_info('Report', 'test_name')        #获取测试人员姓名
                    report_title = readConfig.Read_Config().get_info('Report', 'report_title')  #获取测试报告名称
                    fp = open(report_path + '\' + report_title + nowtime + '.html','wb')       #定义一个文件对象,给后面的HTMLTestRunner生成测试报告用,注意打开方式必须是wb
                    runner = HTMLTestRunnerCN_py3.HTMLTestRunner(stream=fp, title=report_title, description="用例测试情况",tester=test_name)  #生成HTML报告
                    runner.run(all_case)      #执行用例
                    fp.close()
                else:
                    log_info.info('---测试套件为空---')
            except Exception as e:
                log_info.error(str(e))
    
            finally:
                log_info.info("######################### TEST END #########################")
    
    if __name__ == '__main__':
        result=All_Test()
        result.run_case()                               #执行用例

    四、测试报告

    断言失败的会截图显示在报告中。 

    五、日志

     

    六、源码地址

    https://github.com/xiongye105554598/NetEase_Email_Auto-POM.git

    —————————————————————————————— 选择正确的事、再把事做正确 ——————————————————————————————
  • 相关阅读:
    UE4 WCF RestFul 服务器 读取JSON 数据并解析 简单实例
    Android aidl Binder框架浅析
    AIDL
    android 五种存储方式
    Android进程间通信机制
    Service全面总结
    Android平台中关于音频播放
    Android广播机制
    Cursor,CursorAdapter中的观察者模式解析
    ContentProvider和Uri详解
  • 原文地址:https://www.cnblogs.com/airb/p/13490320.html
Copyright © 2011-2022 走看看