Nose 測試 - 工具



nose.tools 模組提供了一些您可能會發現有用的測試輔助工具,包括用於限制測試執行時間和測試異常的裝飾器,以及 unittest.TestCase 中找到的所有相同的 assertX 方法。

  • nose.tools.ok_(expr, msg = None) − assert 的簡寫。

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

  • nose.tools.make_decorator(func) − 包裝測試裝飾器,以便正確複製被裝飾函式的元資料,包括 nose 的附加內容(即,設定和拆卸)。

  • nose.tools.raises(*exceptions) − 測試必須引發一個預期異常才能透過。

  • nose.tools.timed(limit) − 測試必須在指定的時間限制內完成才能透過

  • nose.tools.istest(func) − 裝飾器,用於將函式或方法標記為測試

  • nose.tools.nottest(func) − 裝飾器,用於將函式或方法標記為非測試

引數化測試

Python 的測試框架 unittest 沒有簡單的方法來執行引數化測試用例。換句話說,您無法輕鬆地從外部向 unittest.TestCase 傳遞引數。

但是,pytest 模組以幾種良好整合的形式移植了測試引數化 -

  • pytest.fixture() 允許您在 fixture 函式級別定義引數化。

  • @pytest.mark.parametrize 允許在函式或類級別定義引數化。它為特定測試函式或類提供多個引數/fixture 集。

  • pytest_generate_tests 支援實現您自己的自定義動態引數化方案或擴充套件。

第三方模組 'nose-parameterized' 允許使用任何 Python 測試框架進行引數化測試。可以從此連結下載 - https://github.com/wolever/nose-parameterized

廣告

© . All rights reserved.