Richard Bender
Richard Bender

Reputation: 1134

App Engine Unit Testing with nosetests gae issue

I've been unit testing my application using nosetests with the with-gae plugin.

However, I've wrapped many of my get/post handlers with decorators that check certain items in the datastore and put them in os.environ for use in the handlers.

So I might have a decorator that does something like:

os.environ["user"] = User()

where User() is an ndb.Model instance. However, when I run nosetests, this will always fail and will give me an error like:

os.environ["user"] = user
File "C:\Python27\lib\os.py", line 420, in __setitem__
   putenv(key, item)
TypeError: must be string, not User

I had a similar thread here:

https://groups.google.com/forum/?fromgroups=#!topic/google-appengine/1KR7UIPHoQM

I was told App Engine doesn't actually call getenv/putenv in the os module.

The error is because in Python's normal os module, you cannot insert anything but strings into os.environ and nosetests is using the regular Python os module apparently.

However, App Engine uses request_environment.py to handle os.environ calls.

How can I mitigate this so nosetests will behave with os.environ calls?

Upvotes: 0

Views: 165

Answers (2)

Richard Bender
Richard Bender

Reputation: 1134

I found the solution for anyone else who might encounter this problem: call PatchOsEnviron in google.appengine.api.runtime before running tests.

Upvotes: 0

Renzo
Renzo

Reputation: 126

Another approach could be using the db stub (https://developers.google.com/appengine/docs/python/tools/localunittesting) to put your required entities on it just before testing.

Upvotes: 1

Related Questions