pytest_gae

pytest plugin for apps written with Google's AppEngine
Download

pytest_gae Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Publisher Name:
  • Petras Zdanavicius

pytest_gae Tags


pytest_gae Description

pytest plugin for apps written with Google's AppEngine pytest_gae is a pytest plugin that helps you to test applications written with Google's AppEngine.Options: * --with-gae: Turns on this plugin * --gae-path: AppEngine's root (default google_appengine) * --gae-project-path: Your project's root (default ./)Others:This project was inspired by nose-gae plugin for nosehttp://code.google.com/p/nose-gae/Usage example:Let assume we have a directory that looks something like that./├── gae # AppEngine's root│ ├── ...├── src # Your project's root│ ├── app.yaml│ ├── index.yaml│ └── main.py└── tests # Tests' dir ├── test_handlers.py └── test_models.pymain.py:#!/usr/bin/env pythonfrom google.appengine.ext import webappfrom google.appengine.ext.webapp import utilfrom google.appengine.ext.webapp.util import login_requiredfrom google.appengine.api import usersfrom google.appengine.ext import dbclass MyModel(db.Model): my_field = db.StringProperty(required=False)class IndexHandler(webapp.RequestHandler): def get(self): self.response.out.write('Index')class UsersHandler(webapp.RequestHandler): @login_required def get(self): if users.is_current_user_admin(): self.response.out.write('Admin') else: self.response.out.write('User')def make_application(): return webapp.WSGIApplication(, debug=True)def main(): application = make_application() util.run_wsgi_app(application)if __name__ == '__main__': main()Testing modelstest_models.py:from google.appengine.ext import dbimport pytestfrom main import MyModeldef test_basic(): m = MyModel(my_field='Foo') assert 'Foo' == m.my_fielddef test_new_model(): m = MyModel(my_field='Foo') pytest.raises(db.NotSavedError, lambda: m.key())def test_saved_model(): m = MyModel(my_field='Foo') m.put() assert m.key()Using with WebTestWe could test our handlers with the help of WebTest library.We would create three funcargs' functions that allows us to test application: * From anonymous user perspective * From authorized user perspective * From admin perspectiveWe could do that by altering os.enviromenttest_handlers.py:import osfrom webtest import TestAppfrom main import make_applicationdef pytest_funcarg__anon_app(request): os.environ.update({'USER_EMAIL': '', 'USER_ID': '', 'AUTH_DOMAIN': 'google', 'USER_IS_ADMIN': '0'}) return TestApp(make_application())def pytest_funcarg__user_app(request): os.environ.update({'USER_EMAIL': 'simple@google.com', 'USER_ID': '1', 'AUTH_DOMAIN': 'google', 'USER_IS_ADMIN': '0'}) return TestApp(make_application())def pytest_funcarg__admin_app(request): os.environ.update({'USER_EMAIL': 'admin@google.com', 'USER_ID': '2', 'AUTH_DOMAIN': 'google', 'USER_IS_ADMIN': '1'}) return TestApp(make_application())def test_index(anon_app): assert "Index" in anon_app.get('/index')def test_user_with_user(user_app): assert "User" in user_app.get('/users')def test_user_with_anon(anon_app): assert '302 Moved Temporarily' == anon_app.get('/users').statusdef test_user_with_admin(admin_app): assert "Admin" in admin_app.get('/users')Runningpy.test tests --with-gae --gae-path=gae --gae-project-path=./src/ :platform linux2 -- Python 2.5.5 -- pytest-2.0.0collected 7 itemstests/test_handlers.py ....tests/test_models.py ...============ 7 passed in 0.64 seconds ============ Requirements: · Python · py.test Limitations: Plugin does not prevent You from using code/modules that AppEngine's environment refuse to execute. So, You can easily do something like that: · import socket · import numpy · And tests just pass. But You can not run this code on AppEngine environment, because of sandboxing. See: AppEngine Docs · This plugin uses internal AppEngine's code and there is no guarantee that Google is not going to change it.


pytest_gae Related Software