Switching Between Using NUnit and MSTest for Unit Testing

If you don't want to change any test code (i.e. don't want to add the aliasing at the top), this shim works for me:

using System;
using System.Collections;

namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
    public class Placeholder{}
    public class TestClassAttribute : NUnit.Framework.TestFixtureAttribute
    {
    }
    public class TestInitializeAttribute : NUnit.Framework.SetUpAttribute
    {
    }
    public class TestMethodAttribute : NUnit.Framework.TestAttribute
    {
    }
    public class TestCleanupAttribute : NUnit.Framework.TearDownAttribute
    {
    }
    public class IgnoreAttribute : NUnit.Framework.IgnoreAttribute
    {
    }
    public class ExpectedExceptionAttribute : NUnit.Framework.ExpectedExceptionAttribute
    {
        public ExpectedExceptionAttribute(Type exceptionType) : this(exceptionType, null)
        {
        }
        public ExpectedExceptionAttribute(Type exceptionType, string message) : base(exceptionType)
        {
            UserMessage = message;
        }
    }
    public class TestContext : NUnit.Framework.TestContext
    {
        public TestContext(IDictionary dictionary) : base(dictionary)
        {
        }
    }
    public class Assert : NUnit.Framework.Assert
    {
        public static void IsInstanceOfType(object obj, Type type)
        {
            NUnit.Framework.Assert.IsInstanceOfType (type, obj, null);
        }
        public static void IsInstanceOfType(object obj, Type type, string message)
        {
            NUnit.Framework.Assert.IsInstanceOfType (type, obj, message);
        }
    }
    public class CollectionAssert : NUnit.Framework.CollectionAssert
    {
    }
}

This has worked for me to run MSTest via NUnit (at least under mono with Xamarin Studio). Just include the file and get references right (you may need a different project file or conditional references), and you're good.


The best solution I have found is to make use of a simple piece of code I found in this article. Simply use this code snippet in the namespace section of each .cs test file:

#if NUNIT
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

using NUnitAssert = NUnit.Framework.Assert;
using MsAssert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

The NUNIT in the code snippet refers to a custom build configuration for the solution. You can create this using the VS Configuration Manager (via the VS toolbar or the solution properties). Also, you need to replace all instances of the NUnit's Test attribute on your methods to make use of the MSTest TestMethod attribute (or vice versa).

EDIT: Updated the code snippet above to include a possible fix for the issue Jamie Ide pointed out in the comments. Note, I haven't managed to test this fix. The updated code snippet is taken from a comment by Simon on this blog post.