<p>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 ?</p>

[{'answer_id': 51958, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 8, 'selected': True, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(VS.80).aspx" rel="noreferrer">InternalsVisibleTo</a> attribute to the rescue!</p>\n\n<p>Just add:  </p>\n\n<pre><code>[assembly:InternalsVisibleToAttribute("UnitTestAssemblyName")]\n</code></pre>\n\n<p>to your Core classes AssemblyInfo.cs file</p>\n\n<p>See <a href="https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/assemblies-gac/friend-assemblies" rel="noreferrer">Friend Assemblies (C# Programming Guide)</a> for best practices.</p>\n'}, {'answer_id': 51960, 'author': 'Dan Blair', 'author_id': 1327, 'author_profile': 'https://Stackoverflow.com/users/1327', 'pm_score': 2, 'selected': False, 'text': '<p>You can use reflection (as the MS Test items do), or you can declare the unit test assembly a friend of the core assembly.</p>\n\n<p>The other option is to put the unit tests in the same assembly.</p>\n'}, {'answer_id': 51986, 'author': 'Simon Keep', 'author_id': 1127460, 'author_profile': 'https://Stackoverflow.com/users/1127460', 'pm_score': 4, 'selected': False, 'text': '<p>With InternalsVisible if your assemblies are strongly named you need to specify the public key (note: the full key <strong>not</strong> the public key token) for example...</p>\n\n<pre class="lang-cs prettyprint-override"><code>[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("BoardEx_BusinessObjects.Tests, \n  PublicKey=0024000004800000940000000602000000240000525341310004000001000100fb3a2d8 etc etc")]\n</code></pre>\n\n<p>and the following trick is really useful for getting the public key without resorting to the cmd line...</p>\n\n<p><a href="http://www.andrewconnell.com/blog/archive/2006/09/15/4587.aspx" rel="noreferrer">http://www.andrewconnell.com/blog/archive/2006/09/15/4587.aspx</a></p>\n'}, {'answer_id': 52019, 'author': 'Joel Martinez', 'author_id': 5416, 'author_profile': 'https://Stackoverflow.com/users/5416', 'pm_score': 2, 'selected': False, 'text': '<p>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 <strong>really</strong> need that level of hiding.</p>\n'}, {'answer_id': 52200, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 3, 'selected': False, 'text': '<p>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".</p>\n\n<p>I\'ve heard some objections to this approach, but few of them have been convincing.</p>\n\n<p><strong>It hurts performance</strong> 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.  </p>\n\n<p><strong>It\'s a security risk</strong>.  Only if you have secrets in your tests.  Don\'t do that.</p>\n\n<p>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.</p>\n\n<p>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!</p>\n'}, {'answer_id': 71554850, 'author': 'paraJdox1', 'author_id': 11565087, 'author_profile': 'https://Stackoverflow.com/users/11565087', 'pm_score': 0, 'selected': False, 'text': '<h2>Let\'s start with an example class:</h2>\n<pre class="lang-cs prettyprint-override"><code>using System.Runtime.CompilerServices;\n\n[assembly: InternalsVisibleTo(&quot;App.Infrastructure.UnitTests&quot;)]\n\nnamespace App.Infrastructure.Data.Repositories\n{\n    internal class UserRepository : IUserRepository\n    {\n        // internal members that you want to test/access\n    }\n}\n</code></pre>\n<p>The <code>[assembly: InternalsVisibleTo(&quot;Input_The_Assembly_That_Can_Access_Your_Internals_Here&quot;)]</code> attribute allows <em>all</em> of your <strong>internal classes and members to be accessed by another assembly</strong>, but the <strong>InternalsVisibleTo</strong> attribute is not only applied to a single class (on where you declared it), rather on the <strong>WHOLE</strong> assembly itself.</p>\n<p>In the example code - <code>App.Infrastructure.UnitTests</code> 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.</p>\n<br>\n<h3><a href="https://youtu.be/noxNMji-DRw" rel="nofollow noreferrer">I found out in this youtube video:</a> That there are 2 approaches to make your internals accessible to certain assemblies (or assembly)</h3>\n<p><strong>1. Creating your own AssemblyInfo.cs file</strong></p>\n<p>These are the <strong>only</strong> lines you\'ll need in your AssemblyInfo.cs (delete all &quot;default&quot; code, and replace with the code below)</p>\n<pre class="lang-cs prettyprint-override"><code>using System.Runtime.CompilerServices;\n[assembly: InternalsVisibleTo(&quot;App.Infrastructure.UnitTests&quot;),\n           InternalsVisibleTo(&quot;Another.Assembly&quot;)]\n</code></pre>\n<p><strong>2. Adding the InternalsVisibleTo attribute in the project\'s .csproj (double click the project that you want its internals to be exposed)</strong></p>\n<pre><code>&lt;ItemGroup&gt;\n    &lt;AssemblyAttribute Include=&quot;System.Runtime.CompilerServices.InternalsVisibleTo&quot;&gt;\n        &lt;_Parameter1&gt;The_Assembly_That_Can_Access_Your_Internals&lt;/_Parameter1&gt;\n    &lt;/AssemblyAttribute&gt;\n    \n    &lt;AssemblyAttribute Include=&quot;System.Runtime.CompilerServices.InternalsVisibleTo&quot;&gt;\n        &lt;_Parameter1&gt;Another_Assembly_Or_Project&lt;/_Parameter1&gt;\n    &lt;/AssemblyAttribute&gt;\n&lt;/ItemGroup&gt;\n\n</code></pre>\n<p><strong>Note:</strong> <a href="https://youtu.be/noxNMji-DRw" rel="nofollow noreferrer">watch the whole youtube video for a more detailed explanation on Assembly-Level Attributes</a></p>\n'}, {'answer_id': 72820728, 'author': 'kewur', 'author_id': 4695519, 'author_profile': 'https://Stackoverflow.com/users/4695519', 'pm_score': 0, 'selected': False, 'text': '<p>in dotnet core we can add AssembyAttribute in the .csproj file</p>\n<p>just add this into your .csproj</p>\n<pre><code>&lt;!-- Make internals available for Unit Testing --&gt;\n&lt;ItemGroup&gt;\n    &lt;AssemblyAttribute Include=&quot;System.Runtime.CompilerServices.InternalsVisibleTo&quot;&gt;\n        &lt;_Parameter1&gt;Myproject.Tests&lt;/_Parameter1&gt;\n    &lt;/AssemblyAttribute&gt;\n&lt;/ItemGroup&gt;\n&lt;!-- End Unit test Internals --&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51950', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4694/']