How to test internal class library?
For the latest csproj 2017 formated projects, if your project does not have the AssemblyInfo.cs
file, you can add the following setting:
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(MSBuildProjectName).Tests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
You also can use other variables to relplace MSBuildProjectName
such as AssemblyName
or use the unittest project name directly.
You can check the ProjectName.AssemblyInfo.cs
in obj folder (obj\Debug\netstandard2.0
) has been updated by adding InternalsVisibleTo
.
In .NET, you can use the InternalsVisibleToAttribute in your class library to make your internal types visible to your unit test project.
That way, you can keep your class internal and still use it from other assemblies that you give access.
You use it like this:
[assembly:InternalsVisibleTo("NameOfYourUnitTestProject")]