天天看點

Testify Pythoinc的單元測試架構

Testify Pythoinc的單元測試架構

特性

  • 類級别的配置,同一類的測試方法僅需運作一次配置。
  • 基于裝飾器的fixture方法,提供惰性求值屬性和上下文管理。
  • 類似nose,可以深入包尋找測試用例。
  • 可以以子產品、類為機關運作測試,或者運作單個測試方法。
  • 支援多線程測試。
  • 彩色輸出。
  • 可擴充的插件系統。
  • 友善的測試工具,包括turtle(用于mocking)、測試覆寫、性能分析,等等。
  • 更為Pythonic的命名約定。

例子

from testify import *

class AdditionTestCase(TestCase):

   @class_setup

    def init_the_variable(self):

        self.variable = 0

   @setup

    def increment_the_variable(self):

        self.variable += 1

    def test_the_variable(self):

        assert_equal(self.variable, 1)

   @suite('disabled', reason='ticket #123, not equal to 2 places')

    def test_broken(self):

        # raises 'AssertionError: 1 !~= 1.01'

        assert_almost_equal(1, 1.01, threshold=2)

   @teardown

    def decrement_the_variable(self):

        self.variable -= 1

   @class_teardown

    def get_rid_of_the_variable(self):

        self.variable = None

if __name__ == "__main__":

    run()

相容Unittest

Testify可以直接運作

unittests

,不需改動任何代碼:

testify my_unittests/foo_test.py

如果要使用Testify的進階特性,隻需将

unittest.TestCase

改為

testify.TestCase