Unit Testing Private Methods?
I was not going to go into anything too specific in the blog, just stick to general concepts, but I do have to mention one interesting point discussed today. There was a section of the lecture devoted to unit testing and the topic of writing unit tests for private methods was mentioned. So the question was “Should you write unit tests for private methods” and the answer was NO.
Basically, a class has an interface of public methods. Those public methods use private methods. You write test cases for the public methods to ensure all functionality the class provides is covered by tests. If you need to write unit tests specifically for the private methods, then your test cases for the public methods are probably not covering all cases, and if they do, then the tests for the private methods are just duplicates of the public method test cases. One of the main problems with unit tests on private methods is that they make it difficult to refactor the inner workings of a class.
A developer should have the freedom to refactor the code within a class without needing re-write existing unit tests. We should be working at removing obstacles to refactoring and creating meaningful interfaces to our code that are both useful for development and testing.
No comments yet.
Leave a comment
-
Recent
-
Links
-
Archives
- February 2009 (1)
- January 2009 (10)
- August 2008 (2)
- March 2008 (26)
- February 2008 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS