C# "internal" access modifier when doing unit testing

Hertanto Lie picture Hertanto Lie · Dec 11, 2008 · Viewed 145.4k times · Source

I'm new to unit testing and I'm trying to figure out if I should start using more of internal access modifier. I know that if we use internal and set the assembly variable InternalsVisibleTo, we can test functions that we don't want to declare public from the testing project. This makes me think that I should just always use internal because at least each project (should?) have its own testing project. Can you guys tell me why I shouldn't do this? When should I use private?

Answer

EricSchaefer picture EricSchaefer · Nov 27, 2009

Internal classes need to be tested and there is an assemby attribute:

using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("MyTests")]

Add this to the project info file, e.g. Properties\AssemblyInfo.cs.