Core Principles

I gave a talk about unit testing at a client recently, and I had these two random thoughts I wanted to share. There are a couple benefits to writing unit tests that are not quickly obvious, one, unit tests act as documentation for new developers. Good (and bad, I suppose) software stays around for a long time. You will not be the only developer working on the system. When I come on to an existing team, one of the first things I look at are the unit/integration tests. It gives me the most comprehensive interaction model as a user or developer.

Another thing unit tests help with is design. If your code has to be unit tested, that thought guides you to design loosely coupled and cohesive components. If your module/system/component is tightly (highly) coupled, than you have to do too much setup or configuration to even write a single test method, and possibly your unit tests become integration tests, and they will not run on your continuous integration server. If your code is not cohesive, then you end up writing multiple test classes for a single component.

If either of those cases are true, then you should revisit your design.