python nose测试框架全面介绍三

三、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,
callable_obj=None, *args, **kwargs)

 
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的内置插件的使用

上一篇:UWP 律师信息 MVVM 2.0版本


下一篇:自己定义TextView 调用ttf格式字体