NUnit and Microsoft’s MSTest

Published on
10,281 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

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Join & Write a Comment

Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month