Doctests make me love unit testing

I’ve long had the nagging feeling that I’m not writing enough tests for my code. It’s kind of like doing the dishes, not exactly a task I look forward to.  The fun of programming is solving the problems, not writing code that tests whether your solutions actually work! 

What I love about Python’s doctest is that it fits so naturally into the flow of coding: you write the function signature, then write the docstring saying what it’s supposed to do, right?  So then, right there, you write one or more simple tests to demonstrate the basic functionality you expect — before you’ve written a line of code in the body of the function. You write the function, save the file and then — boom! — run the tests.  Immediate feedback.  And I start to feel like my approach to programming is changing, because I can see that writing tests is not just about due diligence or whatever, but it’s a technique that can actually help me write code.

And — you can paste in commands from the Python interactive interpreter unchanged.  Yet another reason why Python feels like it’s designed with the needs of programmers, human beings, in mind.  It’s trying to make your life easier, simpler, and more productive.  While Python isn’t perfect (no language is), those things alone make it rate pretty high in my book.