[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


NUnit and Microsoft’s MSTest

Published on
10,329 Points
1 Endorsement
Last Modified:
Many of us are using NUnit and want to use Microsoft’s MSTest. I am writing a little about the difference between NUnit and MSTest and what changes you need to make to migrate from NUnit to MSTest. I hope this article will help you understanding the difference and assist with migrating.

NUnit is an open source unit testing framework for Microsoft .NET; basically, you can achieve the same goal as with MSTest. The main difference for me; MS provides unit testing toosl only in the team edition of VS. NUnit is free.

Yes, Team Edition unit testing tool is a bit more capable. For example, you may easily generate (stabs for) tests. It is easy to check test coverage etc. You can't do that with standard GUI for NUnit. If you want to convert or user your project from NUnit to MSTest then you need to change the following.

Replace the reference to the Nunit.Framework assembly with a reference to the Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly.
Replace the “using Nunit.Framework” line with “using Microsoft.VisualStudio.TestTools.UnitTesting”
For each file containing unit tests, replace the following attributes (you can use global search and replace for this):
NUnit Attribute for MSTest Attribute
TestFixture for TestClass
Test for TestMethod
SetUp for TestInitialize
TearDown for TestCleanup
TestFixtureSetup for ClassInitialize
TestFixtureTearDown for ClassCleanup

Replace the following method calls:
NUnit Method Code for MSTest Method Call
Assert.Ignore for Assert.Inconclusive

Unfortunately, the Assert (and related classes) used by MSTest are not as complete as the ones offered by NUnit so you may also end up changing some of your tests. The following Asserts are not available in MSTest:

    * Assert.IsNaN
    * Assert.IsEmpty
    * Assert.IsNotEmpty
    * Assert.Greater
    * Assert.GreaterOrEqual
    * Assert.Less
    * Assert.LessOrEqual
    * Assert.IsAssignableFrom
    * Assert.IsNotAssignableFrom
    * CollectionAssert.IsEmpty
    * CollectionAssert.IsNotEmpty
    * StringAssert.AreEqualIgnoringCase
    * StringAssert.IsMatch
    * FileAssert.AreEqual
    * FileAssert.AreNotEqual

If you want to use NUnit then you can use couple of third party plug-in to use NUnit from Visual studio. Also If you are using ReSharper then you can run the NUnit Unit test cases directly from the Visual Studio IDE.

I have used both for the basic stuff I liked both but as I was use to with NUnit I would still prefer to use NUnit. And Microsoft has added MStest with VS its really good that you can just run the Unit test  cases by right click on class or methods. But the similar functionality you can achieve if you have ReShaper. As I was using ReSharper, I found using NUnit for my current project.

1 Comment

Author Comment

Thanks.. This is my first article.... not only in EE ... its first in My life... :)...

I will make the changes... as you have suggested ...

Featured Post

The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Join & Write a Comment

XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month