Geeks With Blogs

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

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 C# .NET , ASP.NET , Unit Testing | Back to top


Comments on this post: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute

# re: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute
Requesting Gravatar...
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.
Left by Chuck Sphar on Jun 16, 2008 11:02 AM

# re: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute
Requesting Gravatar...
Left by Cheapbagshopping.com on Nov 19, 2011 2:38 AM

# re: Unit testing with NUnit and member accessibility - and the InternalsVisibleTo attribute
Requesting Gravatar...
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
Left by Cheapbagshopping.com on Nov 19, 2011 3:39 AM

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

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net