Test Objects, Not Methods
Posted July 16, 2009 by
Industrial Logic
I'm so often asked: "When microtesting, doing TDD, is it okay to test a private method?" The answer, at least literally speaking, is no!
Let me explain.
Imagine I begin test-driving a new object. As I consider my first test, I have to answer a design question: "What does this object do?" In other words, what can other objects ask of it and what should they expect to get or happen? I decide, then merrily go about writing tests to tease out the interesting details of this general behavior.
Before long, I find something I need this object to do that complicates things. Complicates them to a point that tempts me to ponder, "Gee, I could just expose that new tricky private method by making it protected and test its logic directly. That sure would make things cleaner and simpler."
And in one sense, I'd be right - that code deserves its own tests. But in a more important sense, I'd be wrong. Wrong in that what I should have realized is that this private method on Object A is really just begging to be the public method on a new Object B.
This private method really just wants to be a public method on another new object!
Boom, I've just made a design decision! And wow, one that was driven out of just listening to my tests.
Keep yourself focused on writing tests for the desired behavior of your objects, rather than for the methods they contain.
When you can do this, you're sure to get more of TDD's real benefit - its power as a design tool! -
pschwarz