nose1.3.7文檔翻譯--5.Testing tools

nose.tools模塊提供了需要有用的測試幫助,包括約束測試執行時間和異常的修飾,所有的unittest.TestCase中提供的assertX方法(僅以PEP 8#function-names風格拼寫,因此是assert_equal,而不是assertEqual)。

測試工具

nose.tools提供了許多便利的函數,它們讓編寫用例變得更加簡單。你並不是必須要使用它們,nose並不是依賴這些方法。

nose.tools.ok_(expr, msg=None)
assert斷言.保存3個完整的字符!

nose.tools.eq_(a, b, msg=None)
assert ‘a== b,“ %r != %r”%(a, b)’

nose.tools.make_decorator(func)
裝飾測試例的裝飾器,以便恰當的複製被裝飾函數的元數據,包括nose的額外數據(也就是類似於setup和teardown)

nose.tools.raises(*exceptions)
測試例必須拋出期待的exceptions才被認爲是pass。
比如:

@raises(TypeError, ValueError)
def test_raises_type_error():
    raise TypeError("This test passes")

@raises(Exception)
def test_that_fails_by_passing():
    pass

如果你想要在一個測試例中測試一系列關於斷言的異常,可以使用assert_raises代替。

nose.tools.set_trace()
調用可調用frame中的pdb.set_trace(),首先恢復sys.stdout到真實輸出流。注意,在pdb完成之前,sys.stdout不會被重新設置成任何東西。

nose.tools.timed(limit)
測試必須在limit限制的時間內完成纔算是pass。
例如:

@timed(.1)
def test_that_fails():
    time.sleep(.2)

nose.tools.with_setup(setup=None, teardown=None)
爲測試函數添加setup和teardown

@with_setup(setup, teardown)
def test_something():
    " ... "

注意,with_setup函數僅僅用於一個測試函數,並不適用於TestCase子類中的測試方法。

nose.tools.istest(func)
將一個函數或方法裝飾成測試例

nose.tools.notest(func)
將一個函數或方法裝飾成不是測試例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章