首页 > 编程 > Python > 正文

python nose测试框架全面介绍三

2019-11-11 01:54:28
字体:
来源:转载
供稿:网友

三、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_2test_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 test_lean_2    ok_(4==3,msg="xxx")AssertionError: xxx-------------------- >> begin captured stdout << ---------------------test_learn_2--------------------- >> end captured stdout << --------------------------------------------------------------------------------------------Ran 1 test in 0.045sFAILED (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 test_learn_1    eq_(5, 6, msg="Wrong")AssertionError: Wrong复制代码

nose.tools.assert_in(member, container, msg=None)

代码如下:

from nose.tools import assert_indef 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 test_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_infrom nose.tools import set_tracedef 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) nAssertionError: 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) nAssertionError: Assertio...failed',)> c:/python27/lib/site-packages/nose/case.py(197)runTest()-> self.test(*self.arg)(Pdb) cFAIL0002 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 test_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 timedimport 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 newfunc    raise TimeExpired("Time limit (%s) exceeded" % limit)TimeExpired: Time limit (1) exceeded----------------------------------------------------------------------Ran 1 test in 2.006sFAILED (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) 
eq_   
istest 
make_decorator 
nontrivial 
nontrivial_all 
nottest 
ok_ 
raises 
set_trace 
timed 
trivial 
trivial_all 
with_setup 

下节将介绍nose的内置插件的使用


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表