Thursday, January 20, 2011

Django Unit Test Hobby

I have played with Django, but never really did any work in it. Even my toy app died before I got to a "good working model" state with it, because I did not maintain focus and wandered off too much. Since then I've developed a keen interest in learning more web technology, and so this time is Django Charge 2.0.

I visited some very nice, very smart people this week to took me into their offices and showed me some of their Django code. We started to talk about testing, and I made a small abortive attempt to do some plain-old-unit-testing in their python code base. It was not all that I hoped it would be (Dean, please revert that code if you haven't already). I ran out of time to play, but hopefully will be back to enjoy some pair programming goodness in the near future.

The cool and interesting bit about Django testing is that testing through manage.py will have you standing up an instance of the app complete with database. The setup is fairly onerous, but the tests run quite quickly. I think a lot of good integration testing can be done this way, but I wanted to do proper unit testing, where "proper" means that I do not stand up an application instance, don't use the database, and all of my unit tests (even when there are 20K of them) will run in less than 45 seconds. This becomes hard because Django's object mapper is in the DNA of django model objects. I guess I'll end up designing around that limitation, hopefully without my head exploding.

The other complication is the virtualenv. We definitely want to run the tests in the virtual environment, but don't want to launch a shell instance to do it. I'd like to autonose this, or better yet hook it into pyCharm and/or eclipse pyDev. It shouldn't be much of a problem... worse case should be launching the tool inside the virtualenv.

I'm not interested in doctest. I have a long-standing phobia against the use of magic comments. They offend my sense of how things should work. I'll be doing unittest (unless I succumb to pressure to write my own framework).

Today I begin my new hobby. I'll follow up here with links to interesting Django unit test links, and probably little discoveries. I'm not going to rush to solution, but try in code-camp style to work perfectly instead. It will be fun.

1 comment:

  1. I've had the same concern about "proper" unit testing using Django. I haven't come up with much more than mocking Django's ORM, which seems to work fairly well using voidspace's mock library. Although there is still quite a bit of start up time. I've also opted to use Buildout rather than virtualenv (PyCharm has some Buildout support which is nice). I look forward to hearing what you come up with.

    ReplyDelete