Writing Tests that work in NUnit and MSTest

As someone who is trying to jump into the unit testing world, one of the major problems I had was deciding on using NUnit (which everyone uses) or use MSTest(built in).

I didn’t want to jump into this and realize 500 hours later that I picked the wrong framework.  Well, now it seems I don’t have to worry about that as much. 

First I was reading this article by Roy Osherove, and somehow I ended up on this page talking about converting between NUnit and VSTS projects, and from there I found a link to this page, which contained this great piece of code:

#If NUNIT Then
Imports NUnit.Framework
Imports TestClass = NUnit.Framework.TestFixtureAttribute
Imports TestMethod = NUnit.Framework.TestAttribute
Imports TestInitialize = NUnit.Framework.SetUpAttribute
Imports TestCleanup = NUnit.Framework.TearDownAttribute
#Else
Imports Microsoft.VisualStudio.TestTools.UnitTesting
#End If

This allows you do have the came code for NUnit and MSTest.  Just change the NUNIT variable and recompile.

The other thing you need to do is use <TestAttribute()> instead of <Test()> on your NUnit tests, but I don’t see the difference there.

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s