开发者

How do I run tests in a product being developed in Plone 4?

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.set开发者_如何学PythonupPloneSite()

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


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.


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() 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜