pythonnose框架_python nose测试框架全面介绍七--日志相关

引:

之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四。

但使用一段时间后,发出一个问题,生成的报告只有错误提示,没有日志,查看nose的官网,nose默认支持将日志显示的,如下:

脚本如下:

#coding:utf-8

‘‘‘Created on 2016年6月22日

@author: huzq‘‘‘

importloggingfrom test_case importnewfrom nose.tools importok_from nose.tools importeq_importnoseimportosfrom nose.plugins.attrib importattrfrom nose.plugins.skip importSkipTestimportsys#TODO:jfjfjf

log = logging.getLogger(__name__)deftest_learn_1():

u‘‘‘测试取消‘‘‘

print ‘xxx‘log.info("afdffdfdfd")#raise SkipTest

#print "test_lean_1"

#pass

#assert 1==2

eq_(7, 9, msg=u"错误")

test_learn_1.slow=1@attr(mode=2)deftest_lean_2():

u‘‘‘测试失败‘‘‘

try:print "test_learn_2"ok_(4==3,msg="xxx")printsys._getframe().f_code.co_nameexceptException:printsys._getframe().f_code.co_name

@attr(mode=2)deftest_lean_3():

u‘‘‘测试成功‘‘‘

pass

defsetUp():#set_trace()

globalaprint "0001 test setUp"

#addCleanup(aa)

deftearDown():print "0001 test teardown"a=‘resource setup‘b=‘c‘

#assert a==b

printa

可以看出,报告中将日志及print的日志也都打印出来了。

问题分析

但存在一个问题是,日志日志,格式好像不太美观

支持以下几个参数:

--nologcapture

Disable logging capture plugin. Logging configuration will be left intact. [NOSE_NOLOGCAPTURE]

不抓log--logging-format=FORMAT

Specify custom format toprintstatements. Uses the same format as used by standard logging handlers. [NOSE_LOGFORMAT]

自定义log格式--logging-datefmt=FORMAT

Specify custom date/time format to printstatements. Uses the same format as used by standard logging handlers. [NOSE_LOGDATEFMT]

log时间格式--logging-filter=FILTER

Specify which statements to filterin/out. By default, everything is captured. If the output is too verbose, use this option to filter out needless output. Example: filter=foo will capture statements issued ONLY to foo or foo.what.ever.sub but not foobar or other logger. Specify multiple loggers with comma: filter=foo,bar,baz. If any logger name is prefixed with a minus, eg filter=-foo, it will be excluded rather than included. Default: exclude logging messages from nose itself (-nose). [NOSE_LOGFILTER]

log过滤--logging-clear-handlers

Clear all other logging handlers--logging-level=DEFAULT

Set the log level to capture

接下来我们就通过实例来演示下

--nologcapture 这个就不解释了,不会抓取日志

--logging-format:

默认格式是:

logformat = ‘%(name)s: %(levelname)s: %(message)s‘

可以看出,默认格式是没有日期的,我们可以重新定义日期

nosetests -v test_case_0001.py l --logging-format=%(asctime)s:%(name)s:%(levelname)s:%(message)s

nosetests-v test_case_0001.py --logging-format="%(asctime)s:%(name)s:%(levelname)s: %(message)s"

注意,带空格的日期必须要双引号扩起来,单引号不行

结果如下

--logging-filter

将日志过滤,比如要有多文件要运行时,不同的日志要过滤,可以使用该参数

nosetests -v test_case_0001.py --logging-filter=root

只过滤root的日志

使用文件来定义参数

在参数一多时,每次运行要输那么多参数,不方便,可以使用文件形式来定义

nose执行时,默认使用home目录下的.noserc或者nose.cfg文件,也可以自己写文件如下

[nosetests]

verbosity=2logging-format=%(asctime)s%(name)s:%(levelname)s:%(message)s

执行时,使用-c指定文件即可

nosetests -v test_case_0001.py -c nose.ini

遗留问题:

在运行测试时,本想同时使用--logging-file及--logging-format来同时在运行时显示日志及运行后抓取日志至报告。

但--logging-file是最高级别,会忽略其它日志配置。

so,想同时看日志或结果报告中带日志只能二选一了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值