在自动化脚本执行过程中,使用Python的日志模块记录在测试用例执行过程中一些重要信息或者错误日志等,用于监控和后续调试脚本。

在pycharm下新建工程,并创建Log.py、Logger.conf以及test_use_log.py三个文件。

Logger.conf配置文件,内容如下:

################################################
###########propagate 是否继承父类的log信息,0:否
[loggers]
keys=root,example01,example02
[logger_root]
level=DEBUG
handlers=hand01,hand02
[logger_example01]
handlers=hand01,hand02
qualname=example01
propagate=0
[logger_example02]
handlers=hand01,hand03
qualname=example02
propagate=0
###############################################
[handlers]
keys=hand01,hand02,hand03
[handler_hand01]
class=StreamHandler
level=DEBUG
formatter=form01
args=(sys.stderr,)
[handler_hand02]
class=FileHandler
level=DEBUG
formatter=form01
args=('e:\\AutoTestLog.log', 'a')
[handler_hand03]
class=handlers.RotatingFileHandler
level=INFO
formatter=form01
args=('e:\\AutoTestLog.log', 'a', 10*1024*1024, 5)
###############################################
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
[formatter_form02]
format=%(name)-12s: %(levelname)-8s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

Log.py文件内容:

#encoding=utf-8
import logging.config
import logging logging.config.fileConfig("Logger.conf")
logger = logging.getLogger("example01") #日志配置文件:多个logger,每个logger,指定不同的handler
#handler:设定了日志输出行的格式
#handler:以及设定写日志到文件(是否回滚)?还是到屏幕
#handler:还定了打印日志的级别。 def debug(message):
# 打印debug级别的日志方法
print "debug"
logger.debug(message) def warning(message):
# 打印warning级别的日志方法
logger.warning(message) def info(message):
# 打印info级别的日志方法
logger.info(message) if __name__=="__main__":
debug("hi")
info("gloryroad")
warning("hello")

test_use_log.py文件内容如下:

#encoding=utf-8
from selenium import webdriver
import unittest
import logging # 从当前文件所在目录中导入Log.py文件中所有内容
from Log import * class TestSoGouByObjectMap(unittest.TestCase): def setUp(self):
# 启动Firefox浏览器
self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer") def testSoGouSearch(self):
debug(u"============== 搜索 ==============")
url = "http://www.sogou.com"
# 访问搜狗首页
self.driver.get(url)
debug(u"访问sogou首页")
self.driver.find_element_by_id("query").send_keys(u"光荣之路自动化测试")
warning(u"在输入框中输入搜索关键字串“光荣之路自动化测试”"+self.driver.find_element_by_id("query").get_attribute("value"))
self.driver.find_element_by_id("stb").click()
info(u"点击搜索按钮")
debug(u"========== 测试用例执行结束 ==========") def tearDown(self):
# 退出IE浏览器
self.driver.quit() if __name__ == '__main__':
unittest.main()

执行成功会在e:\\AutoTestLog.log产生日志记录,可点击查看。

最新文章

  1. codeforces 577B B. Modulo Sum(水题)
  2. POJ1002 487-3279
  3. 定义函数def
  4. 设计模式之Adapter(适配器模式)
  5. PHP获取上个月、下个月、本月的日期(strtotime(),date())
  6. IDE模式下安装Windows 7强行改回ACHI后不断重启的解决方法
  7. CDH安装Hadoop
  8. Spark笔记--使用Maven编译Spark源码(windows)
  9. COM编程入门第二部分——深入COM服务器
  10. MySQL安装指南
  11. 我的Linux软件集
  12. 【linux】线上服务器要关注哪些参数
  13. python语法_模块
  14. 寒假作业 pta编程总结3
  15. Jmeter5 实现多机集群压测(局域网组成多机集群)
  16. Linux的SSH免密登录认证过程研究
  17. 使用Mac命令别名,提升工作效率
  18. JAVA_吸血鬼数字 多种方法实现
  19. CORBA(Common Object Request Broker Architecture,公共对象请求代理体系结构,通用对象请求代理体系结构)是由OMG组织制订的一种标准的面向对象应用程序体系规范
  20. 关于MVC打印问题,打印指定的内容

热门文章

  1. springBoot jpa 表单关联查询
  2. 编写Servlet,验证用户登录,如果用户名与密码都为“admin”则验证通过,跳转欢迎页面,否则弹出提示信息“用户名或密码错误,请重新输入!”,点击“确定”后跳转至登录页面
  3. Unity的stateMachineBehaviour
  4. Linux Centos7.2 编译安装PHP7.0.2
  5. 如何变更站点 AD 域服务器IP地址
  6. JS实用技术
  7. 03_5_static关键字
  8. js函数式编程(一)-纯函数
  9. NOIP模拟赛 准考证号
  10. 常用的windows小工具指令和如何打开自定义的程序