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
