The App Engine isn't too friendly for testing: you can't use interactive mode, and there's some setup needed to get a test harness. I've found that using doctest works quite nicely with the appengine, provided you get that initial setup out of the way. If your application interacts with the datastore, then you probably want to set up some objects before querying them and to my mind that sits better with the doctest model of a long chatty document describing a pseudo-interactive session than the xUnit style of separate tests each starting from a clean system doing something and making a single assertion about the resulting state. For a particular project you can update the test framework to perform common setup for the tests. The structure I've come up with sets up the environment to run tests under the appEngine framework. It looks for tests in two places: *.py files in the project directory, and tests/*.tests. For the Python files doctest will run tests from all docstrings found in the file (and you must have at least one docstring in every Python source file otherwise you'll get an error, but you don't have to actually have any tests in it). Each docstring forms a separate test. For the other files they are just plain text (or you can use reST) and the entire file is run as one test (so the state is preserved until the end of the file). Here's a sample doctest file: DateTime testing
Download an example appengine project with this test and execute tests/runtests.py to run it. |
Monday, 5 May 2008
Unit testing with Google App Engine
Posted by Duncan Booth at 10:39
Labels: Google App Engine
Subscribe to:
Post Comments (Atom)
4 comments:
Very nice, thanks! I've had to os.path.expanduser GOOGLE_PATH, and I chose to doctest everything in tests/*, but otherwise this has been tremendous help :)
How nice! That's exactly what I was looking for. Thanks a lot.
Could you show us a more "real" test. Where you actually test some of the code that is going to be used
good solution. Thanks
Post a Comment