【问题标题】:Plone unit tests do not load dependency package profile克隆单元测试不加载依赖包配置文件
【发布时间】:2013-09-06 02:35:44
【问题描述】:

我已经为 Plone 4.2 开发了一个插件,我正在编写测试。当我执行时

# bin/test -s my.stream

我收到以下错误:KeyError: u'profile-my.common:default' 为什么会出现这个错误?

这里有一些可能有用的信息。

回溯:

Traceback (most recent call last):
  File "/blah/user/blah/00-buildout/eggs/zope.testing-3.9.7-py2.6.egg/zope/testing    /testrunner/runner.py", line 366, in run_layer
setup_layer(options, layer, setup_layers)
  File "/blah/user/blah/00-buildout/eggs/zope.testing-3.9.7-py2.6.egg/zope/testing/testrunner/runner.py", line 628, in setup_layer
setup_layer(options, base, setup_layers)
  File "/blah/user/blah/00-buildout/eggs/zope.testing-3.9.7-py2.6.egg/zope/testing/testrunner/runner.py", line 633, in setup_layer
layer.setUp()
  File "/blah/user/blah/00-buildout/eggs/plone.app.testing-4.2-py2.6.egg/plone/app/testing/helpers.py", line 343, in setUp
self.setUpPloneSite(portal)
  File "/blah/user/blah/venv26/buildout/src/my.stream/my/stream/testing.py", line 20,   in setUpPloneSite
applyProfile(portal, 'my.stream:default')
  File "/blah/user/blah/00-buildout/eggs/plone.app.testing-4.2-py2.6.egg/plone/app/testing/helpers.py", line 113, in applyProfile
setupTool.runAllImportStepsFromProfile(profileId)
  File "/blah/user/blah/00-buildout/eggs/Products.GenericSetup-1.7.1-py2.6.egg/Products/GenericSetup/tool.py", line 353, in runAllImportStepsFromProfile
ignore_dependencies=ignore_dependencies)
   - __traceback_info__: profile-my.stream:default
  File "/blah/user/blah/00-buildout/eggs/Products.GenericSetup-1.7.1-py2.6.egg/Products/GenericSetup/tool.py", line 1095, in _runImportStepsFromContext
chain = self.getProfileDependencyChain( profile_id )
  File "/blah/user/blah/00-buildout/eggs/Products.GenericSetup-1.7.1-py2.6.egg/Products/GenericSetup/tool.py", line 1078, in getProfileDependencyChain
chain.extend(self.getProfileDependencyChain( dependency, seen ))
  File "/blah/user/blah/00-buildout/eggs/Products.GenericSetup-1.7.1-py2.6.egg/Products/GenericSetup/tool.py", line 1078, in getProfileDependencyChain
chain.extend(self.getProfileDependencyChain( dependency, seen ))
  File "/blah/user/blah/00-buildout/eggs/Products.GenericSetup-1.7.1-py2.6.egg/Products/GenericSetup/tool.py", line 1076, in getProfileDependencyChain
dependencies = self.getDependenciesForProfile( profile_id )
  File "/blah/user/blah/00-buildout/eggs/Products.GenericSetup-1.7.1-py2.6.egg/Products/GenericSetup/tool.py", line 858, in getDependenciesForProfile
    raise KeyError, profile_id
KeyError: u'profile-my.common:default'

my.stream 取决于 my.common。两者都是 /blah/user/blah/buildout/src 中的附加组件。它们由 mr.developer 管理。

我的构建包括所有开发鸡蛋:

[buildout]
...
eggs =
  my.common
  my.stream
...

[test]
recipe = zc.recipe.testrunner
defaults = ['--auto-color', '--auto-progress']
eggs =
  ${instance:eggs}

我的 testing.py:

from plone.app.testing import PloneSandboxLayer
from plone.app.testing import applyProfile
from plone.app.testing import PLONE_FIXTURE
from plone.app.testing import IntegrationTesting

from zope.configuration import xmlconfig

class MyStreamContent(PloneSandboxLayer):

    defaultBases = (PLONE_FIXTURE,)

    def setUpZope(self, app, configurationContext):
        # Load ZCML
        import my.stream
        xmlconfig.file('configure.zcml',
                       my.stream,
                       context=configurationContext)

    def setUpPloneSite(self, portal):
        applyProfile(portal, 'my.stream:default')

MY__FIXTURE = MyStreamContent()
MY__INTEGRATION_TESTING = IntegrationTesting(
        bases=(MY__FIXTURE,),
        name='MyStreamContent:Integration')

我错过了什么?

提前致谢。

【问题讨论】:

    标签: plone


    【解决方案1】:

    您需要进行单元测试以加载依赖包 ZCML。它们不会自动加载(与 Plone 启动时不同),因为加载所有不需要的包会大大减慢运行测试的速度。

    有几种方法可以解决这个问题,但这很可能会奏效:

    def setUpZope(self, app, configurationContext):
        # Load ZCML
        import my.common
        self.loadZCML(package=my.common)
    
        # my.setup goes here
    

    此外,包应该分层,这样如果my.common 提供测试,那么您的my.stream 测试层将依赖于my.common 层并记住调用它们的super() 方法,以便类层次结构负责初始化依赖包。

    【讨论】:

    • 如果你的包的configure.zcml中包含了依赖的ZCML,你可以只包含你的包的ZCML。即使使用 在 setup.py 中包含 install_requires 的所有依赖项的 ZCML。但是仅通过 z3c.autoinclude 配置的包不会在测试中自动配置。
    • 非常感谢 Mikko 和 David!
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2016-10-29
    相关资源
    最近更新 更多