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\nosetest_lear\test_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 nose测试框架全面介绍七--日志相关的更多相关文章
- python nose测试框架全面介绍十---用例的跳过
又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...
- python nose测试框架全面介绍六--框架函数别名
之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...
- python nose测试框架全面介绍五--attr介绍
之前写了一系列nose框架的,这篇介绍下attr tag 在nose框架中attr用来标识用例,使得在运行时可以通过标识来执行用例,之前在nose测试框架全面介绍四中有说明,但没有说明清楚,这里再总结 ...
- python nose测试框架全面介绍一
一.简介 nose 是python自带框架unttest的扩展,使测试更简单高效:nose是一个开源的项目,可以在官网上下载源码 1.快速安装 有以下几中安装方式: easy_install ...
- python nose测试框架全面介绍四
四.内部插件介绍 1.Attrib 标记,用于筛选用例 在很多时候,用例可以分不同的等级来运行,在nose中很增加了这个功能,使用attrib将用例进行划分 有两种方式: ef test_big_do ...
- python nose测试框架全面介绍三
三.nose的测试工具集 nose.tools模块提供了一系列的小工具,包括测试执行时间.异常输出及unittest框架中所有的assert功能. 为了使写用例更加容易,nose.tools提供了部分 ...
- python nose测试框架全面介绍二
二.基本使用 nosetest脚本的使用(在安装完nose之后) nosetests [options] [(optional) test files or directories] 我们可以使用配置 ...
- python nose测试框架全面介绍十二 ----用例执行顺序打乱
在实际执行自动化测试时,发现我们的用例在使用同一个资源的操作时,用例的执行顺序对测试结果有影响,在手工测试时是完全没法覆盖的. 但每一次都是按用例名字来执行,怎么打乱来执行的. 在网上看到一个有意思的 ...
- python nose测试框架全面介绍十一---用例的发现
nose是怎么发现用例的??网上一大把说函数以test开头的都会自动发现,真的是这样吗???还是自己来试验下吧 首先,我们还是来看看官方文档怎么说的吧: If it looks like a test ...
随机推荐
- Unable to resolve target 'android-9'
右键项目文件--->properties--->android 选择对应版本 保存 如还不生效 打开项目文件project.properties ,修改 target=android-1 ...
- bashrc profile的区别
bashrc与profile的区别 bashrc和profile的差异在于:1. bashrc是在系统启动后就会自动运行.2. profile是在用户登录后才会运行.3. 进行设置后,可运用sourc ...
- mysql 错误代码:1118解决方法
错误描述: 错误代码: 1118Row size too large. The maximum row size for the used table type, not counting BLOBs ...
- C#爬网页时“远程服务器返回错误: (403) 已禁止”解决方法
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri); //这个一定要加上,在某些网站没有会发生"远程服务器返回错误: ...
- ASCII码与16进制的互相转换(表)
所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...
- xcode 5.1打包iOS 7.1应用问题笔记
XCODE 5.1默认情况下是要求应用都通过64位编译.但是往往有些第三方的类库还是32位.还木有更新64位类库.使得项目编译出错. 解决办法: BuildSetting 的Valid Archite ...
- hdu 2348 Turn the corner(三分&&几何)(中等)
Turn the corner Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- linux_开发软件安装=命令步骤
1.Linux 操作系统软件安装以及redis 学习 JDK ----- Java开发运行环境 Tomcat -- WEB程序的服务器 MySQL --- 持久化存储数据 Re ...
- 8 -- 深入使用Spring -- 1...3 容器后处理器
8.1.3 容器后处理器(BeanFactoryPostProcessor) 容器后处理器负责处理容器本身. 容器后处理器必须实现BeanFacotryPostProcessor接口.实现该接口必须实 ...
- ios开发之 -- NSData 和 NSString , UIImage 等之间的互转
//NSData转换为UIImage NSData *imageData = [NSData dataWithContentsOfFile: imagePath]; UIImage *image = ...