.NN #18: Another use for Using

Sunday, June 01 2008

If you are a C# developer you're familiar with the using directive.  It appears at the top of every one of your class files informing the compiler what namespaces should be looked at while compiling this code page so that you don't have to fully qualify every object. (This isn't the interesting bit)

using System;
using System.Collections.Generic;

But did you also know you can use it to make aliases? (This still isn't the interesting bit, keep reading...)

using Foo = System.Diagnostics;

This would allow code to say Foo.Trace or Foo.Debug.  Aliases are used most often to deal with namespace collisions where two namespaces contain an object with the same name.  You can provide an alias to one of the namespaces and then use the alias with the object declaration.  Note that you can alias not just a namespace, but a type as well.

The interesting bit is what I ran across in the Enterprise Library code.  They were using aliases along with conditional pre-compiler directives to be able to use either NUnit or MSTest in their unit tests without having the change code.  Here's an example:

using System.Configuration;
using SysConfig = System.Configuration;
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif
 
namespace LongNameRemovedForSpace.Logging.Tests
{
      [TestClass]
      public class LoggerFixture
      {
            
            [TestInitialize]
            public void SetUp()
            {
                  Logger.Reset();
                  MockTraceListener.Reset();
                .....

Notice how they have a #if pre-compiler directive set up that either sets a using for the MSTest Unit testing namespace or, if the !NUNIT is set to false, then it pulls in a series of namespaces aliasing the attribute names for NUnit to the corresponding attribute name for the MSTest framework.  This means that the code file will compile using one set of attribute names for their unit tests, but via a compiler constant you can switch to run those unit tests under NUnit or MSTest.  That's pretty slick.

I posted this as a .NET Nugget because I think it is a cool use of the using directive.   When they put this together they had to solve the problem of allowing for people to run unit tests under both NUnit and MSTest.  Rather than having duplicate code files everywhere or tons of #if - #else pre-compiler directives around all the attributes they found a simple way to solve the problem.

Comments

Wednesday, June 18 2008 3:02 PM Tom said:

Hi,

Nice little trick. Would you be able to do the same in reverse to have the resharper integration also available when testing?

I've tried reversing the attributes, but the Resharper unit test icon's dissapear as soon as I reference the MS TestTools in the usings.

using SysConfig = System.Configuration;

#if NUNIT

using NUnit.Framework;

#else

using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;

using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute;

using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;

using TearDown = Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute;

#endif


Wednesday, June 18 2008 10:32 PM Michael Wood said:

Tom: I'm not sure how the ReSharper testing tool icons work. I'm a CodeRush/Refactor! guy myself. It sounds like Resharper has an aversion to MSTest period. You might post a question on the JetBrains forums to see if someone there has an idea. It wouldn't suprise me if ReSharper was just parsing the using statements and not taking into account the compiler conditionals.