如何在 Plone 4 中开发的产品中运行测试?
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的方法是编辑 buildout.cfg 并添加创建“bin/test”脚本的部分。像这样的事情:
不要忘记将“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:
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.
您需要使用 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.
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.