Thursday, August 5, 2010

TDD: Only Unit Testing Public

Many times when I spoke to people about TDD and Unit Testing, quite surprisingly I will be trapped into a debate of "Unit Test Public only?".

My quick answer is "Yes".

Reason?
Internal, Protected, Private "properties" of an Assembly or Class are the abstraction for each of them respectively. Unit tests should never cross the border to break the abstraction intend of a design.

DISCLAIMER: "properties" I used here refers to methods, subtypes, property (fields) etc.

Come to think of this topic again, why would one asked the question of such like? (unit test public?)
My only view is that, these may be the people who are writing real implementation first before writing the tests. They knew the design and implementation before even writing unit test and they knew the importance of the internal implementation and they would like to secure it (secure logic implementation intend). Are these suggesting TDD practice? (Refer to my earlier TDD post HERE).

If one started to think of the application (usage) of code to be developed first before thinking how to develop it (or design it), one should start with the accessible content. (Which is public content). Get the usage right is the prime criteria for TDD and also many software quality assurance measure. From the righteous usage of the public "properties" corresponded with right expectation and that makes the "right" deliverable.

So, spend more time thinking of what may be "right" for the time of writing, and ensure unit tested the "right" intend that you would want to enforced. Internal implementation are abstraction that meant for future re-development or refactoring or enhancement that usually and very likely to change from time to time without prior noticed.

This way, you have done your team and yourself a favor by created some maintainable unit tests.

No comments:

Post a Comment