Tuesday, January 10, 2012

Converting NUnit to MSTest

At my normal job, I was recently put in charge of converting our NUnit tests to MSTest.  After much researching online and reading lots of posts, here is the method that seemed to work for me.
  1. Remove dll references to NUnit.Core & NUnit.Framework.
  2. Add a reference to Microsoft.VisualStudio.TestTools.UnitTesting.
  3. In the code, find and replace:
    1. using NUnit.Framework; → using Microsoft.VisualStudio.TestTools.UnitTesting;
    2. [TestFixture] → [TestClass]
    3. [Test] → [TestMethod]
    4. [SetUp] → [TestInitialize]
    5. [TearDown] → [TestCleanup]
    6. [TestFixtureSetUp] → [ClassInitialize]
    7. [TestFixtureTearDown] → [ClassCleanup]
    8. (TestFixtureAttribute) → (TestClassAttribute)
    9. (TestFixtureSetUpAttribute) → (TestInitializeAttribute)
    10. (TestFixtureTearDownAttribute) → (TestCleanupAttribute)
    11. (TestAttribute) → (TestMethodAttribute)
  4. Update all of the Assert calls.
  5. The 'hidden' part. In your project file, locate <PropertyGroup> (not the one specifying debug|release settings), and add the following to it under <ProjectGuid>…</ProjectGuid>:
    1. <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    2. Do not change the GUID.  Visual Studio uses that exact GUID set to identify all Unit Test projects.
  6. In Visual Studio, go to the "Test View" and confirm that your tests are listed.
  7. If needed, change setup()→ public static void setup(TestContext context)
  8. If needed, change teardown()→ public static void teardown()
I'm posting this here for historical purposes.  UnitTests can come in handy for all kinds of development and Visual Studio's built in testing makes it extremely easy to do.  I know I'm guilty of not having many unit tests for my game, but as code becomes more and more mature, unit testing can make sure that we're not doing something we shouldn't.  It's also a good way to make sure the build you're about to release to your players isn't going to destroy something vital, like Saved Games or existing map data.

No comments: