Writing Good Tests

DZone 's Guide to

Writing Good Tests

Using unit tests while you write software will help to point out bugs earlier, allowing you to make the connection in the moment, rather than weeks or months later.

· Agile Zone ·
Free Resource

As Misko Hevery, the author of AngularJS, says in his outstanding Google Tech Talk series " Clean Code Talks," most developers assume they know how to write a good test but they don't.

Knowing how to write a good test is paramount to being successful with TDD. Tests must be unique, expressive, and independent - easier said than done.

Doing TDD correctly is a discipline. It takes a deep understanding of a large body of knowledge as well as a lot of experience doing it. You can't learn TDD from a 500-word blog post any more than you can learn brain surgery from one. It takes dedication, time, and practice.

I'm hooked on TDD but I guess I was predisposed to that since I've written automated test programs from the very start of my career as a developer and throughout the past three decades.

As I grow older my memory fades. This happens to all of us, although I didn't believe it would happen to me when I was younger. Fortunately, wisdom replaces energy.

I lean on TDD to help me manage all those details I need to manage when I code. I rely on instant feedback from my builds to tell me if what I just did was a good thing or a bad thing. And with that kind of instant feedback, I make the connection between what I just did and how it affects my code.

Stimulus and response have to go together in order for an association to be made. If Pavlov rang the bell then fed his dogs an hour later they'd never make the connection. That's why we don't make the connection between the bugs we write when they're found, weeks later, by QA.

But good unit tests help us make this connection by giving us immediate feedback whenever we make a change to the system. They also help us articulate the behavior we want to create and then validate that that behavior is working as expected.

Writing implementation independent tests by asserting against acceptance criteria or known behaviors gives us the freedom to change the implementation later without breaking our tests. Most developers weren't taught how to do this, but writing good tests are central to success with TDD. Writing a test for every public method often drives us to write implementation-dependent tests, which can break when we refactor code.

Good tests are extremely valuable because they help you build out behaviors in ways that make sense and they support you in refactoring your code later. But in order for tests to have these amazing benefits and more they must be good tests. They must be unique, at the right level of abstraction, based on acceptance criteria, and be implementation independent.

I have discussed some of the major pitfalls to doing TDD in my book, Beyond Legacy Code: Nine Practices to Extend the Life (and Value) of Your Software, as well as on my blog. I also teach classes for professional software developers. Where you get experience doing TDD correctly you can see the benefits for yourself.

agile ,agile testing ,tdd ,unit tests

Published at DZone with permission of David Bernstein , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}