Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  153 Posts | 0 Stories | 2415 Comments | 653 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

One of the annoying things about unit testing, is that not all members of the target code class are accessibly to the test fixtures. 

There are limited options to work around this (as you really don’t want to put the unit tests within the code assembly).

 

In the past, the usual method has been to open up the access levels of the code class members.  This isn’t ideal from a best practices and security point of view.

 

However, as of .Net 2.0 a better method has been introduced.  This is to use the InternalsVisibleTo attribute on the code assembly.  This makes any code members with an accessibility level of internal or above (internal, protected internal and public) available to the specified assemblies.

 

To use this attribute add the following line to the AssemblyInfo.cs file of the code assembly:

[assembly: InternalsVisibleTo("MyAssembly.UnitTests")]

where MyAssembly.UnitTests is the unit test assembly name.

Tim

posted on Thursday, August 30, 2007 4:39 AM

Feedback

# re: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute 6/16/2008 11:02 AM Chuck Sphar
I agree that InternalsVisibleTo is valuable, especially when you're using Test-Driven Development. I'd just note that many unit testers have the philosophy that you should only test public methods on public classes. That scenario works just fine in both NUnit and MSTest. Presumably, as you write public methods and refactor them based on tests, you'll develop the less-than-public methods by letting them fall out of the refactoring.

As for internal classes, many of those may fall out of refactoring as well--as you move methods onto extracted classes that you want to be internal. (I'm sure they don't always arise in that way, especially if you're not doing TDD, hence the value of testing internals.

BTW, I just learned from Roy Osherove that NUnit supports the InternalsVisibleTo attribute along with MSTest. I've been reading and commenting on his new book in progress, "The Art of Unit Testing," available for download on the Manning Publishing site. Only 7 chapters so far, but the content is good.

# re: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute 11/19/2011 2:38 AM Cheapbagshopping.com
Thank you very much.Waiting for update.Cheap women High heel shoes from China for wholesale on line free shipping

# re: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute 11/19/2011 3:39 AM Cheapbagshopping.com
It is nice of you to post this.I will pay more attention on it.wholesale cheap replica women Prada High heel shoes on line accept paypal

# re: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute 12/18/2011 1:11 AM Cheap Flights To Vietnam
Very interesting article. Content has been written in very nice manner. I enjoy reading this kind of stuff. Thanks for sharing good knowledge.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: