Jul 23

Automating tests in Django

Updated

At work lately we've been writing a bunch of tests for all of the work we've been doing. This is generally a good thing (tm). I was getting tired of manually having to write all of the code to test the views inside of my app. So I decide to write a little app that helps me automate the writing of tests.

I wrote a piece of middleware (that should obviously only be used during development!) that shadows the current activity in django into a log file. This log file should then be ready to copy and paste into a doctest for easy testing of your views. This is a little hard to explain, but the code should be pretty self explanitory.

I created a google code projectfor it so that people can go ahead and hack on it and make it better. It is pretty rudimentary at current, but it gets the job done.

I think a big win from this approach is that your testing data is much more "real", since it's a copy of your session with a real browser. I know writing django tests I sometimes use contrived data because it is a pain to enter it all. This should help improve on that situation.

Here is a video of it in action, this should allow it to make more sense.


Django TestMaker from Eric Holscher on Vimeo.

Writeup

Figured it would be good to writeup the screencast.

Step 1: Get django-testmaker svn checkout http://django-testmaker.googlecode.com/svn/trunk/ django-testmaker-read-only

Make sure the testmaker module is in your PYTHONPATH.

Step 2: Add
'testmaker.middleware.testmaker.TestMakerMiddleware',

to your MIDDLEWARE_CLASSES in your settings file.

Step 3: Run the test server with the middleware installed. ./manage.py runserver

Browse around your site.

Step 4: run tail -f /tmp/testmaker.log

to see your output.

Step 5: Take the output from testmaker.log and put it into a file in PROJECT/tests.py. Make sure that your tests.py contains:

"""
>>> from django.core.management import call_command
>>> call_command('loaddata', 'PATH/TO/PROJECT/fixtures/PROJECT.json', verbosity=0)
>>> from django.test import Client
>>> c = Client()
YOUR TESTS GOES HERE
"""

at the top of your tests.py file.

Step 6: Run the command ./manage.py dumpdata > PROEJCT/fixtures/PROJECT.json

You can have dumpdata just dump the data for a single project if you provide PROJECT as an argument to it. Be warned though, that the tests might break because of it using data from other apps. (Like my example would break because the mine project uses data from my blog app.)

Step 7: ./manage.py test PROJECT

Step 8: PROFIT!!

Update

I added a management command to the project to simply this process a ton. I'll be making another screencast and blog post (and maybe even some REAL DOCS!) tonight, so stay tuned for that.


Comments

1 says...

Sorry comments were broken. I did some template refactoring and forgot to move my comments over. They should be working now!

Posted at 2:23 p.m. on July 24, 2008

2 says...

great job Eric. i look forward to seeing this project grow and more screencasts from you. awesome stuff.

Posted at 2:29 p.m. on July 24, 2008

3 says...

This is HOT!

Posted at 3:03 p.m. on July 24, 2008

4 says...

Eric, I couldn't get this to work - does it rely on a particular version of django/python ?

Thanks.

David

Posted at 3:21 p.m. on July 24, 2008

5 says...

@David, It should run on anything that django does. It uses the logging module included with python starting at 2.3, and the rest is django.

Posted at 3:34 p.m. on July 24, 2008

6 says...

Nice job on the screencast. I look forward to seeing more of them from you. :) There is one thing about this that doesn't quite sit right with me and that's that it seems like the testing process is backwards. If you're creating tests based on what you have how are you possibly going to cover what's specified but not implemented properly?

Posted at 5:39 p.m. on July 24, 2008

7 says...

@empty: This isn't meant as a complete testing solution, just a way to record current behavior. Assuming your code works as expected, you can do this and have a record of how it worked. Then if you change things you will know if it breaks. Testing models and other more extensive testing would also be good, but this is a good baseline, and it is a non-trivial starting point for creating tests.

Posted at 7:46 p.m. on July 24, 2008

8 says...

@Eric Holscher: Sorry, I spoke to you about this on IRC - the logging was conflicting with the logging in Satchmo. I have made a slight modification to the middleware to get around it now.

Posted at 9:23 a.m. on July 25, 2008

9 says...

@empty: I'm kind of thinking of it as a way of automatically running over a bunch of clicks in a browser without having to click them - a bit like Selenium, I guess.

Posted at 9:27 a.m. on July 25, 2008

10 says...

@Eric and @David Reynolds - any chance we can the slight modification to the middleware posted here? I work with satchmo as well and love to see the workaround.

Posted at 2:27 p.m. on July 25, 2008

11 says...

@Kevin: I'm releasing a new version of the app tonight that might help. I think that satchmo is using logging as well, and it's using that logging output module. I know a workaround for it, and i'll test it out tonight or tomorrow, and mention it in my next post.

Posted at 8:45 p.m. on July 25, 2008

12 says...

@eric - thanks. also, i'm using my wife's laptop, IE 7, and the grey font color for the commentor's name is barely legible. probably her color settings but since you're working with a dark bg I'd go with a softer grey, or a simple a white would work. or just ignore :P

Posted at 12:42 a.m. on July 26, 2008

13 Eric Holscher says...

Testing akismet comment stuff.

Posted at 3:03 a.m. on October 7, 2008

14 Tim says...

Looks like a really good idea, but when I run it I quite often get "dict objects are unhashable".

Any thoughts? Could it be a dict that has been sent through to a template?

Posted at 8:59 p.m. on October 16, 2008

15 Optoppyvaky says...

?????? ???????????? ??????????: ???????. ??? ??? ??????? ????????? ???????? ?? ?????? ????? ??? ????????? ???????? ???????? ???????, ????????? ?? ?????????????? ???????, ???????????? ? ??????????, ?? ??????? ???????????? ??? ? ???????? ???????? ???? ??????? ????????? ???????? (?????, ??????, ?????, ???????, ????????. <a href=http://skuper.ru/makers.php?maker_id=24>??????? anbert</a> <a href=http://skuper.ru/sect_about.php?section_id=42>????????? ???????</a>

???????? ?? ?????????? ??? ????? ????????????? ??? ????????? ? ?????????? ? ?????????? ?????????? ???????????. <a href=http://skuper.ru/makers.php?maker_id=9>??????? Kronostar</a> <a href=http://skuper.ru/sect_about.php?section_id=40>??????? ???</a>

???????? ??? <a href=http://skuper.ru>Skuper</a> <a href=http://skuper.ru>Skluper</a>

Posted at 9:54 p.m. on December 7, 2008

16 Beldheede says...

?????, ??????? ??????????? ??????????, ?????????? ????????? ??? ????????? ????????, ???????? ???? ??? ??????. ?, ??? ?? ???????, ? ????? ??????, ??? ?????? ??????? ? ???? ????? ?????????? ?? ??????, ??????, ????? ??????????? ?????????????? ????? ?????? ????????????? ???????, ?????????? ????????? ??????????????, ? ????? ???????? ? ??????? ??????, ?? ???????? ????????????? ?????????????? ?????????? ???????????, ?? ????????????. <a href=http://stroi-mk.ru>????????????? ??????</a> ???????????? - ??????? ? ??????????? ????????. ?????? ??????? ?????? ?? ?????? ????????? ????? ? ????? - ??????????. ???? ?????? ??????????? ???????? ?? ???????? ??????????? ????? ?? ?????????. ??????? ?????? - ????? ??????? ?? ????????????? ??????????. ??????? ?????? ??????? ?????? - ????????? ?????? ??????????? ???????? ????????. ????????????? ?????? ? ????? ???????????? ? ????????????? ??? ?????????? ????????????, ???? ? ???????????, ?????? ?????, ???????.

<a href=http://construct-m.ru>construct-m</a>

Posted at 9:54 a.m. on December 11, 2008

17 HoaveTreloary says...

Hi! My name is Jessika!

Posted at 6:01 p.m. on December 12, 2008

18 soitlepeell says...

? ???????? ????? ????? ????????? ????????????? ????????????????? ??????, ???????????? ????????? ???????? ? ????????? 18-???????? ???????? ??????. ????????? ?????? ????????? ? ??????? ???????, ????????? ??????? ?????????, ???????? ??? ?????? ? ???????? ??? ?????? ????????? Suzuki Swift ??????? ????? ??????????? ????????. <a href=http://bs-avto.ru>??? ? ???????</a> ??????? ??????????? ??????? ????? ????????????? ????????? ???????. ??, ??? ????????????? ??????? ?? ?????????,, ????? ????????: ??????? ?????, ???????, ??? ????? ????????? ?????? , ??? ????? ??? ????? ??????????? ?????????? ????? ????????? ? ??????? ???????? ??????????? ???. <a href=http://bs-avto.ru>???</a> ???????? ??????-???????? Mattig ??????????? ???? ?????? ???????????? ?????????? Suzuki Swift. <a href=http://bs-avto.ru>bs-avto</a>

Posted at 7:51 a.m. on December 15, 2008

Comment are disabled for this post.