Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
InternalsVisibleTo: Testing internal methods in .Net 2.0

Ok, so maybe I am the last person on the planet to learn about this new attribute, but oh well. 

Last week a co-worker of mine came across this attribute when doing some research and both of us just kinda looked at each other and smiled.  See, for a long while now we have been saying how it would be nice to be able to test non-public methods from external assemblies without the use reflection.  It appears that Microsot thought that this was important enough to add to the framework (thanks guys) in the form of a simple attribute [InternalsVisibleTo] (MSDN Documentation).  Sorry VB.Net developers, Microsoft is again shunning you guys and this attribute is NOT available to you.... :(

In order to use this attribute you need to add one line of code to your AssemblyInfo.cs class for each external assembly that needs to access internal methods.


Now, to create a Simple application to test.  For this example I will create a simple ShoppingCart that takes a Shopping Cart item.

Here is the test that allows the testing of the internal method.

Now, you can test those internal methods from external assemblies.  Like I said above, maybe I was the last to find this, but I am glad I finally did.

Derik 


Posted 04-09-2007 6:33 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Christopher Bennage wrote re: InternalsVisibleTo: Testing internal methods in .Net 2.0
on 04-09-2007 10:28 AM

Nice. I'll confess that I didn't know about the attribute, so maybe I'm the last developer on earth to find out...

Though, there's a lot of arguments going around about marking things internal to begin with.

Derik Whittaker wrote re: InternalsVisibleTo: Testing internal methods in .Net 2.0
on 04-09-2007 10:43 AM

Christopher,

You are correct, there is a lot of arguments about white box vs. black box testing.  It is for that reason I did not even mention that in the post.  

Personally, all i care about is having the tests.  

Derik

Ravi Terala wrote re: InternalsVisibleTo: Testing internal methods in .Net 2.0
on 04-09-2007 6:02 PM

Don't use InternalsVisibleTo for testing purposes. Use a wrapper class generated by VS Test Tools. It is a better model and prevents unnecessary exports to test assemblies.

Derik Whittaker wrote re: InternalsVisibleTo: Testing internal methods in .Net 2.0
on 04-09-2007 7:28 PM

Ravi,

Unless they have changed something, the last time i used the wrapper classes i was not very happy.  If i remember correctly I had issues with the wrappers when ever the object that was being wrapped was changed.

Derik

jonkas wrote re: InternalsVisibleTo: Testing internal methods in .Net 2.0
on 04-19-2007 11:54 AM

i just ran some quick tests and things work great if the method is marked 'internal' ... how does one go about testing the 'private' and 'protected' access modifiers?

Tor wrote re: InternalsVisibleTo: Testing internal methods in .Net 2.0
on 04-26-2007 8:37 AM

My personal preference is to use this InternalVisibleTo attribute to promote testing rather than the wrapper class generated by VS Test Tools.

I find the concept of wrapper class generation a bit strange and sometimes also problematic. It doesn't work with static classes either

Derik Whittaker wrote Whitebox vs Blackbox testing (location of unit tests)
on 01-28-2008 6:38 AM

Over on the Alt.Net mailing list ( here ) the topic of where to put your unit tests was the hot topic

Direttive al preprocessore | hilpers wrote Direttive al preprocessore | hilpers
on 01-18-2009 8:21 AM

Pingback from  Direttive al preprocessore | hilpers

The Silence Of The LaMs wrote Testowanie jednostkowe metody prywatnej
on 03-14-2010 8:14 PM

Testowanie jednostkowe metody prywatnej

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)