支持测试代码
nti.testing的Python项目详细描述
无损检测
支持编写测试,特别是在zope3/ztk环境中, 使用zope.testing(nose2也可以工作,但不推荐使用)。
完整的文档位于 https://ntitesting.readthedocs.io/
安装
可以使用pip从git存储库安装nti.testing 或者来自pypi:
pip install nti.testing
皮哈姆克雷斯特
nti.testing提供一组 pyhamcrest 匹配器。两者都有 用户使用的通用匹配器和匹配器 zope.interface 和 zope.schema
匹配器可以从 nti.testing.matchers 模块导入。
基本匹配器
为真 且 为假 检查所提供的 对象(我们使用文本进行解释,但是 显然,当匹配的对象 是一个变量,通常是更复杂的类型):
>>> from hamcrest import assert_that, is_ >>> from nti.testing.matchers import is_true, is_false >>> assert_that("Hi", is_true()) >>> assert_that(0, is_false())
接口匹配器
接下来我们将介绍支持基本使用zope.interface的匹配器。
我们可以检查对象是否提供接口以及工厂 实现它:
>>> from zope.interface import Interface, Attribute, implementer >>> class IThing1(Interface): ... pass >>> class IThing2(Interface): ... pass >>> class IThings(IThing1, IThing2): ... got_that_thing_i_sent_you = Attribute("Did you get that thing?") >>> @implementer(IThings) ... class Thing(object): ... pass >>> from nti.testing.matchers import provides, implements >>> assert_that(Thing(), provides(IThings)) >>> assert_that(Thing, implements(IThings))
细心的读者会注意到 things 定义了 我们的实现实际上没有提供的属性。这是 下一次更严格的检查是在哪里。 可验证的 确定所有属性和方法的接口机制 由接口指定的显示如下所述:
>>> from nti.testing.matchers import verifiably_provides >>> assert_that(Thing(), verifiably_provides(IThing2, IThing1)) >>> assert_that(Thing(), verifiably_provides(IThings)) Traceback (most recent call last): ... AssertionError:... Expected: object verifiably providing IThings but: <class 'Thing'> failed to provide attribute "got_that_thing_i_sent_you" from IThings <BLANKLINE>
zope.interface 只能检查属性或 方法已存在。对 属性的值,我们可以升级到 zope.schema 和 有效地提供匹配器
>>> from zope.schema import Bool >>> class IBoolThings(IThing1, IThing2): ... got_that_thing_i_sent_you = Bool() >>> @implementer(IBoolThings) ... class BoolThing(object): ... pass
有效地提供 是可验证地提供的 的超集:
>>> from nti.testing.matchers import validly_provides >>> assert_that(BoolThing(), validly_provides(IThing1, IThing2)) >>> assert_that(BoolThing(), validly_provides(IBoolThings)) Traceback (most recent call last): ... AssertionError:... Expected: (object verifiably providing IBoolThings and object validly providing <InterfaceClass ....IBoolThings>) but: object verifiably providing IBoolThings <class 'BoolThing'> failed to provide attribute "got_that_thing_i_sent_you" from IBoolThings <BLANKLINE>
对于更细粒度的控制,我们可以将数据与架构字段进行比较:
>>> from nti.testing.matchers import validated_by, not_validated_by >>> field = IBoolThings.get('got_that_thing_i_sent_you') >>> assert_that(True, is_(validated_by(field))) >>> assert_that(None, is_(not_validated_by(field)))
父子关系
matcher使用从获取到 检查父/子关系:
>>> from nti.testing.matchers import aq_inContextOf >>> class Parent(object): ... pass >>> class Child(object): ... __parent__ = None >>> parent = Parent() >>> child = Child() >>> child.__parent__ = parent >>> assert_that(child, aq_inContextOf(parent))
测试夹具
可在 nti.testing.base 和 测试层。 基础 包装包括全肉 直接使用的out基类,而 层 包包括 可用于构建自己的测试层的混音。
base 包区分了"normal"和"shared" 固定装置。普通夹具是用于单个测试的夹具 案例。它们通过 设置建立并通过 拆卸
相比之下,共享设备预计将持续一段时间 类中的所有测试或层中的所有测试。这些是 最适合在夹具制造成本高的情况下使用。任何 从 基扩展。abstractSharedTestBase 创建共享设备。 通过元类的魔力,这样的子类也可以被分配 作为要用作测试层的另一个类的 层 属性 可以跨多个类共享。
最重要的基础是 base.configuringtestbase 和 base.sharedconfiguringtestbase 。这两个都是 从现有包或完整文件配置zcml 路径。要使用它们,请将它们子类化并定义类属性 设置软件包 和(如有必要)功能 :
>>> from nti.testing.base import ConfiguringTestBase >>> import zope.security >>> class MyConfiguringTest(ConfiguringTestBase): ... set_up_packages = ( ... 'zope.component', # the default configuration by name ... # a named file in a named package ... ('ftesting.zcml', 'zope.traversing.tests'), ... # an imported module ... zope.security, ... # Our own package; in a test, this will mean the parent ... # package ... ".")
然后我们将继续编写我们的测试方法。我们的包裹 将在每个测试方法周围设置和删除指定的。在 此外,zope.testing的清除函数也将运行 每种测试方法。
时间
拥有一个保证以正增长方式移动的时钟 每次调用 time.time 都很有用。 无损检测时间 提供一个decorator来实现这一点,确保值始终是 至少现在的时间总是在增加。(不是线 它可以应用于函数或方法,并且可以选择 a 粒度 参数:
pip install nti.testing0