Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching Between Using NUnit and MSTest for Unit Testing

Tags:

How can I configure a .NET solution (C#, .NET 2.0) to to allow other developers to make use of the same unit tests for the solution using either NUnit or MSTest?

Background:

In this project, some developers use VS2005 Team Edition, and others make use of VS2005 Pro, so not all of the developers are able to run MSTest. Given that this is an Enterprise project, the team is not able to make use of TestDriven.net or ReSharper. I am aware using either of these products with VS would resolve this issue, but given the time it would take to authorize the purchase of licenses, buying either of these products isn't a viable option.

Thanks in advance for your help, MagicAndi.

like image 254
Tangiest Avatar asked Apr 02 '09 11:04

Tangiest


People also ask

Which is better MSTest or NUnit?

The main difference is the ability of MsTest to execute in parallel at the method level. Also, the tight integration of MsTest with Visual Studio provides advantages in both speed and robustness when compared to NUnit. As a result, I recommend MsTest.

Why xUnit is preferred more than NUnit or MSTest?

XUnit vs. MSTest is concerned, the biggest difference between xUnit and the other two test frameworks (NUnit and MSTest) is that xUnit is much more extensible when compared to NUnit and MSTest. The [Fact] attribute is used instead of the [Test] attribute.

What is difference between NUnit and unit testing?

NUnit is an older, more established unit testing framework designed to do exactly one thing - unit testing. MSTest is newer so it does not have the same level of maturity in its API. For example, NUnit offers more Assert methods than MSTest.

What is the difference between NUnit and JUnit?

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. On the other hand, NUnit is detailed as "An open-source unit testing framework". An evolving, open source framework designed for writing and running tests in Microsoft .


1 Answers

The best solution I have found is to make use of a simple piece of code I found in this article. Simply use this code snippet in the namespace section of each .cs test file:

#if NUNIT using TestClass = NUnit.Framework.TestFixtureAttribute; using TestMethod = NUnit.Framework.TestAttribute; using TestCleanup = NUnit.Framework.TearDownAttribute; using TestInitialize = NUnit.Framework.SetUpAttribute; using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute; using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute; #else using Microsoft.VisualStudio.TestTools.UnitTesting; #endif  using NUnitAssert = NUnit.Framework.Assert; using MsAssert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert; 

The NUNIT in the code snippet refers to a custom build configuration for the solution. You can create this using the VS Configuration Manager (via the VS toolbar or the solution properties). Also, you need to replace all instances of the NUnit's Test attribute on your methods to make use of the MSTest TestMethod attribute (or vice versa).

EDIT: Updated the code snippet above to include a possible fix for the issue Jamie Ide pointed out in the comments. Note, I haven't managed to test this fix. The updated code snippet is taken from a comment by Simon on this blog post.

like image 97
Tangiest Avatar answered Oct 28 '22 20:10

Tangiest