如何在 Plone 4 中开发的产品中运行测试?

发布于 2024-10-31 09:38:41 字数 920 浏览 0 评论 0原文

我正在为 Plone 4 开发一个产品,位于安装的 zeocluster/src/... 目录中,并且我有一个自动化测试。不幸的是,当我运行“bin/client1 shell”,然后运行 ​​(Plone 的 Python 路径)/bin/python setup.py test 时,它失败了。错误是

File "buildout-cache/eggs/Products.PloneTestCase-0.9.12-py2.6.egg/Products/PloneTestCase/PloneTestCase.py", line 109, in getPortal
    return getattr(self.app, portal_name)
AttributeError: plone

在 Plone 4 中运行自动化测试的正确方法是什么?

在 setup.py 中,

...
test_suite = "nose.collector"
...

失败的测试:

import unittest

from Products.PloneTestCase import PloneTestCase as ptc

ptc.setupPloneSite()

class NullTest(ptc.PloneTestCase):        
    def testTest(self):
        pass

def test_suite():
    return unittest.TestSuite([
            unittest.makeSuite(NullTest)
        ])

if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')

I am developing a product for Plone 4, inside the zeocluster/src/... directory of an installation, and I have an automated test. Unfortunately, when I run 'bin/client1 shell' and then (path to Plone's Python)/bin/python setup.py test, it fails. The error is

File "buildout-cache/eggs/Products.PloneTestCase-0.9.12-py2.6.egg/Products/PloneTestCase/PloneTestCase.py", line 109, in getPortal
    return getattr(self.app, portal_name)
AttributeError: plone

What is the correct way to run automated tests in Plone 4?

In setup.py,

...
test_suite = "nose.collector"
...

The failing test:

import unittest

from Products.PloneTestCase import PloneTestCase as ptc

ptc.setupPloneSite()

class NullTest(ptc.PloneTestCase):        
    def testTest(self):
        pass

def test_suite():
    return unittest.TestSuite([
            unittest.makeSuite(NullTest)
        ])

if __name__ == '__main__':
    unittest.main(defaultTest='test_suite')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淑女气质 2024-11-07 09:38:41

最好的方法是编辑 buildout.cfg 并添加创建“bin/test”脚本的部分。像这样的事情:

[test]
recipe = zc.recipe.testrunner
# Note that only tests for packages that are explicitly named (instead
# of 'implicitly' added to the instance as dependency) can be found.
eggs =
# Use the name of the plone.recipe.zope2instance part here, might be zeoclient instead: 
    ${instance:eggs}
defaults = ['--exit-with-status', '--auto-color', '--auto-progress']

不要忘记将“test”添加到 buildout.cfg 的主“buildout”部分中的“parts”。运行 bin/buildout,您现在应该有一个 bin/test 脚本。有关更多选项和说明,请参阅本教程的 PyPI 页面

现在运行“bin/test”应该对实例部分中明确命名的所有鸡蛋运行所有测试。这可能会运行太多测试。使用“bin/test -s your.package”仅运行 your.package 的测试,前提是 your.package 是实例中 Egg 的一部分。

请注意,最好添加一个您确定会失败的测试,例如“self.assertEqual(True, False)”,而不是现在测试中的“通过”。然后更容易看到您的测试确实已经运行并且它按预期失败。

当我有一个简单的构建来测试我正在开发的一个特定包时,我通常会扩展 plonetest 构建中的配置之一,例如 这个用于 Plone 4;你可以看看它以获取灵感。

Best is to edit your buildout.cfg and add a part that creates a 'bin/test' script. Something like this:

[test]
recipe = zc.recipe.testrunner
# Note that only tests for packages that are explicitly named (instead
# of 'implicitly' added to the instance as dependency) can be found.
eggs =
# Use the name of the plone.recipe.zope2instance part here, might be zeoclient instead: 
    ${instance:eggs}
defaults = ['--exit-with-status', '--auto-color', '--auto-progress']

Do not forget to add 'test' to the 'parts' in the main 'buildout' section of your buildout.cfg. Run bin/buildout and you should now have a bin/test script. See the PyPI page of this recipe for more options and explanation.

Now running 'bin/test' should run all tests for all eggs explicitly named in the instance part. This may run far too many tests. Use 'bin/test -s your.package' to run only the tests for your.package, provided your.package is part of the eggs in the instance.

Note that instead of the 'pass' that you now have in the test, it is better to add a test that you know for certain will fail, like 'self.assertEqual(True, False)'. Then it is easier to see that your test indeed has been run and that it fails as expected.

When I have a simple buildout for testing one specific package that I am developing, I usually extend one of the configs in the plonetest buildout, like this one for Plone 4; you can have a look at that for inspiration.

无人问我粥可暖 2024-11-07 09:38:41

您需要使用 zope.testrunner 和 zope.testing 来运行测试。 Plone 测试无法通过鼻子运行,并且我们不支持 setuptools 发明的 setup.py 的“test_suite”参数。

其他答案解释了如何设置测试运行脚本。

You need to use zope.testrunner and zope.testing to run your tests. Plone tests cannot be run via nose and we don't support the 'test_suite' argument to setup.py as invented by setuptools.

The other answers explain how to get a test runner script set up.

暮色兮凉城 2024-11-07 09:38:41

ptc.setupPloneSite() 注册一个延迟函数,该函数将在设置 zope.testrunner 层时实际运行。我猜您没有使用 zope.testrunner,因此未设置该层,因此永远不会创建 Plone 站点,因此当它随后尝试获取门户对象时会出现 AttributeError 。

ptc.setupPloneSite() registers a deferred function that will be actually run when the zope.testrunner layer is set up. I'm guessing you're not using zope.testrunner and thus the layer isn't being setup so the Plone site is never created, hence the AttributeError when it tries subsequently to get the portal object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文