Wednesday, April 12, 2006

Mix-and-match Unit Testing

In this MSDN Magazine article, John Robbins presents a tiny little hack that could save a huge amount of time if you're migrating a .Net 1.1 application with NUnit tests to .Net 2.0 and Visual Studio 2005.

The hack basically involves using some compile tests to switch between NUnit and VS2005 native testing.

The code he cites was actually found in the Patterns & Practices group Composite UI block source and is remarkably simple, requiring just the following code at the head of every test class and a change from using [Test]attributes to using [TestMethod]attributes instead.
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
#endif
Nice when you find a simple solution to an apparently intractable problem!

No comments: