Geeks With Blogs



Add to Technorati Favorites

An Archived Managed World This blog has moved to

I kind of touched on this issue in my last Tanks update. Thomas was nice enough to leave a comment and a follow up post that he made. I went to leave a comment on his blog in response to his post but "anonymous" comments aren't allowed and, frankly, I don't want to sign up for an iBlogger account simply so I can leave a comment. So, with that said, I'm just going to post my response here.


When writing unit tests, I no longer really think about methods. What I have started to think about is a behavior or responsibility of the class under test (which is _really_ done if you're doing TDD). If this test involves testing multiple methods so be it. Then if the behavior changes, you only have to change a small amount of tests.

I also believe that thinking about behavior when testing encourages developers to think about the "Single Responsibility Principle". If you're focusing on behavior and the test is hard to write, there is a good chance that the behavior of your class is too convoluted or complicated to test well. Or if the test becomes very long, it is likely that the responsibilities imposed on the class are too great.

In my opinion, developers need to get used to thinking about the "test matrix" for the class under test and focusing on code coverage. The number of "edge cases" can be minimized by using a Design By Contract approach to ensure the state of the class is always in an expected state. This way you don't have to have a separate test case for everything under the sun (fail fast, fail often). So, if I can get full coverage of a behavior by simpling expanding the scope of one test (like sending more commands/events through the system), than I will do that and get higher test coverage using a single test.

Of course you always have to be conscious of striking that balance between a test that gets a lot of code coverage done versus a test that is hard to read because it is too large and convoluted itself.

However, my opinion is still morphing on this subject the more unit testing I do. My only concrete rule of thumb is that if you start testing the abilities of the framework itself, you've dug too deep (like setting a property and asserting the value is what you expect it to be). A lot of the scenarios that test the behavior of the framework itself can be covered by "side effect" where other tests would fail if the framework every changes the way it behaves (but how likely is that?).

Anyone have any strong thoughts or opinions on the subject of unit testing granularity? When is it too granular, when is it not granular enough? Where do you draw the line?

[Crosspost from Managed World]

Posted on Thursday, May 4, 2006 5:38 PM | Back to top

Comments on this post: Unit Testing Granularity

# re: Unit Testing Granularity
Requesting Gravatar...
I have been trying to get my head around some of these concepts for a while. You and many others state that if it is complex to test then you haven't done your job in the design. The question I have is what do you consider complex.

If I have an object graph that contains a large amount of data are you saying that I should only test the pieces within the graph and not the graph as a whole? If the parent object has a validate method in order to test it I need to populate all the children. This could be fairly complicated from a testing perspective. Are you saying this is acceptable, don't do this test, or my design is wrong.
Left by Tim Murphy on May 05, 2006 3:18 AM

# re: Unit Testing Granularity
Requesting Gravatar...
I totally agree with you that when we consider the behaviour of the class, we can focus more on what kind of questions we can include in the test. hydrophobic glass Also, it helps to determine the length of the test and how that would be beneficial to the class eventually.
Left by Susen on Oct 28, 2015 4:13 PM

# re: Unit Testing Granularity
Requesting Gravatar...
i like to learn window 10 learning in easy step by step follow this link address windows 10 support and learn online window 10 by best quality images.thank you so much for this amazing website.
Left by best window 10 learning website on Nov 09, 2017 5:53 PM

# re: Unit Testing Granularity
Requesting Gravatar...
My view on this is that, if you are doing unit testing it is definitely going to to take a long time. Especially when the class you are using is being called in many areas. At the same time it can be something that will help point out any errors accurately.
electronic health record software
Left by Corbin Gravely on Nov 15, 2017 6:02 AM

Your comment:
 (will show your gravatar)

Copyright © Jason Olson | Powered by: