Refatoring for Unit Testing

by byamabe on October 17, 2009

I’ve been working on LaG for a month or so. Progress has been slow, because I haven’t set enough short term goals. I really need to get back to my GTD weekly review to prioritize and get to a list of actions for the project.

Most of the work I have done has been to flesh out the data model. I had some basic functionality that I wanted to implement, and then I fleshed out the data model and functionality to support it. The one thing that was nagging me was that there weren’t any unit tests. I know that unit tests are a good thing. I’ve even done some TDD/BDD and have been very happy with the results. I’ve also had the experience where TDD/BDD has made me feel like I wasn’t actually moving closer to delivering anything of value. I was writing good (in my mind at least) code, but wasn’t getting closer to end user functionality. I decided that I needed to get some tests written and that it would also help me by focusing me on a task that would move LaG forward.

Part of the reason I had avoided writing tests was that I assumed that app-engine-patch didn’t support the same unit testing that is available in standard Django. Of course it took me all of one Google search to find that I was wrong. So, I fired up ./manage.py test and 26 tests ran and passed . Cool, now I had to add my tests.

No Business Logic in model.py

I recently read a tweet from UncleBob about isolating 3rd party API’s. For Django/app-engine-patch, I consider model.py to be owned by the API. It will be very different for each of the implementations and I would like to be able to port the app to a standard Django deployment if GAE is no longer appropriate. I found that some business logic has crept into model.py. The first thing I need to do is to write a test and pull this functionality out of model.py.

No db in the Business Layer

I also found that some google.appengine.ext.db calls had crept into my business layer. So my next task will be to write a test and remove any db calls from my business layer.

I think my decision to focus on just getting some functionality and database modeling done without testing was appropriate. I wanted to get a feel for what I would be able to do and what the application would feel like. But now, it’s time for me to clean up the code and get to TDDing. Sometimes l need to just write code to keep myself motivated, and other times I need to be more disciplined and follow a process to gain focus. I’m not sure the Agile term ‘spike’ is exactly write, but that what it feel like to me. A short interval where you experiment and just code, then go back to disciplined development.

Previous post:

Next post: