Converting MSTest Assemblies to NUnit

If you wanted to convert existing test assemblies for a Visual Studio solution from using MSTest to NUnit, how would you do it?  This post will provide one answer to that question.

I started by changing the type of the test assembly.  To do this, I opened the .proj file with a text editor, then used this link to find the MSTest GUID in the file and remove it (the guid will be inside a ProjectTypeGuids XML tag).  This should ensure that Visual Studio and/or any third-party test runners can identify it correctly.  Once I saved that change, the remaining steps were:

  • replace references to Microsoft.VisualStudio.QualityTools.UnitTestFramework with nunit.framework
  • change unit test attributes from MSTest to NUnit (you may find a side-by-side comparison helpful)
  • delete any code specific to MSTest (this includes TestContext, DeploymentItem, AssemblyInitialize, AssemblyCleanup, etc)

After the above steps, NUnit ran the tests without any further modifications.  All of my calls to Assert worked the same way in NUnit that they did in MSTest.

Comments

  1. Will Hopkins says:

    Hi,

    As a newbie to unit testing with either MSTest or NUnit, my approach was to create unit tests that would run in either environment and basically run NUnit tests within an MSTest “environment” by using the following code snippet;

    #region MSTest/NUnit Compatability

    // Define MSTEST as a conditional compilation symbol in your DEBUG build then you can do testing with your DEBUG test builds
    // directly in Visual Studio. This also means you can debug your tests.
    // Note, if you use any MSTest specific functionality that is NOT available in NUnit, it WILL NOT WORK in NUnit!
    // Such functionality includes any direct use of TestContext, any web service related attributes and other stuff I haven’t yet dug up but know exists!
    // Any NUnit functionality WILL work in MSTest since the NUnit Assert is THE Assert class and the NUnit.Framework assembly
    // is referenced and used. Any test assemblies that have been built with MSTEST specified will NOT work in NUnit, therefore
    // it is suggested that from time to time you create a Release build of your tests. If you have used any MSTest only functionality
    // then your build (and consequently the build of your test on the build server) will FAIL. Check it!
    // Note, ensure your test is in DEBUG build before using VS to create unit tests or it will add MSTest usings into your Release build!

    #if !MSTEST
    #if DEBUG
    #error ******* DEBUG builds should define MSTEST as a Conditional Compilation Symbol in Project Properties->Build Tab ************
    #else
    #warning ******* Test is compiled for NUnit ************
    #endif
    using NUnit.Framework;
    using NUnit.Framework.Constraints;

    // The following using declarations allow the Visual Studio autogenerated framework to actually use NUnit attributes.
    // This means that where in the test code below we see the VS generated [TestClass()] attribute, it is “really” [TestFixture],
    // i.e. NUnit style with MSTest syntax – which gets autogenerated and is thus easier to “write” as VS does it for you. 🙂

    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;
    using TestContext = System.Object;
    #else
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
    using Category = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
    #endif

    // Since NUnit’s Assert is a superset of MSTest’s Assert, we’ll treat Assert as the NUnit version and get the extra goodies.
    using Assert = NUnit.Framework.Assert;

    #endregion

Leave a Reply to Scott Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.