Using GAE Testbed with GAEUnit: Testing that email was sent

In my quest for testing if emails are being sent by my app engine app, I came across GAE Testbed.  GAE Testbed

… 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.)

Since I’m already using GAEUnit as the unit testing framework for my app, I wanted to use GAE Testbed alongside GAEUnit so I only had to call one command (http://localhost:8080/test) to execute all my tests.  The documentation from the GAE Testbed home page is written to make it work alongside NoseGAE, so I decided to write one for GAEUnit.

Add GAEUnit to your app:

  1. Download the zipped archive of GAEUnit from its Google Code project hosting page.
  2. Extract the archive.
  3. From the folder extracted from the archive, copy gaeunit.py to your app’s root folder.
  4. Add the following 2 lines to your app.yaml, directly below the line that says handlers::
    - url: /test.*
      script: gaeunit.py
  5. (Optional) From the folder extracted from the archive, there’s a folder named sample_app and inside it is the modified version of the webtest module. Copy the webtest module (the entire folder containing debugapp.py and __init__.py) to the root of your app.

Add GAE Testbed to GAEUnit:

  1. Download the GAE Testbed tar gzipped archive from its Google Code project hosting page.
  2. Extract the archive.
  3. Inside the extracted archive is the gaetestbed module (it’s the folder named “gaetestbed”).  Copy the module to the root of your app.
  4. Create a file inside the test folder of the root of your app. For the sake of this example, let’s name it test_mailer.py.
  5. Using the example from the GAE Testbed Google Code project hosting page, add the following lines to test_mailer.py:
    import unittest
    from gaetestbed import MailTestCase
    
    class MyTestCase(MailTestCase, unittest.TestCase):
        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)

Start your server and go to http://localhost:8080/test. You should notice that (an additional) 1/1 test was ran from http://localhost:8080/test.

* Many thanks to John Geewax for creating GAE Testbed.

4 thoughts on “Using GAE Testbed with GAEUnit: Testing that email was sent

  1. Hi Kevin. I have never used NoseGAE, but from what i’ve read, NoseGAE requires the installation of some other packages, which I think makes it not very portable and doesn’t ensure that my app will work on other servers. What I like about GAEUnit is that you already know that it comes with your app when you deploy it.

    But I’m not sure. I’d like to know more about NoseGAE as well.

Leave a Reply

Your email address will not be published. Required fields are marked *