How to access classes in another assembly for unit-testing purposes?

Kevin Montrose picture Kevin Montrose · Jul 31, 2009 · Viewed 15k times · Source

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.

Answer

Arnold Zokas picture Arnold Zokas · Jul 31, 2009

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

Add

[assembly:InternalsVisibleTo("MyProjectTests")]

to AssemblyInfo.cs in your MyProject assembly.