Unit testing internal classes

While working on class libraries, it’s a common and a good practice to unit test it before publishing. If you think about making implementation of interfaces hidden from other assemblies you may mark the class as internal. Unfortunately, it will make testing more difficult. Luckily there’s a way to handle it. All we need to do is finding our class library a friend. Friend assemblies can access internal types of another assembly.

I’ll present two ways of making a friend for the assembly:

  1. Use attribute [assembly: InternalsVisibleTo("FriendAssemblyName")] on any of classes
  2. Add following section to csproj file
    <ItemGroup><AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo"><_Parameter1>FriendAssemblyName</_Parameter1></AssemblyAttribute></ItemGroup>