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
posts - 10 , comments - 27 , trackbacks - 0

How to write your unit tests to switch between NUnit and MSTest

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

Print | posted on Saturday, September 15, 2012 1:17 PM | Filed Under [ .Net 3.5 Visual Studio 2008 .Net 4.0 Visual Studio 2010 Unit Testing ]

Feedback

Gravatar

# re: How to write your unit tests to switch between NUnit and MSTest

Awesome trick
3/20/2013 10:56 AM | QT
Gravatar

# re: How to write your unit tests to switch between NUnit and MSTest

Good post.

Appreciate your hardwork

Thanks
Running Junit tests with Jmockit in eclipse
5/16/2013 12:47 AM | Junit - Java unit testing Exampl
Gravatar

# re: How to write your unit tests to switch between NUnit and MSTest

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 ???
6/2/2014 11:59 PM | RAJA M
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: