Writing Good Tests

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 test-driven development (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.

this is a shameless plug... makes want to stop receiving posts from this group... at least place a disclaimer at the first sentence

Like
Reply

To view or add a comment, sign in

More articles by David Scott Bernstein

  • Green Tests and Red Tests

    In practice, I found that there are times that I want a bit more test coverage than I get from just doing test-first…

  • Radiators and Silos

    In the old days of corporate America, the way you got ahead was through hard work and perseverance. You strove to…

    2 Comments
  • Makers and Menders

    I’ve been getting back into some research interests of mine that require data acquisition from a variety of sensors so…

  • Core Developer Practices

    Every field of engineering has a core set of practices that they follow and software engineering is no different. But…

    1 Comment
  • Still XP After All These Years

    Are you humming in your head Paul Simon’s “Still Crazy After All These Years”? I am. And it does seem crazy.

  • The Importance of Continuous Integration

    Perhaps the most important yet easiest to implement of all the software development practices in Agile is continuous…

  • The Importance of Technical Practices (Again)

    Software development has undergone many revolutions over the last few decades. The way we build software today is…

  • Summary of Seven Strategies Series

    I finished my “Seven Strategies” series of 72 blog posts with seven strategies for implementing each of the nine…

    1 Comment
  • Refactor to Learn What Not to Do

    One of the things that I was not expecting when I started refactoring other people’s code was that I started to see…

    4 Comments
  • Refactor to Clean Up Before Moving On

    Of course, the best time to refactor code is while it’s fresh in your mind, right after having worked with it. Once I…

Others also viewed

Explore content categories