The Tao of Unit Testing

Careful programmers test early and test often.

As I begin to write unit tests for my project, I have decided to embrace the effective approach one must take in order to write programming unit tests and the best perspective one must posses when analyzing the usefulness of unit tests. A book, I own, title, Beautiful Code: Leading Programmers Explain How They Think” – best describes the practice of writing unit tests effectively by recognizing the fact that the unit test will motivate the code that you write. This posting will explain what this means.

Most programmers have had the experience of looking at a snippet of code and thinking it was not only functional but also exquisite. Code is typically considered exquisite if it does what it’s supposed to do with unique elegance and economy.

But what about the tests for that exquisite code – especially the kind of tests that developers write, or should write, while they are working on the code?

Any nontrivial code, exquisite or note, needs not one, but a team of tests, where each test should be focused on checking a specific aspect of the code, similar to the way different players on a sports team are responsible for different tasks and different areas of the playing field.

The main purpose of tests is to reconfirm ones confidence that the code works properly and efficiently. Therefore, to me, the most exquisite tests are those that help me maximize my confidence that the code does, and will continue to do, what it’s supposed to.

Some tests are brilliant for their simplicity and efficiency, other tests are exquisite because, in the process of writing them, they help you improve the code they are meant to test in subtle but important ways. They may not discover proper bugs or defects, but they bring to the surface problems with the design, testability or maintenance of the code.

Tests reveal ways to make code more elegant, maintainable and testable. The process of writing tests often helps you realize not only logical problems, but also structural and design issues with ones implementation. As well, very thorough tests increase confidence that the code performs as expected, not only in some basic cases, but in all cases.

Overall, one must think of testing as ones way of stepping aside from ones code to look at ones work with critical eyes and from different perspectives – it will make one a better programmer.

Cheers,

AaronMT

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: