Unit testing with webtest problem

69 views
Skip to first unread message

Richard Arrano

unread,
Sep 9, 2012, 1:06:33 PM9/9/12
to google-a...@googlegroups.com
Hello,
I've been using webtest to unit test my application and I've encountered a strange issue. I wrap many of my get/post handlers in decorators and in some of those decorators, I put ndb.Model instances in os.environ for later use in the subsequent handler. This works on my local dev server and in production. However, when I run nosetests it always gives me an error:
 
os.environ["user"] = user
File "C:\Python27\lib\os.py", line 420, in __setitem__
    putenv(key, item)
TypeError: must be string, not User
 
Any ideas on how to mitigate this so my tests won't error out at this point?
 
Thanks,
Richard

Guido van Rossum

unread,
Sep 11, 2012, 9:09:15 AM9/11/12
to google-a...@googlegroups.com
You'll probably get more help from StackOverflow.com. You'll need to provide more info, nobody can help you debug this with just that traceback information unless they're psychic.

Richard Arrano

unread,
Sep 11, 2012, 5:00:21 PM9/11/12
to google-a...@googlegroups.com
What I'm asking more specifically is what does App Engine do, if anything, to os.putenv? Because using the App Engine console, I can do things like:
 
os.environ["x"] = ndb.Model()
 
and access it with os.environ["x"] without a problem. However, when I pull up the interpreter outside of Python and attempt to put anything other than a string into os.environ, it will balk:
 
import os
>>> import os
>>> os.environ["abc"] = {}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

  File "C:\Python27\lib\os.py", line 420, in __setitem__
    putenv(key, item)
TypeError: must be string, not dict
>>>
 
So it seems like App Engine does something to the putenv call where it serializes model instances into strings first, and if that's the case must also do a similar operation to os.getenv to deserialize them. What is App Engine doing, if anything to the getenv/putenv calls?
 
Thanks,
Richard

Brian Quinlan

unread,
Sep 11, 2012, 5:09:56 PM9/11/12
to google-a...@googlegroups.com
On Tue, Sep 11, 2012 at 4:00 PM, Richard Arrano <ricka...@gmail.com> wrote:
> What I'm asking more specifically is what does App Engine do, if anything,
> to os.putenv? Because using the App Engine console, I can do things like:
>
> os.environ["x"] = ndb.Model()
>
> and access it with os.environ["x"] without a problem. However, when I pull
> up the interpreter outside of Python and attempt to put anything other than
> a string into os.environ, it will balk:
>
> import os
>>>> import os
>>>> os.environ["abc"] = {}
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
>
> File "C:\Python27\lib\os.py", line 420, in __setitem__
> putenv(key, item)
> TypeError: must be string, not dict
>>>>
>
> So it seems like App Engine does something to the putenv call where it
> serializes model instances into strings first, and if that's the case must
> also do a similar operation to os.getenv to deserialize them. What is App
> Engine doing, if anything to the getenv/putenv calls?

Modifying os.environ in App Engine doesn't actually result in
getenv/putenv calls. You can see the class used here:
http://googleappengine.googlecode.com/svn/trunk/python/google/appengine/runtime/request_environment.py

Please file a bug if the incompatibility between regular Python and
App Engine os.environ is causing you problems:
http://code.google.com/p/googleappengine/issues/list

Cheers,
Brian

>
> Thanks,
> Richard
>
> On Tuesday, September 11, 2012 6:09:16 AM UTC-7, Guido van Rossum wrote:
>>
>> You'll probably get more help from StackOverflow.com. You'll need to
>> provide more info, nobody can help you debug this with just that traceback
>> information unless they're psychic.
>>
>> On Sunday, September 9, 2012 10:06:33 AM UTC-7, Richard Arrano wrote:
>>>
>>> Hello,
>>> I've been using webtest to unit test my application and I've encountered
>>> a strange issue. I wrap many of my get/post handlers in decorators and in
>>> some of those decorators, I put ndb.Model instances in os.environ for later
>>> use in the subsequent handler. This works on my local dev server and in
>>> production. However, when I run nosetests it always gives me an error:
>>>
>>> os.environ["user"] = user
>>> File "C:\Python27\lib\os.py", line 420, in __setitem__
>>> putenv(key, item)
>>> TypeError: must be string, not User
>>>
>>> Any ideas on how to mitigate this so my tests won't error out at this
>>> point?
>>>
>>> Thanks,
>>> Richard
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-appengine/-/er6JJQK-EJoJ.
>
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-appengi...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
Reply all
Reply to author
Forward
0 new messages