How do I allow assembly (unit testing one) to access internal properties of another assembly?

.NetUnit Testing

.Net Problem Overview


I would like my Core assembly to not expose a certain class and I would still like to be able to test it. How can I do that ?

.Net Solutions


Solution 1 - .Net

InternalsVisibleTo attribute to the rescue!

Just add:

[assembly:InternalsVisibleToAttribute("UnitTestAssemblyName")]

to your Core classes AssemblyInfo.cs file

See Friend Assemblies (C# Programming Guide) for best practices.

Solution 2 - .Net

With InternalsVisible if your assemblies are strongly named you need to specify the public key (note: the full key not the public key token) for example...

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("BoardEx_BusinessObjects.Tests, 
  PublicKey=0024000004800000940000000602000000240000525341310004000001000100fb3a2d8 etc etc")]

and the following trick is really useful for getting the public key without resorting to the cmd line...

http://www.andrewconnell.com/blog/archive/2006/09/15/4587.aspx

Solution 3 - .Net

I put my unit tests in the same assembly as the code that it's testing. This makes sense to me, because I think of "test yourself" as a feature of a class, along with things like "initialize yourself" and "describe yourself".

I've heard some objections to this approach, but few of them have been convincing.

It hurts performance Bah, I say! Don't optimize without hard data! Perhaps if you are planning your assemblies to be downloaded over slow links, then minimizing assembly size would be worthwhile.

It's a security risk. Only if you have secrets in your tests. Don't do that.

Now, your situation is different from mine, so maybe it'll make sense for you, and maybe it won't. You'll have to figure that out yourself.

Aside: In C#, I once tried putting my unit tests in a class named "Tests" that was nested inside the class that it was testing. This made the correct organization of things obvious. It also avoided the duplication of names that occurs when tests for the class "Foo" are in a class called "FooTests". However, the unit testing frameworks that I had access to refused to accept tests that weren't marked "public". This means that the class that you're testing can't be "private". I can't think of any good reason to require tests to be "public", since no one really calls them as public methods - everything is through reflection. If you ever write a unit testing framework for .Net, please consider allowing non-public tests, for my sake!

Solution 4 - .Net

I would suggest not going to such troubles ... if you really want to unit test your "internal" classes, just hide them away in a namespace that only your internal code would end up using. Unless you're writing a framework on the scale of the .NET framework, you don't really need that level of hiding.

Solution 5 - .Net

You can use reflection (as the MS Test items do), or you can declare the unit test assembly a friend of the core assembly.

The other option is to put the unit tests in the same assembly.

Solution 6 - .Net

Let's start with an example class:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("App.Infrastructure.UnitTests")]

namespace App.Infrastructure.Data.Repositories
{
    internal class UserRepository : IUserRepository
    {
		// internal members that you want to test/access
    }
}

The [assembly: InternalsVisibleTo("Input_The_Assembly_That_Can_Access_Your_Internals_Here")] attribute allows all of your internal classes and members to be accessed by another assembly, but the InternalsVisibleTo attribute is not only applied to a single class (on where you declared it), rather on the WHOLE assembly itself.

In the example code - App.Infrastructure.UnitTests can access all internals in the assembly that you declared (InternalsVisibleTo attribute) it even if you didn't explicitly declare it to other classes that belong to the same assembly.


I found out in this youtube video: That there are 2 approaches to make your internals accessible to certain assemblies (or assembly)

1. Creating your own AssemblyInfo.cs file

These are the only lines you'll need in your AssemblyInfo.cs (delete all "default" code, and replace with the code below)

using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("App.Infrastructure.UnitTests"),
           InternalsVisibleTo("Another.Assembly")]

2. Adding the InternalsVisibleTo attribute in the project's .csproj (double click the project that you want its internals to be exposed)

<ItemGroup>
	<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
		<_Parameter1>The_Assembly_That_Can_Access_Your_Internals</_Parameter1>
	</AssemblyAttribute>
	
	<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
		<_Parameter1>Another_Assembly_Or_Project</_Parameter1>
	</AssemblyAttribute>
</ItemGroup>

Note: watch the whole youtube video for a more detailed explanation on Assembly-Level Attributes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomas PajonkView Question on Stackoverflow
Solution 1 - .NetakuView Answer on Stackoverflow
Solution 2 - .NetSimon KeepView Answer on Stackoverflow
Solution 3 - .NetJay BazuziView Answer on Stackoverflow
Solution 4 - .NetJoel MartinezView Answer on Stackoverflow
Solution 5 - .NetDan BlairView Answer on Stackoverflow
Solution 6 - .NetparaJdox1View Answer on Stackoverflow