Writing tests first

I never thought I’d say that writing failing unit tests first could be a good thing to do. I grew up with Design by Contract, where we design our component and its interface and write the tests and implementation separately. In that world the tests and implementation would ideally be written by different people, though I’ve not seen that happen in practice.

This is a qualified support for writing the failing test first. I’m working on someone else’s code. It’s in Javascript, and it’s quite complex, and I don’t fully understand its behaviour at the start of the piece of work I’m doing. By the end of the piece of work I do en up understanding it.

So I add to the tests that already exist. I create tests for the new functionality I need to support and then run them. If I’m lucky some of them may just work. At the moment I’m taking code that assumed its data lived in a flat namespace with numeric keys and want to make it work with a structured namespace of string keys. Thanks to Javascript being weakly typed some bits did work.

Then I go and fix the tests that are failing.

This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.