geewax.org home blog projects open source

GAE Testbed Documentation Released

I’d been looking to get familiar with Sphinx for a while, and couldn’t ever seem to find a project of mine that needed documentation enough to take the leap. This past week, I decided that even though GAE Testbed is a relatively small project, it could definitely use good documentation and finally got around to working with Sphinx.

I must say that I’m really impressed with the package as a whole (running it both on my MacBook and my Windows machine), though it was a bit tricky to get set up and familiar with the layout.

That said, feel free to take a look at the new GAE Testbed documentation which is located at http://gaetestbed.geewax.org/index.html. For those of you looking to get the code for GAE Testbed , it’s located on Google Code at http://gae-testbed.googlecode.com .

If you just want to get started using GAE Testbed, it’s easy_installable like this:

easy_install -U gaetestbed

TaskQueue Support in GAE Testbed

I’ve just finished preliminary support for testing the “experimental” Task Queue feature for Google App Engine applications. I needed this to test my own app, so I just buckled down and finished it today. The interface is pretty simple, very similar to the Mail testing interface.

As with all the other test cases, you can now inherit from the gaetestbed.TaskQueueTestCase (which is included in the UnitTestCase and FunctionalTestCase common base classes) and you then have access to the following methods:

self.clear_task_queue()

This called in setUp of TaskQueueTestCase to sandbox tests from each other. If you want to zero out the queue, you can use this at any point during your test.

self.get_task_queues()

This returns a list of dictionaries where each dictionary has a few details about the queue.

self.get_task_queue_names()

This returns a list of the available task queue names registered with the API stub. This is really the same as:

  [q['name'] for q in self.get_task_queues()]

self.get_tasks(*queue_names)

Returns a list of dictionaries where each dictionary contains the details for a specific task (such as its name, url, headers, or body). This will return the tasks for the specified queue_names, or if left blank will return all tasks across all queues.

Specific queues should be passed as positional arguments, for example:

  self.get_tasks('mailqueue', 'myotherqueue')

self.assertTasksInQueue(self, n=None, name=None, url=None)

This assert helper allows you to assert the contents of a queue with a few helpers This assert will allow you check check for various items in the queue, and ensures the test will fail if matching tasks aren’t found.

With all the default arguments left in place, it with assert that across all queues there are some tasks scheduled. That is, that not all queues are empty.

To assert just a specific number of tasks, use the n argument. For example, the following would make sure that across all queues there are exactly 5 tasks scheduled:

  self.assertTasksInQueue(5)

To assert a specific name or URL for the tasks, use the name or URL arguments. For example, the following would assert that there is at least one task across all queues that will hit the /workers/send-email/ resource if it were executed:

  self.assertTasksInQueue(url='/workers/send-email/')

When everything is put together, a sample test case might look like this:

  import unittest
  from gaetestbed import TaskQueueTestCase

  class MyTestCase(unittest.TestCase, TaskQueueTestCase):
    def test_taskqueue(self):
      # Check that nothing is in the queue
      self.assertTasksInQueue(0)
      
      # Add something to a Queue
      add_to_taskqueue(url='/worker/dummy/')
      
      # Checks that there are things in the queue
      self.assertTasksInQueue()

      # Checks exactly one item in the queue
      self.assertTasksInQueue(1)
      
      # Checks that 1 item with the specified URL is in the queue
      self.assertTasksInQueue(1, url='/worker/dummy/')

You can get this update via easy install:

sudo easy_install -U gaetestbed

First Version of GAE Testbed Released

I was writing a whole suite of tests for a project I built for Google AppEngine, and the tests were looking quite nasty, so I figured I’d hide the nastiness somewhere outside of all the tests. I first abstracted them up into a base class inside my project, and then realized that I’d probably end up copying and pasting the library around for all of my AppEngine projects. So instead of copying all over the place, I decided to throw the work into a “testbed” that anyone can pull down and use in their AppEngine app’s test suites.

The idea is you have different base test cases that you can mix in, and these bases do different things to make sure the slate is wiped clean between tests, and also gives you other “assert” methods to use with the AppEngine APIs.

For example, to check that e-mail was successfully sent, you could do the following:

  import unittest
  from gaetestbed import MailTestCase

  class MyTestCase(unittest.TestCase, MailTestCase):
    def test_email_sent(self):
      # Do something that sends an e-mail
      send_email_to('test@example.org')
      
      # Check that the e-mail was sent to somebody
      self.assertEmailSent(to='test@example.org')
      self.assertEqual(len(self.get_sent_messages()), 1)

Another neat thing is the ability to count the DataStore queries in a block of code. Using the with statement you can do:

  from __future__ import with_statement
  import unittest
  from gaetestbed import DataStoreTestCase
  from myproject.models import MyModel
  
  class MyTestCase(unittest.TestCase, DataStoreTestCase):
    def test_num_queries(self):
      # Check that no more than 1 query is run in this block of code
      with self.max_queries(10):
        # Whatever you do in here can't take more than 10 queries
        # Otherwise the test case will fail.
        MyModel(name='Name').put()

The project is available on PyPI as gaetestbed or on Google Code at gae-testbed.googlecode.com

It’s a really young project, so if anyone comes across new assert methods that are needed, or new features they want, definitely create an issue ticket on the Google Code site. So far it doesn’t have support for the Task Queue or XMPP APIs in GAE, but it does make it really easy to test the DataStore, Memcache, and Mail APIs.

Sending E-mail on OSX with the AppEngine Development Server

I had a lot of trouble figuring out how to get e-mail sending working with the AppEngine development server on Mac OSX. The solution was actually surprisingly simple:

  1. Open the AppEngine launcher
  2. Double click on your Application in the list
  3. In the “Extra Flags” section, add: --smtp_host=127.0.0.1
  4. In a shell, run sudo postfix start (you’ll be prompted for your password)

After that, you’re running a local mail server, and the AppEngine dev server should work exactly as you’d expect. If you restart your computer you may need to run sudo postfix start again.

CheckTree 0.3b1

I saw a fork of CheckTree by ycTIN that looked to be much faster so I figured I’d give a shot to optimizing some of CheckTree. I now have a first beta of 0.3 available on the Google Code project site (jquery-checktree.googlecode.com).

This version works fine with jQuery 1.3.2, and seems to be must faster than 0.2. I tested the two on a tree with about 250 items and found 0.2 to take almost 2000ms, where 0.3 took only about 400ms. I can’t really say for sure whether it is fully backwards compatible and bug free, so use at your own risk. Bug reports would be greatly appreciated.

You can see ycTIN’s fork of CheckTree here.

←newer older →