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

# 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 on programming issues.my post
Left by Andy March on Jan 27, 2017 2:26 PM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
We lately discovered your internet site and also have already been understanding together. I believed I might abandon my initial review. I don't know what to point out apart from I have appreciated understanding. Nice blog site. I'm going to keep going to this web site sometimes back pain blog
Left by Andy Hag on Feb 14, 2017 10:53 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
We these days found out your internet site and still have been recently learning alongside.footware blog
Left by Andy Hag on Feb 14, 2017 10:55 AM

# 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. "
Why don't you use nunit instead of mstest in your tests?
electric

Left by Andy Hag on Feb 17, 2017 6:36 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
NUnit and MSTest are very similar framework - the only difference is that mstest is integrated into the visual studio environment - wrote about it on
my blog.
Left by Andy Hag on Feb 19, 2017 7:19 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
It's also worth noticing that MSTest and their entire ALM strategy is targeted at many different styles of automated testing, including Integration Testing, System Testing, Performance Testing, etc., so while it attempts to be a one-size-fits-all, it's quite ill-suited for unit testing, because it's too heavy to work with
my website.
Left by Andy Hag on Feb 19, 2017 7:21 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
I have gone through your post and came to know about how to write unit tests to switch between Nunit and MStest. I have tried the sample source code which you have provided here and it executed successfully. Thank you for providing this. small server rack
Left by Amity on Mar 15, 2017 7:05 AM

# nice topic
Requesting Gravatar...
This post seems to be very useful to know more about the use of both NUnit and MsTest for unit testing while handling the project. I think it provides better method to create test unit and all. Really look forward to reading more.wholesale tablets
Left by edwise on Mar 17, 2017 6:49 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
This post seems to be very useful to know more about the use of both NUnit and MsTest for unit testing while handling the project. Thats an amazing feature - integrated into the visual studio.hoverboards
Left by MySite on Apr 05, 2017 9:46 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Can you specify what this line does?

using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;

Thanks Andy
inversion guide
Left by Andy Hag on May 05, 2017 3:01 AM

# re: How to write your unit tests to switch between NUnit and MSTest
Requesting Gravatar...
Thank you so much for such an excellent article!
inversion
Left by Andy Hag on May 10, 2017 2:22 PM

Your comment:
 (will show your gravatar)


Copyright © Justin Jones | Powered by: GeeksWithBlogs.net