Geeks With Blogs
Justin a.k.a. The Code Monkey Code Monkey [kohd muhng'-kee] n : 1. Creature known for it's ability to transform caffeine into code. 2. Justin Jones

On my current project I found it useful to use both NUnit and MsTest for unit testing. When using ReSharper for running unit tests, it just simply works better with NUnit, and on large scale projects NUnit tends to run faster. We would have just simply used NUnit for everything, but MSTest gave us a few bonuses out of the box that were hard to pass up. Namely code coverage (without having to shell out thousands of extra dollars for the privilege) and integrated tests into the build process. I’m one of those guys who wants the build to fail if the unit tests don’t pass. If they don’t pass, there’s no point in sending that build on to QA.

So making the build work with MsTest is easiest if you just create a unit test project in your solution. This adds the right references and project type Guids in the project file so that everything just automagically just works. Then (using NuGet of course) you add in NUnit. At the top of your test file, remove the using statements that refer to MsTest and replace it with the following:

#if NUNIT
using NUnit.Framework;
#else
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
using TestFixtureSetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute;
using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

Basically I’m taking the NUnit naming conventions, and redirecting them to MsTest. You can go the other way, of course. I only chose this direction because I had already written the tests as NUnit tests. NUnit and MsTest provide largely the same functionality with slightly differing class names. There’s few actual differences between then, and I have not run into them on this project so far.

To run the tests as NUnit tests, simply open up the project properties tab and add the compiler directive NUNIT. Remove it, and you’re back in MsTest land.

image

Posted on Saturday, September 15, 2012 1:17 PM .Net 3.5 , Visual Studio 2008 , .Net 4.0 , Visual Studio 2010 , Unit Testing | Back to top


Comments on this post: How to write your unit tests to switch between NUnit and MSTest

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Awesome trick
Left by QT on Mar 20, 2013 10:56 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Good post.

Appreciate your hardwork

Thanks
Running Junit tests with Jmockit in eclipse
Left by Junit - Java unit testing Exampl on May 16, 2013 12:47 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Hi.. Very useful technique.. Here how do we manage assert calls ?In Mstest , we have Assert.AreEqual<T> where as nuint it has overloads not generic. in this case we need to write
#if NUNIT
assert.areEqual()
#else
assert.AreEqual<T> ...... for every assert operation ???
Left by RAJA M on Jun 02, 2014 11:59 PM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Their tools aren’t working. I am also commenting to make you know of the hpelful discovery my wife’s princess obtained reading your web site. Khelomcx.com
Left by Asfaq Ramzani on Aug 20, 2015 7:10 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
The tips and tricks that are shared here on the page to write the unit test to switch between n units proved to be an interesting one to me. I am s computer student and this site has helped me increase my knowledge on the subject. jewish school los angeles
Left by Mania on Dec 29, 2015 9:51 PM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
I found your this post while searching for some related information on blog search...Its a good post..keep posting and update the information.
http://bestdownloadablefreeware.ru
Left by Toom on Mar 31, 2016 2:36 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
I've proper selected to build a blog, which I hold been deficient to do for a during. Acknowledges for this inform, it's really serviceable!
downloadmyfileshere.com
Left by Astronavt on Mar 31, 2016 11:46 PM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
So making the build work with MsTest is easiest if you just create a unit test project in your solution. This adds the right references and project type Guids in the project file so that everything just automagically just works.
hemorrhoids
Left by Dan begg on Aug 08, 2016 1:18 PM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Can you specigy more about the difference between nunit and mstest (they seem quite similair)...I know mstest advantage is that it is incorporated into visual studio
piles health
Left by Andy Heig on Aug 13, 2016 2:46 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Prolapsed piles come about if the internal hemorrhoid is forced out from the rectum. Strangulated hemroids come about in the event the prolapsed piles tend to be captured outside of the rectum that can take off your circulation.
Left by Andy Heig on Sep 06, 2016 2:03 PM

Your comment:
 (will show your gravatar)


Copyright © Justin Jones | Powered by: GeeksWithBlogs.net | Join free