We are developing a new API at work. My colleague and I are having different opinion regarding whether internal classes should be unit tested or not.
Points given by my colleague for not unit testing internal classes
- The unit tests should be written against public classes/methods only
- You should be able to cover your complete code, including the internal classes via the public API itself. If your internal classes are not being tested via the public interface, that means there are some missing test cases, or you found a dead code which is not needed.
- Writing unit tests for internal classes might result in redundant testing or might force to write code which is never needed.
Points given by me in favor of unit testing internal classes
- Covering complete code through unit testing only public classes might be difficult/impractical. Writing unit tests for internal classes ensures that they behave correctly in their own world at least and we can have integration tests to test the correctness of whole application.
- Even if there are some redundant unit tests, it is better than having missing test cases at the top level.
- Internal classes are public to it's surroundings so we should test them independently. If there is only 1 public class and 100 internal classes, it is very difficult to cover all the scenarios via the public interface.
I tried searching online but seems like there is no best practise or standard opinion about this. What do you think is a good approach to follow?
Note to java people: "internal " is a keyword in C# which limits the visibility of the class to the assembly. The class won't be accessible outside the package/assembly. It is not equivalent to private class.