验证所有implements/classimplements声明的接口协定

affinitic.verifyinterface的Python项目详细描述


简介

如果类没有正确实现接口,那么声明接口有什么用?

当然,您应该在测试中验证这一点,但如果您不想编写测试来检查您的所有代码是否真的实现了承诺的接口,请使用此包。

这是一个简单的补丁,一旦声明实现 接口并打印BrokenImplementation或BrokenMethodImplementation作为警告(如果有)。

testrunner中的简单示例:

默认情况下,zope.interface.implementszope.interface.classImplements存在于所有软件包中:

>>> write('buildout.cfg',
... """
... [buildout]
... parts =
...     test
...
... [test]
... recipe = zc.recipe.testrunner
... eggs = affinitic.verifyinterface
...        zope.exceptions
... defaults = ['-m', 'module']
... """)

>>> print system(buildout)
Installing test.
...

>>> from os.path import join
>>> print system(join('bin', 'test')),
<class 'affinitic.verifyinterface.tests.test_module1.Foo'> failed implementing <InterfaceClass affinitic.verifyinterface.tests.test_module1.IFoo>: An object has failed to implement interface <InterfaceClass affinitic.verifyinterface.tests.test_module1.IFoo>
<BLANKLINE>
        The bla attribute was not provided.
<BLANKLINE>
<class 'affinitic.verifyinterface.tests.test_module2.Bar'> failed implementing <InterfaceClass affinitic.verifyinterface.tests.test_module2.IBar>: An object has failed to implement interface <InterfaceClass affinitic.verifyinterface.tests.test_module2.IBar>
<BLANKLINE>
        The bla attribute was not provided.
<BLANKLINE>
Running zope.testing.testrunner.layer.UnitTests tests:
  Set up zope.testing.testrunner.layer.UnitTests in 0.000 seconds.
  Ran 2 tests with 0 failures and 0 errors in 0.000 seconds.
Tearing down left over layers:
  Tear down zope.testing.testrunner.layer.UnitTests in 0.000 seconds.

极限验证

但是你可以把这个包限制在需要验证的地方(有时候你不在乎 依赖的包没有正确实现接口)。

这是通过添加环境变量verifyinterface来完成的,在该变量中,可以指定接受哪些包/模块(通常用n分隔)来验证接口。

下面是一个简单的例子,我只想对模块1使用的接口的错误实现发出警告:

>>> write('buildout.cfg',
... """
... [buildout]
... parts =
...     test
...
... [test]
... recipe = zc.recipe.testrunner
... eggs = affinitic.verifyinterface
...        zope.exceptions
... defaults = ['-m', 'module']
... environment = testenv
...
... [testenv]
... verifyinterface = affinitic.verifyinterface.tests.test_module1
... """)

>>> print system(buildout)
Uninstalling test.
Installing test.
...

>>> from os.path import join
>>> print system(join('bin', 'test'))
<class 'affinitic.verifyinterface.tests.test_module1.Foo'> failed implementing <InterfaceClass affinitic.verifyinterface.tests.test_module1.IFoo>: An object has failed to implement interface <InterfaceClass affinitic.verifyinterface.tests.test_module1.IFoo>
<BLANKLINE>
        The bla attribute was not provided.
<BLANKLINE>
Running zope.testing.testrunner.layer.UnitTests tests:
  Set up zope.testing.testrunner.layer.UnitTests in 0.000 seconds.
  Ran 2 tests with 0 failures and 0 errors in 0.000 seconds.
Tearing down left over layers:
  Tear down zope.testing.testrunner.layer.UnitTests in 0.000 seconds.

更改日志

0.1(2009-12-18)

  • 初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java如何在泛型类中调用我的类T的方法   java如何在XML配置中添加quartz JobListener(springframework)   java JPA查询以空值结束   密码学为什么Java DES密钥生成器生成的密钥大小不正确?   java布尔算法在输出为假时生成真输出   java缓存休眠混乱的行为   java如何让Android SDK 1.5版在我的G1手机上运行?   java无状态应用程序Spring安全性   Eclipse上的java,“首选项>常规>工作区>使用本机挂钩或轮询刷新”做了什么?   java私有静态方法从公共静态方法访问   滚动窗格JavaFX中图形上的java静态标签   如何从Oracle Java教程中调用findLargest方法(使用接口作为类型)   JavaEE:EJB远程接口中的自定义类