Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Creating unit test classes as nested classes

In a comment to an NUnit post in Santomania, Phillip Carnasky recommends using nested classes for NUnit tests. It looks like an idea worth trying.  Here is his comment:

Title: re: Nunit
Name: Philip Canarsky
Posted Date: 3/7/2003 12:38 PM
I have been creating my unit test classes as nested classes. I like this for two reasons. First, it cuts down on the number of classes appear to be in the namespace. Second, a nested class has access to private instance variables of an instance of its containing class. This little trick can make writing unit tests that need to manipulate a class' internals(for whatever reason) much easier to write.

As far as making the unit test "disappear" from my compile when I am not testing, I normally wrap the unit test class within a #if DEBUG preprocessor conditional. This way, the unit tests are not included in my release code.

No Comments