user3208634
user3208634

Reputation: 51

How to run python unit tests in google app engine

I'm fairly new to google app engine and python so please bear with me. I'm trying to run a python unit test on gae for first time while following the guide at Webapp2

But when I run the test I keep getting the following error :

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import webapp2
ImportError: No module named webapp2

This is my test.py file:

import unittest
import webapp2

# from the app main.py
import main

class TestHandlers(unittest.TestCase):
   def test_hello(self):
       # Build a request object passing the URI path to be tested.
       # You can also pass headers, query arguments etc.
       request = webapp2.Request.blank('/')
       # Get a response for that request.
       response = request.get_response(main.app)

       # Let's check if the response is correct.
       self.assertEqual(response.status_int, 200)
       self.assertEqual(response.body, 'Hello, world!')

if __name__ == '__main__':
    unittest.main()

This is my main.py file:

import webapp2

class HelloHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello, world!')

app = webapp2.WSGIApplication([('/', HelloHandler)])

def main():
    app.run()

if __name__ == '__main__':
    main()

This is my app.yaml file:

application: test-app
version: 1
runtime: python27
api_version: 1
threadsafe: true

- url: /.*
  script: main.app

libraries:
- name: jinja2
  version: latest

builtins:
- remote_api: on

My current folder structure is:

Test-app
         app.yaml
         main.py
         test.py
         index.yaml

And to run the test I'm doing:

$ cd test-app
$ python test.py

Can anyone point me in the write direction, on why am I getting the error message above and why can't I run this simple test.

I've tried to post as much info as i could, hope will be enough, for someone to give me a little hand.

Thanks.

Upvotes: 4

Views: 2485

Answers (3)

user3208634
user3208634

Reputation: 51

Thanks Tim Hoffman and cdonts for your replies they definitely got me to think about this, and Tim Hoffman you were almost right.

I mean webapp2 is already included as a library in google app engine (google_appengine/lib/webapp2-2.5.2). So the reason why i could not run my tests was because I did not added the google_appengine/lib to the $PYTHONPATH that's why the "import webapp2" was not working as expected.

Therefore and since I'm working with virtualenv what I did was simply to add the google_appengine/lib (directory) to the $PYTHONPATH of my virtualenv by running:

# add2virtualenv ---> adds/this/directory/to/the/PYTHONPATH
$ add2virtualenv google_appengine/lib/webapp2-2.5.2 

# check which directories have been added to the virtualenv
$ add2virtualenv
Usage: add2virtualenv dir [dir ...]

Existing paths:
google_appengine/lib/webapp2-2.5.2 

Tests are now running and working as expected, thank you both.

Upvotes: 1

Tim Hoffman
Tim Hoffman

Reputation: 12986

webapp2 is included in the google supplied runtime however you need to configure it's support by using the libraries directive in app.yaml

Please see docs on directly supported 3rd party libs https://developers.google.com/appengine/docs/python/tools/libraries27

In your case you would include

libraries:
- name: webapp2
  version: "latest" 

in your app.yaml and then you can deploy your code without having to include webapp2 in your codebase.

The other answer is correct for libraries not listed in the 3rd parties library documentation or for unlisted versions.

Upvotes: 2

cdonts
cdonts

Reputation: 9621

Google App Engine has support for some web frameworks: Django, web.py, webapp2, and others, but it doesn't include them. You need to put their source codes (or in your case webbapp2 source code) along with your application.

You can download the webapp2 framework from here.

So now you will have:

Test-app
     app.yaml
     main.py
     test.py
     index.yaml
     webapp2/

Make the deploy and you're ready!

Hope it helps.

Upvotes: -1

Related Questions