码迷,mamicode.com
首页 > 编程语言 > 详细

python nose测试框架全面介绍三

时间:2016-07-19 13:30:52      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

三、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中还有设置测试用例是否为要测及不测选项,这里就不介绍了。

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

python nose测试框架全面介绍三

标签:

原文地址:http://www.cnblogs.com/landhu/p/5664613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!