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

Python 如何在Plone 4中开发的产品中运行测试?,python,plone,nose,Python,Plone,Nose,我正在为Plone 4开发一个产品,在安装的zeocluster/src/…目录中,我有一个自动测试。不幸的是,当我运行“bin/client1shell”然后运行(Plone的Python路径)/bin/Python setup.py测试时,它失败了。错误是 File "buildout-cache/eggs/Products.PloneTestCase-0.9.12-py2.6.egg/Products/PloneTestCase/PloneTestCase.py", line 109, i

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

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')

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

最好是编辑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']
不要忘记在buildout.cfg的主“buildout”部分的“parts”中添加“test”。运行bin/buildout,您现在应该有一个bin/test脚本。有关更多选项和说明,请参阅此配方的

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

请注意,与其在测试中使用“pass”,不如添加一个您确定会失败的测试,比如“self.assertEqual(True,False)”。这样就更容易看到您的测试确实已经运行了,并且按照预期失败了


当我有一个简单的构建来测试我正在开发的一个特定包时,我通常会扩展plonetest构建中的一个配置,比如;您可以从中获得灵感。

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


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

如果使用统一安装程序安装,则通过运行“bin/buildout-c develope.cfg”可以获得bin/test和其他面向开发的工具。编辑develop.cfg以自定义选项。