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

8

83

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 ?

Kindergartner answered 9/9, 2008 at 13:58 Comment(0)
J
111

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.

Jujutsu answered 9/9, 2008 at 13:59 Comment(1)
I just did this successfully with a strong named assembly following the guidance that aku links to. I did have one issue though. After getting everything configured I had to restart Visual Studio for the Intellisense to work. It would compile before restart but would always show the red lines.Lifeless
G
21

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

Glyph answered 9/9, 2008 at 14:17 Comment(0)
B
9

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!

Bursitis answered 9/9, 2008 at 15:40 Comment(3)
Interesting approach. And I totally agree about tests being public. Stupid requirement, IMO.Histidine
@Kilhoffer: I've been complaining about that for years, and you're the first person to really agree with me. Thanks!Bursitis
If you use [Conditional("DEBUG")] on test classes then they should not be in the production assembly and not hurt the performance.Tel
C
2

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.

Clubfoot answered 9/9, 2008 at 14:1 Comment(0)
W
2

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.

Wylen answered 9/9, 2008 at 14:32 Comment(0)
O
1

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

Openminded answered 21/3, 2022 at 8:46 Comment(0)
H
1

in dotnet core we can add AssembyAttribute in the .csproj file

just add this into your .csproj

<!-- Make internals available for Unit Testing -->
<ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
        <_Parameter1>Myproject.Tests</_Parameter1>
    </AssemblyAttribute>
</ItemGroup>
<!-- End Unit test Internals -->
Hasseman answered 30/6, 2022 at 18:51 Comment(0)
V
0

Since .NET 5 (if i'm not wrong) you can simply do it in your .csproj

<ItemGroup> 
    <InternalsVisibleTo Include="MyFriendProject" /> 
</ItemGroup>

I know some people hate this kind of writing because is not readable, but in my usecase it just fits perfectly tbh.

Vector answered 4/1 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.