python除了unittest,还有一款更快捷的nose,nose可以说是对unittest的一种简化吧
但是他不需要unittest那种必须有固有的格式,他只需要文件,类名,方法名等含有test就可以
unittest是需要手动来写discover函数来遍历用例的
-
Name my test modules/files starting with ‘test_’.
-
Name my test functions starting with ‘test_’.
-
Name my test classes starting with ‘Test’.
-
Name my test methods starting with ‘test_’.
-
Make sure all packages with test code have an ‘init.py’ file.
官网地址:http://pythontesting.net/framework/nose/nose-introduction/
里面介绍的很详细,一下总结几个常用的点:
安装:
easy_install nose
运行:
nosetests [文件]
nosetests [目录]
如果不加目录,默认执行当前目录下的所有符合nose条件的用例,
如果加目录,则运行指定目录里面符合nose条件的用例
-v 把运行中的具体过程输出:nosetests -v
-s 把测试用例中的print输出打印到控制台,这个调试的时候还是挺方便的
nose的执行规则:和unittest一样,会优先执行setUp,最后执行tesrDown,和函数的位置没关系
nose里面常用的工具有很多函数,比如类似unittest的assertEqual
使用方法
from nose import tools
然后看下里面有这么多的方法
assert_almost_equal
assert_almost_equals
assert_dict_contains_subset
assert_dict_equal
assert_equal
assert_equals
assert_false
assert_greater
assert_greater_equal
assert_in
assert_is
assert_is_instance
assert_is_none
assert_is_not
assert_is_not_none
assert_items_equal
assert_less
assert_less_equal
assert_list_equal
assert_multi_line_equal
assert_not_almost_equal
assert_not_almost_equals
assert_not_equal
assert_not_equals
assert_not_in
assert_not_is_instance
assert_not_regexp_matches
assert_raises
assert_raises_regexp
assert_regexp_matches
assert_sequence_equal
assert_set_equal
assert_true
assert_tuple_equal
eq_
istest
make_decorator
nontrivial
nontrivial_all
nottest
ok_
raises
set_trace
timed
trivial
trivial_all
with_setup