views:

1135

answers:

2

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.

+4  A: 

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.

MagicAndi
In addition, there is at least one assert method, IsInstanceOfType, that is incompatible because the argument order is reversed.
Jamie Ide
A: 

Do you have a mix of existing tests? If not, or you don't mind converting existing MSTests, I would standardize on NUnit. I strongly prefer NUnit over MSTest; it's faster and it doesn't force you to have the TestContext nonsense in your test classes. It's also more compatible with CI servers.

Jamie Ide
Jamie, I would tend towards this myself, but the trend in the team is to standardize on MSTest. However, there are legacy tests in NUnit, and it will take some months to get the rest of the team to upgrade their DEV PCs to use VS Team System. This is basically a short to medium term solution. Thanks
MagicAndi
The `TestContext` thingy can safely be deleted.
mafutrct