• python nose测试框架全面介绍七--日志相关


    引:

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

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

    脚本如下:

    #coding:utf-8
    '''
    Created on 2016年6月22日
    @author: huzq
    '''
    
    import logging
    from test_case import new
    from nose.tools import ok_
    from nose.tools import eq_
    import nose
    import os
    from nose.plugins.attrib import attr
    from nose.plugins.skip import SkipTest
    import sys
    
    #TODO:jfjfjf
    log = logging.getLogger(__name__)
    
    
    
    def test_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) 
    def test_lean_2():
        u'''测试失败'''
        try:
            print "test_learn_2"
            ok_(4==3,msg="xxx")
            print sys._getframe().f_code.co_name
        except Exception:
            print sys._getframe().f_code.co_name
            
        
    @attr(mode=2) 
    def test_lean_3():
        u'''测试成功'''
        pass
        
     
    def setUp():
        #set_trace()
        global a
        print "0001 test setUp"
        #addCleanup(aa)
        
    def tearDown():
        print "0001 test teardown"
        a='resource setup'
        b='c'
        #assert a==b    
        print a
        
    
        

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

    问题分析


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

    那我们就重温下nose的Logcapture: capture logging during tests

    支持以下几个参数:

    --nologcapture
    Disable logging capture plugin. Logging configuration will be left intact. [NOSE_NOLOGCAPTURE]
    不抓log
    
    --logging-format=FORMAT
    Specify custom format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGFORMAT]
    自定义log格式
    
    --logging-datefmt=FORMAT
    Specify custom date/time format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGDATEFMT]
    log时间格式
    
    --logging-filter=FILTER
    Specify which statements to filter in/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"

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

    在windows下,将脚本写成bat时,%%会识别成变量,这个要注意。没法规避

    结果如下

    --logging-filter

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

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

    只过滤root的日志

    使用文件来定义参数


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

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

    [nosetests]
    verbosity=2
    logging-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,想同时看日志或结果报告中带日志只能二选一了。

    -----------------------------------分隔线---------------------------------------------

    ------------------------------------update 2.9----------------------------------------

    上次说到鱼和熊掌不能兼得,但最近发现上github发现一个插件,即可以在控制台输出日志,也可以将日志显示在caplog中,好用,名字就是nose-printlog

    安装:

    pip install nose-printlog

    使用

    只需要在后面加--with-printlog即可,

    E:workspace
    osetest_lear	est_case>nosetests -v -s test_case_0001.py --with-printlog

    如果想要输出格式之类的,见上面,如果想输出至文件,可以加参数--debug-log=xxx,默认是标准输出至屏幕

    ------------------------------------update 2.28----------------------------------------

    noselog也可以使用

    pip install noselog

    --with-noselog 还可以--noselog-file到指定文件,可多个使用

  • 相关阅读:
    理解Python中的元类(metaclass)
    The selected directory is not a valid home for Go SDK
    FlinkSQL 之乱序问题
    Prometheus的一些基础知识
    redis+lua实现脚本一键查询
    mysql刷题笔记
    SpringBoot利用AbstractRoutingDataSource 源码分析
    K8S那些事
    框架源码解析系列
    SpringBoot集成RocketMQ
  • 原文地址:https://www.cnblogs.com/landhu/p/7905257.html
Copyright © 2020-2023  润新知