三、nose的測試工具集
nose.tools模塊提供了一系列的小工具,包括測試執行時間、異常輸出及unittest框架中所有的assert功能。
為了使寫用例更加容易,nose.tools提供了部分便利的功能函數,下面寫幾個常用的,如下:
nose.tools.ok_(expr, msg=None)
標准的assert,例子如下:
from nose.tools import eq_ def test_lean_2(): print "test_learn_2" ok_(4==3,msg="Error")
運行結果如下:
E:\workspace\nosetest_lear\test_case>nosetests -v test_case_0001:test_lean_2 test_case_0001.test_lean_2 ... FAIL ====================================================================== FAIL: test_case_0001.test_lean_2 ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest self.test(*self.arg) File "E:\workspace\nosetest_lear\test_case\test_case_0001.py", line 26, in tes t_lean_2 ok_(4==3,msg="xxx") AssertionError: xxx -------------------- >> begin captured stdout << --------------------- test_learn_2 --------------------- >> end captured stdout << ---------------------- ---------------------------------------------------------------------- Ran 1 test in 0.045s FAILED (failures=1) E:\workspace\nosetest_lear\test_case>
nose.tools.eq_(a, b, msg=None)
將參數a與b快速對比
from nose.tools import eq_ def test_learn_1(): eq_(5, 6, msg="Wrong")
運行結果如下:
====================================================================== FAIL: test_case_0001.test_learn_1 ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest self.test(*self.arg) File "E:\workspace\nosetest_lear\test_case\test_case_0001.py", line 20, in tes t_learn_1 eq_(5, 6, msg="Wrong") AssertionError: Wrong
nose.tools.assert_in(member, container, msg=None)
代碼如下:
from nose.tools import assert_in def test_lean_5(): assert_in("aaa",'bbb',msg="test in failed")
運行結果如下:
====================================================================== FAIL: test_case_0002.test_lean_5 ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest self.test(*self.arg) File "E:\workspace\nosetest_lear\test_case\test_case_0002.py", line 24, in tes t_lean_5 assert_in("aaa",'bbb',msg="test in failed") AssertionError: test in failed ----------------------------------------------------------------------
其它的assert這里就不說了,nose.tools包含很多內容,可以直接查詢。
下面再介紹幾個有用的nose.tools工具
nose.tools.set_trace()
-------------單步調試工具,在多個模塊,大程序時這個功能好用。內部使用的是pdb.set_trace
代碼如下:
from nose.tools import assert_in from nose.tools import set_trace def test_lean_5(): set_trace() assert_in("aaa",'bbb',msg="test in failed")
結果如下:
test_case_0002.test_lean_5 ... > e:\workspace\nosetest_lear\test_case\test_case_ 0002.py(26)test_lean_5() -> assert_in("aaa",'bbb',msg="test in failed") (Pdb) n AssertionError: Assertio...failed',) > e:\workspace\nosetest_lear\test_case\test_case_0002.py(26)test_lean_5() -> assert_in("aaa",'bbb',msg="test in failed") (Pdb) n --Return-- > e:\workspace\nosetest_lear\test_case\test_case_0002.py(26)test_lean_5()->None -> assert_in("aaa",'bbb',msg="test in failed") (Pdb) n AssertionError: Assertio...failed',) > c:\python27\lib\site-packages\nose\case.py(197)runTest() -> self.test(*self.arg) (Pdb) c FAIL 0002 test teardown ====================================================================== FAIL: test_case_0002.test_lean_5 ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest self.test(*self.arg) File "E:\workspace\nosetest_lear\test_case\test_case_0002.py", line 26, in tes t_lean_5 assert_in("aaa",'bbb',msg="test in failed") AssertionError: test in failed
具體的pdb調試手段可參見http://www.cnblogs.com/chencheng/archive/2013/07/07/3161778.html
nose.tools.timed(limit)
測試必須在設定的時間內(以秒為單位)完成 ,否則測試失敗;代碼如下:
from nose.tools import timed import time @timed(1) def test_lean_5(): time.sleep(2) pass
測試結果如下:
test_case_0002.test_lean_5 ... FAIL ====================================================================== FAIL: test_case_0002.test_lean_5 ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest self.test(*self.arg) File "c:\python27\lib\site-packages\nose\tools\nontrivial.py", line 100, in ne wfunc raise TimeExpired("Time limit (%s) exceeded" % limit) TimeExpired: Time limit (1) exceeded ---------------------------------------------------------------------- Ran 1 test in 2.006s FAILED (failures=1)
nose.tools中還有很多assert的函數工具,不一一介紹了,列出表如下,需要的時候可以使用。
assert_equal(first, second, msg=None) | 兩個對像對比,使用"=="操作對比 |
assert_not_equal(first, second, msg=None) | 不相等 |
assert_true(expr, msg=None) | 判定表達式是否為真 |
assert_false(expr, msg=None) | 判定表達式是否為假 |
assert_is(expr1, expr2, msg=None) | expr1 is expr2 |
assert_is_not(expr1, expr2, msg=None) | |
assert_is_none(obj, msg=None) | 為空 |
assert_is_not_none(obj, msg=None) | 不為空 |
assert_in(member, container, msg=None) | merber in container判斷 |
assert_not_in(member, container, msg=None) | 不包含判斷 |
assert_is_instance(obj, cls, msg=None) | |
assert_not_is_instance(obj, cls, msg=None) | |
assert_raises_regexp(expected_exception, expected_regexp, |
|
assert_almost_equal(first, second, places=None, msg=None, delta=None) | |
assert_greater(a, b, msg=None) | |
assert_greater_equal(a, b, msg=None) | |
assert_less(a, b, msg=None) | |
assert_less_equal(a, b, msg=None) | |
assert_regexp_matches(text, expected_regexp, msg=None) | |
assert_not_regexp_matches(text, unexpected_regexp, msg=None) | |
assert_items_equal(expected_seq, actual_seq, msg=None) | |
assert_dict_contains_subset(expected, actual, msg=None) | |
assert_multi_line_equal(first, second, msg=None) | |
assert_sequence_equal(seq1, seq2, msg=None, seq_type=None) | |
assert_list_equal(list1, list2, msg=None) | |
assert_tuple_equal(tuple1, tuple2, msg=None) | |
assert_set_equal(set1, set2, msg=None) | |
assert_dict_equal(d1, d2, msg=None) |
下節將介紹nose的內置插件的使用