plone / plone.app.testing

Testing tools for Plone-the-application
http://pypi.python.org/pypi/plone.app.testing
2 stars 7 forks source link

Error in "Module ZPublisher.HTTPRequest, line 1390, in __getattr__ AttributeError: SESSION - Expression:" when using self.request.SESSION in a dependency during tests #14

Closed idgserpro closed 9 years ago

idgserpro commented 9 years ago

We are doing some tests using robotsframework and plone.app.testing in collective.myproduct that has collective.mydependency as a dependency. This dependency adds some functionality to the login form. This dependency is loaded via zcml in tests and it's profile installed as well (it's in collective.myproduct metadata.xml).

In this functionality, we ask for self.request.SESSION, that works fine in Plone, but gives an error when running bin/test:

SESSION - Expression: "expression" - Filename: ... myfilename/widget.pt - Location: (line 7: col 26) - Source: ... :condition="mycondition"> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Arguments: repeat: {...} (0) template: <ImplicitAcquisitionWrapper login_form at 0xb0175d9cL> modules: <instance - at 0xb718248cL> here: <ImplicitAcquisitionWrapper plone at 0xb13dcf54L> user: <ImplicitAcquisitionWrapper - at 0xafda3fa4L> nothing: <NoneType - at 0x81ad6b4> target_language: <NoneType - at 0x81ad6b4> container: <ImplicitAcquisitionWrapper plone at 0xb13dcf54L> default: <object - at 0xb757f760L> request: <instance - at 0xaff1542cL> wrapped_repeat: <SafeMapping - at 0xafd8dc5cL> traverse_subpath: <list - at 0xaffc00ecL> loop: {...} (1) context: <ImplicitAcquisitionWrapper plone at 0xb13dcf54L> translate: <function translate at 0xaff320d4L> root: <ImplicitAcquisitionWrapper Zope at 0xafd677d4L> options: {...} (2)

Doing a little googling (thanks https://github.com/collective/collective.whathappened/commit/367159ad7a2376677ebc6fb901d47414dda3d4f3), we found out that you have to import

from Testing.ZopeTestCase.utils import setupCoreSessions

and in the end of setUpZope set

setupCoreSessions(app)

Is this the intended behavior? Isn't plone.app.testing supposed to handle SESSION in PloneSandboxLayer?

tomgross commented 9 years ago

Running setupCoreSessions(app) in the setUpZope handler of your Plone testing layer is the inteded behavior AFAIK.

idgserpro commented 9 years ago

This is really confusing to beginners. Zope2 is already a dependency in plone.app.testing, wouldn't it be nice to call setupCoreSessions in plone.app.testing layer lifecycle, for example in setUp?

tomgross commented 9 years ago

I don't know the exact reason but I guess it is because sessions was removed from Plone to be more scaleable and it would be an overhead in most testing scenarios anyways.

idgserpro notifications@github.com schrieb am Do., 25. Juni 2015 um 17:15:

This is really confusing to beginners. Zope2 is already a dependency in plone.app.testing, wouldn't it be nice to call setupCoreSessions in plone.app.testing layer lifecycle, for example in setUp?

— Reply to this email directly or view it on GitHub https://github.com/plone/plone.app.testing/issues/14#issuecomment-115289061 .

idgserpro commented 9 years ago

it would be an overhead in most testing scenarios anyways.

Well, that's true. But, IMHO, if it's a small overhead, taking it for granted instead of explicity forcing users to call setupCoreSessions is preferred, mainly because this step is only needed when I'm in using the testing machinery.

I still think that some stuff in plone.app.testing that now is explicity needed to be set (like session or default workflows for content types) should be take for granted, but overridable somehow for those cases that it shouldn't be, to avoid these WTF moments for new Plone users.

Well, this would be hard to implement and this opinion isn't the scope of this issue. They are just my 0.02c. Thanks anyway.

tomgross commented 9 years ago

I agree that it is sometimes hard (even for long time Plone users) to figure out what is given and what is not in unittests but the general philosophy of unittests is to start with the minimal possible configuration and add stuff as needed. The usage of session should at least be properly documented. I filed a corresponding issue.