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

时间:2011-04-08 19:22:25

标签: python plone nose

我正在为安装的zeocluster/src/...目录中的Plone 4开发一个产品,我有一个自动化测试。不幸的是,当我运行'bin / client1 shell'然后运行(path to Plone's 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')

3 个答案:

答案 0 :(得分:3)

最好是编辑你的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脚本。有关更多选项和说明,请参阅本食谱的PyPI page

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

请注意,除了您现在在测试中使用的'pass'之外,最好添加一个您知道肯定会失败的测试,例如'self.assertEqual(True,False)'。然后更容易看到您的测试确实已经运行并且它按预期失败。

当我有一个简单的buildout来测试我正在开发的一个特定包时,我通常会扩展plonetest buildout中的一个配置,比如this one for Plone 4;你可以看看它的灵感。

答案 1 :(得分:3)

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

其他答案解释了如何设置测试跑步者脚本。

答案 2 :(得分:2)

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