Tuesday, June 3, 2008

MsTest and NUnit; Using Both

If you can use MsTest; you should. The integration benefits will pay dividends over and over.

But if it is not available across your company, you need not settle for the least common denominator of NUnit. And you may want to develop with MSTest but deploy with NUnit to keep the footprint light.

Fortunately there is an almost one to one mapping between the namespaces of the two.

So using the following conditional code at the top of your test class file, will enable you to alternate which testing harness you use.

==============
Code Start

==============
#if MSTEST
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;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
#endif
==Code End==
Some things like Categories have no direct equivalent so you will need to conditionally compile their attributes:
==Code Start==
[TestClass ]
#if MSTEST
//Intentionally empty
#else
[Category("Web Service")]
#endif
public class QA_Feature_2_1
==CodeEnd==
And another gotcha occurs around the test and suite set up and tear down functions:
==Code Start==
[ClassInitialize()]
#if MSTEST
public static void MyClassInitialize(TestContext testContext)
#else
public void MyClassInitialize()
#endif

=========================

No comments: