python自动化框架nose

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

simple_example.test_um_nose.test_numbers_3_4 ... ok
simple_example.test_um_nose.test_strings_a_3 ... ok
 
----------------------------------------------------------------------
Ran 2 tests in 0.000s
 
OK

-s 把测试用例中的print输出打印到控制台,这个调试的时候还是挺方便的

C:\Users\Administrator>nosetests -s "d:\\00"

thisisprint1
.thisisprint2

------------------------------------------------
Ran 2 tests in 0.044s

OK

nose的执行规则:

和unittest一样,会优先执行setUp,最后执行tearDown,和函数的位置没关系

 

def tearDown():
        print "teardown"
def test1():
        print 1
        #assert(1==2)
def test2():
        print 2
def setUp():
        print "this setup"

C:\Users\Administrator>nosetests -s -v  "d:\\00"
this setup
testnose.test1 ... 1
ok
testnose.test2 ... 2
ok
teardown
=
------------------------------------------------
Ran 2tests in 0.010s

OK

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

 

posted @ 2015-11-17 10:28  米 立  阅读(6667)  评论(0编辑  收藏  举报