gaetestbed

A set of test cases to simplify testing on AppEngine
Download

gaetestbed Ranking & Summary

Advertisement

  • Rating:
  • License:
  • GPL
  • Price:
  • FREE
  • Publisher Name:
  • JJ Geewax
  • Publisher web site:
  • http://code.google.com/u/jgeewax/

gaetestbed Tags


gaetestbed Description

A set of test cases to simplify testing on AppEngine gaetestbed is a Python library that provides a set of base test cases that can be mixed into your existing test cases.They provide additional features to sandbox each test (by clearing the DataStore, Memcache, etc) and also add in additional assert style statements.Writing tests for AppEngine applications seems more difficult to me than it should be. This project is a set of base test cases to make it simple to test the more complicated pieces of AppEngine's framework (such as sending E-mail messages, the datastore, Memcache, etc).How to get it...sudo easy_install gaetestbedSome Example Test CasesHere are a few examples of how GAETestbed makes testing the complicated parts of AppEngine really simple.Testing that E-mail was Sent(as seen on StackOverflow)import unittestfrom gaetestbed import MailTestCaseclass MyTestCase(unittest.TestCase, MailTestCase): def test_email_sent(self): send_email_to('test@example.org') # Some method that sends e-mail... self.assertEmailSent(to='test@example.org') self.assertEqual(len(self.get_sent_messages()), 1)Testing that Memcache was Hitimport unittestfrom gaetestbed import MemcacheTestCaseclass MyTestCase(unittest.TestCase, MemcacheTestCase): def test_memcache_gets_hit(self): self.assertMemcacheItems(0) self.assertMemcacheHits(0) add_to_memcache('something', 'something') # Add something to memcache somehow... self.assertMemcacheItems(1) self.assertMemcacheHits(0) get_page('/page_that_hits_memcache/') self.assertMemcacheItems(1) self.assertMemcacheHits(1)Testing that stuff was saved to the Datastore(Most of this is provided thanks to NoseGAE.)import unittestfrom gaetestbed import DataStoreTestCasefrom myproject.models import MyModelclass MyTestCase(unittest.TestCase, DataStoreTestCase): def test_datastore_gets_hit(self): self.assertEqual(MyModel.all().count(), 0) MyModel(name='Name').put() self.assertEqual(MyModel.all().count(), 1) def test_datastore_still_empty(self): self.assertEqual(MyModel.all().count(), 0)Optimization testing, test number of DataStore queriesfrom __future__ import with_statementimport unittestfrom gaetestbed import DataStoreTestCasefrom myproject.models import MyModelclass MyTestCase(unittest.TestCase, DataStoreTestCase): def test_num_queries(self): self.assertEqual(MyModel.all().count(), 0) # Check that no more than 1 query is run in this block of code with self.max_queries(1): MyModel(name='Name').put() def test_query_count(self): self.assertEqual(MyModel.all().count(), 0) MyModel(name='Name').put() self.assertEqual(MyModel.all().count(), 1) # Check that the number of queries total for this test case is under 100 self.assertTrue(self.query_count < 100)Web Testing(Most of this is provided thanks to WebTest.)import unittestfrom gaetestbed import WebTestCase, DataStoreTestCasefrom handlers import application # (application should be a WSGI app)class MyTestCase(unittest.TestCase, WebTestCase, DataStoreTestCase): APPLICATION = application def test_get_redirects(self): response = self.get('/') self.assertRedirects(response) def test_post_creates_model(self): self.assertEqual(MyModel.all().count(), 0) data = {'name': 'Name'} response = self.post('/create-my-model/', data=data) self.assertRedirects(response) self.assertEqual(MyModel.all().count(), 1) def test_get_with_cookie(self): response = self.get('/') self.assertRedirects(response) self.set_cookie('session_id', 'secret') response = self.get('/') self.assertOK(response) def test_cookies_cleared_between_tests(self): session_id = self.get_cookie('session_id') self.assertEqual(session_id, None)Mixing them All togetherA "Unit" test caseUnitTestCase has all of the basics except WebTestCase. This makes it useful for testing your models and library methods.import unittestfrom gaetestbed import UnitTestCasefrom myproject.models import MyModelclass MyTestCase(unittest.TestCase, UnitTestCase): def test_memcache_gets_hit(self): self.assertMemcacheHits(0) def test_datastore_gets_hit(self): MyModel(name='Name').put() self.assertEqual(MyModel.all().count(), 1) def test_email_sent(self): # (Send an e-mail) self.assertEmailSent()A "Functional" test caseFunctionalTestCase has everything the UnitTestCase does, with the addition of WebTestCase for testing web interactions using WebTest.import unittestfrom gaetestbed import FunctionalTestCasefrom myproject.models import MyModelfrom handlers import application # (application should be a WSGI app)class MyTestCase(unittest.TestCase, FunctionalTestCase): APPLICATION = application def test_memcache_gets_hit(self): self.assertMemcacheHits(0) def test_datastore_gets_hit(self): MyModel(name='Name').put() self.assertEqual(MyModel.all().count(), 1) def test_email_sent(self): # (Send an e-mail) self.assertEmailSent() def test_get_redirects(self): response = self.get('/') self.assertRedirects(response) def test_post_creates_model(self): self.assertEqual(MyModel.all().count(), 0) data = {'name': 'Name'} response = self.post('/create-my-model/', data=data) self.assertRedirects(response) self.assertEqual(MyModel.all().count(), 1)DependenciesThis set of cases was designed to run with NoseGAE, so to run the tests that way you'll probably want to download an install it. Additionally, to run the functional (web) tests, you'll need to grab WebTest. Both of these are easy-installable:sudo easy_install nosesudo easy_install nosegaesudo easy_install webtestBenefits * Each test is sandboxed so that you can assume all of the services are empty when you start your test. This way, there's no need to worry about cleaning up data between tests. * Helper assert methods that simplify testing such as assertMailSent() or assertMemcacheHits() Requirements: · Python


gaetestbed Related Software