C# – Unit testing internal methods in VS2017 .NET Standard library

.net-standardcunit testingvisual-studio-2017

I am currently playing around with the latest Visual Studio 2017 Release Candidate by creating a .NET Standard 1.6 library. I am using xUnit to unit test my code and was wondering if you can still test internal methods in VS2017.

I remember that you could add a line in AssemblyInfo.cs class in VS2015 that would enable specified projects to see internal methods:

[assembly:InternalsVisibleTo("MyTests")]

As there is no AssemblyInfo.cs class in VS2017 .NET Standard projects, I was wondering if you can still unit test internal methods?

Best Answer

According to .NET docs for the InternalsVisibleToAttribute:

The attribute is applied at the assembly level. This means that it can be included at the beginning of a source code file, or it can be included in the AssemblyInfo file in a Visual Studio project.

In other words, you can simply place it in your own arbitrarily named .cs file, and it should work fine:

// some .cs file included in your project
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("MyTests")]