25

I'm jumping into unit-testing the Visual-Studio 2008 way, and I'm wondering what's the best way to accomplish cross-assembly class access for testing purposes.

Basically, I have two projects in one solution:

  1. MyProject (C#)
  2. MyProjectTests (C# Test Project)

Everything in MyProject currently has default accessibility, which if I recall correctly means everything is effectively internal. I'm mostly looking to test at the class level, but there are a few delegates involved.

There will probably be an external API sometime in the future, but I'm about 20% of the way to feature complete (at least on paper) and I'm getting pretty leery of layering more code on top of this untested core. Accordingly I'd like to get some testing done now, before the app is complete enough for traditional (read: bad and/or lazy) functional testing and definitely before the version n+1 external API is up.

In addition to a straight answer, an example of the solution would be greatly appreciated.

Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
  • 1
    To forestall your next question -- why does the testing assembly have to be signed if the tested assembly is signed? -- here's my article on that subject: http://blogs.msdn.com/ericlippert/archive/2009/06/04/alas-smith-and-jones.aspx – Eric Lippert Jul 31 '09 at 15:27

6 Answers6

41

You can use assembly-level attribute InternalsVisibleToAttribute to achieve this.

Add

[assembly:InternalsVisibleTo("MyProjectTests")]

to AssemblyInfo.cs in your MyProject assembly.

Arnold Zokas
  • 8,306
  • 6
  • 50
  • 76
  • With appropriate strong-name key reference included, of course -- you are strong naming your assemblies, aren't you. – Steve Gilham Aug 02 '09 at 08:24
4

You need to add

[assembly:InternalsVisibleTo("Unit.Tests.Assembly")] 

to AssemblyInfo.cs of your "MyProject (C#)". That then allows your tests to access the internal methods for testing.

Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
3

You can test internal methods, by adding an attribute to the AssemblyInfo.cs for your main project, giving access to the internal methods to a named assembly:

[assembly:InternalsVisibleTo("MyProjectTestsNameSpace.MyProjectTests")]

Further info is here

AdaTheDev
  • 142,592
  • 28
  • 206
  • 200
1

Looks like you need the InternalsVisibleToAttribute

However I'd recommend against this approach - test your internal classes via the public interface or API.

Gishu
  • 134,492
  • 47
  • 225
  • 308
  • Problem is that those don't exist yet; and won't for awhile. Its kind of a large project (or, it will be when completed). – Kevin Montrose Jul 31 '09 at 10:06
  • The trouble with the inside out approach is that you may run into a situation where you have the internals done.. but the external API dont plug in as expected - because there was a disconnect in understanding. Feedback comes much later.. when it is more expensive to rectify. – Gishu Jul 31 '09 at 10:51
  • True, but putting off thorough testing until the application is "done" is just begging for trouble. Plus, the external API is going to probably take the form of automating user action and extracting data rather than extending functionality; its much harder to screw up compared to a full blown plugin API. – Kevin Montrose Jul 31 '09 at 10:53
  • 1
    I'm not advocating not testing till the end.. what i am saying is that develop thin vertical slices from the outside in. However you know better about your specific situation - if the risk is minimal it doesn't matter. – Gishu Jul 31 '09 at 11:29
1

Although [InternalsVisibleTo] is most sensible way IMO, there are at least 2 other ways to go about this:

  • By using Reflection

     var method = instance.GetType().GetMethod(
        methodName, BindingFlags.NonPublic | BindingFlags.Instance, 
        null, paramTypeArray, null);
     return method.Invoke(instance, parameters);
    

The problem with this approach is that if the method name or signature changes, the unit test will start failing at Run Time, whereas [InternalsVisibleTo] would have easily been picked up this breaking change at compile time.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
1

I found this one https://msdn.microsoft.com/en-us/library/hh598957.aspx Hope it might help someone.

Summary:

  • In your unit test project, add a reference to the code under test. Here’s how to create the reference to a code project in the same solution:
  • Select the project in Solution Explorer.
  • On the Project menu, choose Add Reference....
  • In the Reference Manager dialog box, open the Solution node and choose Projects.
  • Check the code project name and close the dialog box.
allo
  • 3,955
  • 8
  • 40
  • 71
Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32