Friday, August 22, 2014

Unit Testing Question

Hi there. I have a question for you. I'm looking for some articles/papers/blogs/talks about Unit Testing at the right level of granularity. Could you send me some links?

Let me try to explain what I mean. I've noticed that when you write what I'll call "good OO" (for the sake of simplicity) you tend to end up with individual objects which on their own don't do that much.  Instead the behavior that you care about emerges from the way in which the objects are composed together.  And this poses an interesting problem for unit testing.

If I define a unit as a single object, then I will write tests for each of those little objects and I'll mock/stub/fake out the collaborators.  But since each object doesn't do much this means my tests do not end up describing the behavior of the system that I'm usually concerned with.  These tests might also get in my way during new development or refactoring because the "low level" individual APIs are locked in by the tests.

Alternatively, I could write tests that exercise the full composition of all the objects.  These tests would be closer to describing the "end user" level behavior that I more likely care about.  They are also more robust in the face of change as the lower level API can change without breaking these tests.  But, they can also suffer from Cyclomatic complexity problems because they are testing many levels of objects together.

These last tests can be taken all the way to the edge of the application and written as Acceptance Tests that literally automate the web browser.  Or they can be written like a regular unit test, but the "unit" is at a higher conceptual level, not just a single object.

Finally, these are not mutually exclusive options.  I could write both kinds of tests.

So to loop back, my question for YOU is, have you seen any good resources that discuss this issue and make any recommendations?  If so, I'd love a link, or, share your opinions on the issue.  Thanks!

No comments:

Post a Comment