Unit testing + Google App Engine in Python 2
From my previous post, where I explained how to make GAE use your own pip package locally I just had to follow up with an additional short post about how you can unit test your pip package if you’ve got dependencies to Google App Engine, e.g. if you got deps to NDB Model.
My favorite IDE these days is PyCharm, so my example takes place in an environment where I’ve got the following setup:
- PyCharm
- Google App Engine SDK
- My own pip package (Python package with a setup.py) with deps to GAE
- Python 2.7.10
- MacOS 10.13.2 (High Sierra)
To be honest, I am not sure if virtualenv is something that fellow python developers use all the time, but I’ve learned the hard way that GAE dev works better with this tool. However, when you use PyCharm the default setup is to use the default Python interpreter in your system, in my case:
Python 2.7.10 located at /usr/bin/python
This makes things funny, because even if you setup your PyCharm project with a reference to GAE SDK, the test runner will not understand nor import your common GAE packages such as:
google.appengine.ext
google.appengine.api
Immediately I found this resource:
But doing:
import sys
sys.path.insert(1, 'google-cloud-sdk/platform/google_appengine')
sys.path.insert(1, 'google-cloud-sdk/platform/google_appengine/lib/yaml/lib')
sys.path.insert(1, 'myapp/lib')
Didn’t work for me — so after a little bit of googling I found a post on Jetbrains community forum saying that I should put this at the top of my ‘my_test.py’
import dev_appserver
dev_appserver.fix_sys_path()
I did this and it made the trick.
I also did a ‘pip install . --upgrade’ and reinstalled my package locally.
PS! Of course, all of this implies that you have the Google App Engine SDK installed.
For samples I would like to recommend the samples located here: