Testify 是Python的一款测试框架,可以替代unittestnose

特性

  • 类级别的配置,同一类的测试方法仅需运行一次配置。
  • 基于装饰器的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

项目主页

更多内容请访问Testify的GitHub页面。