C# "internal" access modifier when doing unit testing


I'm new in 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?) has it's own testing project. Can you guys tell me a reason why I shouldn't do this? When should I use 'private'?

3/25/2015 9:50:53 AM

Accepted Answer

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

using System.Runtime.CompilerServices;


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

12/17/2014 11:51:28 PM

If you want to test private methods, have a look at PrivateObject and PrivateType in the Microsoft.VisualStudio.TestTools.UnitTesting namespace. They offer easy to use wrappers around the necessary reflection code.

Docs: PrivateType, PrivateObject

For VS2017 & 2019, you can find these by downloading the MSTest.TestFramework nuget

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow