天天看点

Python测试框架nose之设计case(2)

官方链接:

test tools

1.nose 框架同unittest一样支持包,模块,类级别的setUp和tearDown

所有在test模块下的函数,凡是符合正则testMatch条件((?:^|[\\b_\\.-])[Tt]est都封装入FunctionTestCase。

test functions支持函数级别的setUp

例:

def setup_func():
    "set up test fixtures"

def teardown_func():
    "tear down test fixtures"

@with_setup(setup_func, teardown_func)
def test():
    "test ..."
           

2.nose.tools的使用

nose.tools. ok_ ( expr, msg=None )
Shorthand for assert. Saves 3 whole characters!
nose.tools. eq_ ( a, b, msg=None )
Shorthand for ‘assert a == b, “%r != %r” % (a, b)
nose.tools. make_decorator ( func )
Wraps a test decorator so as to properly replicate metadataof the decorated function, including nose’s additional stuff(namely, setup and teardown).
nose.tools. raises ( *exceptions )
Test must raise one of expected exceptions to pass.
@raises(TypeError, ValueError)
def test_raises_type_error():
    raise TypeError("This test passes")

@raises(Exception)
def test_that_fails_by_passing():
    pass
           
nose.tools. set_trace ( )
Call pdb.set_trace in the calling frame, first restoringsys.stdout to the real output stream. Note that sys.stdout is NOTreset to whatever it was before the call once pdb is done!
nose.tools. timed ( limit )

Test must finish within specified time limit to pass.

Example use:

@timed(.1)
def test_that_fails():
    time.sleep(.2)
      
nose.tools. with_setup ( setup=None, teardown=None )
Decorator to add setup and/or teardown methods to a test function:
@with_setup(setup, teardown)
def test_something():
    " ... "
      
Note that with_setup is useful only for test functions, not for testmethods or inside of TestCase subclasses.
nose.tools. istest ( func )
Decorator to mark a function or method as a test
nose.tools. nottest ( func )
Decorator to mark a function or method as not a test