White Box vs. Black Box

A common question regarding TDD is whether you should use white-box or black-box testing. Should you test private methods or just methods that are publicly available (such as methods in the interface)?

I prefer to use black-box testing and test only the public interface with my automatic unit tests. The negative effect of this is that the public interface often gets a bit larger. I usually need to expose sub results as property gets. The bigger interface is read-only, so it's not a major problem. Sure, there's a risk of showing too much of the algorithm, but the larger interface often proves useful for the real consumer.

I also use ordinary assertions (such as System.Diagnostics.Trace.Assert) for pre- and post-conditions in my methods (similar to Design by Contract by Bertrand Meyer). Of course, those assertions will help when running the automatic unit tests too.

(Excerpted from my speaker tips, VSLive 2004)