A blog about one guys quest to learn Python, Django and Google App Engine.
Feed: http://appengineguy.com/atom.xml

den 30 juni 2008

Proper Unit Testing of App Engine/Django

While there are a couple of things written on unit testing with App Engine, I unfortunately found them all lacking in one way or another. With this post, I'd like to describe how to unit test Django views in App Engine, using as little mocking as possible.

More specifically, I did not want to mock the Data Store Model objects, since that very quickly turns into mocking hell - you just end up spending three times as much time thinking about mocking than you are about code. Fortunately, after quite a bit of digging, I discovered that Google provides a temporary, in-memory datastore stub, specifically made for unit testing! They also provide stubs for mail, urlfetch and the user service. MAN that is nice!

For this example, I am using:

We will be using the Test Runner provided by Django. Django looks for tests in a number of places, one is tests.py - which it expects to be in same folder as models.py and views.py. To have django run your tests you go:

manage.py test nameofYourApp

The code

models.py

tests.py

views.py

address.html is basically just "{{ form }}" and some HTML.

There ya go. Post any questions as comments, and I will try to answer them!

9 kommentarer:

Gordon_Freeman sa...

Cool tutorial, although I'm having trouble reproducing your models.py.
Would be nice to get it :)

Mattias Johansson sa...

Hey Gordon!

Thanks for the suggestion - totally forgot that. I've updated the entry to include the models.py.

Gordon_Freeman sa...

Ha! That worked beautifully, with the models.py that is.
Thanks a lot!

rittee's product sa...

้hi ! Thanks

R2K sa...

: )

Joe Gelb sa...

thankyou

Music Lovers sa...

nice

Mr. Nissan sa...

I don't know what any of this is

Trolleydude sa...

Yeah, this tutorial helped a lot. Thanks dude.