Python web自动化测试框架搭建(功能&接口)——通用模块

1、通用模块:

  • config.conf: 公共配置文件,配置报告、日志、截图路径,以及邮件相关配置
  • [report]
    reportpath = E:\workspace\WebAutomation\src\functiontest\Report\2017-07-18
    screen_path = E:\workspace\WebAutomation\src\functiontest\Report\2017-07-18\Screenshoots
    report_path = E:\workspace\WebAutomation\src\functiontest\Report\2017-07-18\Report\TestReport-2017-07-18-15-23-06.html
    log_path = E:\workspace\WebAutomation\src\functiontest\Report\2017-07-18\Logs\2017-07-18-15-23-06.log
    
    [mail]
    mail_from = xxx
    mail_tolist = xxx
    mail_host = mail.xx.com
    mail_user = xxxx
    mail_pass = aGFpbmFuNVU1Ng==

  • logger: 日志模块

  • main.py: 执行器,负责执行整体测试任务模块

  • testrunner.py: 负责测试用例执行和结果收集

  • utils.py: 公共方法,如创建报告文件夹、生成测试报告、发送邮件


2、日志模块:


#coding:utf-8
import logging.handlers
import ConfigParser

class Loger(logging.Logger):
    def __init__(self, filename=None):
        super(Loger, self).__init__(self)
        # 日志文件名
        conf = ConfigParser.ConfigParser()
        conf.read("config.conf")
        filename = conf.get("report", "log_path")
        self.filename = filename
        
        # 创建一个handler,用于写入日志文件
        fh = logging.handlers.RotatingFileHandler(self.filename, 'a')
        fh.setLevel(logging.DEBUG) 

        # 再创建一个handler,用于输出到控制台 
        ch = logging.StreamHandler() 
        ch.setLevel(logging.DEBUG) 

        # 定义handler的输出格式 
        formatter_fh = logging.Formatter('[%(asctime)s] - %(filename)s [Line:%(lineno)d] - [%(levelname)s] - %(message)s') 
        formatter_ch = logging.Formatter('[%(asctime)s] - %(message)s') 
        fh.setFormatter(formatter_fh) 
        ch.setFormatter(formatter_ch) 

        # 给logger添加handler 
        self.addHandler(fh) 
        #self.addHandler(ch) 


3、执行模块:


# coding=utf-8
import unittest
import os
import Utils
from sys import argv
 
def runTest(case_dir, patter):
    import TestRunner
    reload(TestRunner)
    discover = unittest.defaultTestLoader.discover(case_dir+"\\testcases", pattern=patter)
    runner = TestRunner.AutoTestRunner()
    result, infos = runner.run(discover)
    return result, infos

def run(cadir):
    filename = Utils.createFolder(cadir[0])  #创建文件夹
    import Logger
    log = Logger.Loger()
    log.info(cadir[2] + u"测试开始")
    log.info(u"开始创建文件夹和文件")
    log.info(u"日志文件:"+filename[0])
    log.info(u"报告文件:"+filename[1])
    log.info(u"文件夹和文件创建成功")
    log.info(u"开始执行测试用例")
    result, infos = runTest(cadir[0], cadir[1])  #收集和执行测试用例
    log.info(u"测试用例执行完成,开始写入报告")
    if cadir[2] == "functiontest":
        Utils.createReport(result, infos, filename, cadir[3])  #测试结果写入报告
    log.info(u"报告写入结束,测试结束")
    log.info(u"开始发送邮件……")
    isSuccess = Utils.sendMail(filename[1],cadir[3])
    log.info(isSuccess)
    log.info("================================================================\n")

if __name__ == '__main__':
    projectpath = os.path.dirname(os.path.realpath(__file__))
    test_dir = projectpath + '\\functiontest\\'  #功能测试用例路径
    test_dir1 = projectpath + '\\interfacetest\\'  #接口测试用例路径
    casedirs = []
    #argv=["","all"]
    if argv[1] == "interface":
        casedirs.append([test_dir1, "*TestCase.py", "interfacetest","接口"])
    elif argv[1] == "function":
        casedirs.append([test_dir, "TestCase*.py", "functiontest","功能"])
    else:
        casedirs.append([test_dir1, "*TestCase.py", "interfacetest","接口"])
        casedirs.append([test_dir, "TestCase*.py", "functiontest","功能"])
        
    for cadir in casedirs:
        run(cadir)


4、创建报告文件夹:


"""创建报告文件夹、日志文件夹、截图文件夹、日志文件、报告文件"""    
def createFolder(test_path):
    # conf = ConfigParser.ConfigParser()
    # conf.read("config.conf")    
    # reportFolder = conf.get("result", "resultpath") + time.strftime('%Y-%m-%d', time.localtime(time.time()))
    reportFolder = test_path + "Report\\" + time.strftime('%Y-%m-%d', time.localtime(time.time()))
    log_path = reportFolder + "\\Logs"
    screen_path = reportFolder + "\\Screenshoots"
    report_path = reportFolder + "\\Report"
    pathlist = [report_path, log
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值