zoukankan      html  css  js  c++  java
  • Selenium2+python自动化72-logging日志使用【转载】

    前言

    脚本运行的时候,有时候不知道用例的执行情况,这时候可以加入日志,这样出现问题后方便查阅,也容易排查哪些用例执行了,哪些没有执行。

    一、封装logging模块

    1.关于logging日志的介绍,我这里就不详细讲解了,主要有两大功能,一个是控制台的输出,一个是保存到本地文件

    2.先封装logging模块,保存到common文件夹命名为logger.py,以便于调用

    # coding:utf-8
    import logging,time,os
    # 这个是日志保存本地的路径
    log_path = "D:\test\newp\report"
    class Log:
        def __init__(self):
            # 文件的命名
            self.logname = os.path.join(log_path, '%s.log'%time.strftime('%Y_%m_%d'))
            self.logger = logging.getLogger()
            self.logger.setLevel(logging.DEBUG)
            # 日志输出格式
            self.formatter = logging.Formatter('[%(asctime)s] - %(filename)s[line:%(lineno)d] - fuc:%(funcName)s- %(levelname)s: %(message)s')
        def __console(self, level, message):
            # 创建一个FileHandler,用于写到本地
            fh = logging.FileHandler(self.logname, 'a')  # 追加模式
            fh.setLevel(logging.DEBUG)
            fh.setFormatter(self.formatter)
            self.logger.addHandler(fh)

            # 创建一个StreamHandler,用于输出到控制台
            ch = logging.StreamHandler()
            ch.setLevel(logging.DEBUG)
            ch.setFormatter(self.formatter)
            self.logger.addHandler(ch)

            if level == 'info':
                self.logger.info(message)
            elif level == 'debug':
                self.logger.debug(message)
            elif level == 'warning':
                self.logger.warning(message)
            elif level == 'error':
                self.logger.error(message)
            # 这两行代码是为了避免日志输出重复问题
            self.logger.removeHandler(ch)
            self.logger.removeHandler(fh)
            # 关闭打开的文件
            fh.close()

        def debug(self, message):
            self.__console('debug', message)

        def info(self, message):
            self.__console('info', message)

        def warning(self, message):
            self.__console('warning', message)

        def error(self, message):
            self.__console('error', message)

    if __name__ == "__main__":
       log = Log()
       log.info("---测试开始----")
       log.info("输入密码")
       log.warning("----测试结束----")

    二、log保存本地

    1.logger模块的封装在9.2章节,我的用例参考目录如下

    2.先设置保存log到本地的文件路径地址,如:log_path = "D:\test\newp\report"

    三、用例代码

     

    以下是简单的一个百度的搜索案例仅供参考

    # coding:utf-8

    import unittest,time

    from common.logger import Log

    from selenium import webdriver

    log = Log()

    class Test(unittest.TestCase):

        def setUp(self):

            self.driver = webdriver.Firefox()

            self.driver.get("https://www.baidu.com")

            self.driver.implicitly_wait(30)

        def test_01(self):

            log.info("-------测试用例开始---------")

            self.driver.find_element_by_id("kw").send_keys("yoyo")

            log.info("输入内容:yoyo")

            self.driver.find_element_by_id("su").click()

            log.info("点击按钮:id = su")

            time.sleep(2)

            t = self.driver.title

            log.info(u"获取title内容:%s"%t)

            self.assertIn(u"百度搜索",t)

        def tearDown(self):

            self.driver.quit()

            log.info("-------测试用例结束----------")

    if __name__ == "__main__":

        unittest.main()

    四、运行结果:

    1.执行run_all脚本(3.9章节)

    2.打开存放日志文件的目录,找到log文件

    3.打开报告,看到的效果如下

  • 相关阅读:
    python数据结构树和二叉树简介
    python双向链表的实现
    Python单向链表的实现
    栈和队列数据结构的基本概念及其相关的Python实现
    模型融合目录
    算法汇总目录
    一个完整的机器学习目录
    python基础-面向对象opp
    Python random模块
    python-字符串前面添加u,r,b的含义
  • 原文地址:https://www.cnblogs.com/caoj/p/7815797.html
Copyright © 2011-2022 走看看