16

Reading about MSTest and NUnit I couldn't really decide what to use in my project. We use TFS 2008 and VS2010.

I like MSTest because of its integration into VS2010, Continuous Integration and Code Coverage reports. I like NUnit because it allows to formulate complex assert statements in a nice, readable fashion.

Stumbling upon http://alsagile.com/archive/2010/03/09/stop-the-war-between-nunit-and-mstest-make-them.aspx I ask the community: is it possible to use both?

I also think about sticking to MSTest and use Fluent Assertions to give me a more flexible way in formulating assert statements. Wouldn't this be the best option after all?

ZAD-Man
  • 1,328
  • 23
  • 42
mcanti
  • 1,964
  • 2
  • 17
  • 14

3 Answers3

24

I personally don't like the idea of mixing two frameworks like it's mentioned in the article you are referring to.

A possible condition to make your unit test run under both test frameworks could be that you do not want or can not install Visual Studio on your continuous integration server.

Just for clarification, MSTest is not the Visual Studio Unit Testing Framework defined in the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assembly.

To be able to let your unit test run under both frameworks you would need to define a build constant (here NUNIT) and with the help of preprocessor directives and namespace aliases you would end up with a snippet like this:

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Category = Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute;
#else
using NUnit.Framework;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestContext = System.Object;
using TestProperty = NUnit.Framework.PropertyAttribute;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
#endif

I believe I came across this idea first through this MSDN Magazine article. Another worthwhile blog post covering this topic would be:

To your last question: The Fluent Assertions project seems to be a good library. I do not see any real reasons why you shouldn't use it if you like the fluent style of it.

Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
2

Add a reference to nunit.framework and the following line to the top of your MSTest tester class...

using NAssert = NUnit.Framework.Assert;

Now you can use either...

// Test whether a new SimplexInterpreter was created
[TestMethod]
public void SimplexInterpreterConstructorTest()
{
    Assert.IsNotNull(target);
    NAssert.IsNotNull(target);
}
Riegardt Steyn
  • 5,431
  • 2
  • 34
  • 49
2

You can certainly have one test project using MSTest and another using NUnit. If you want to mix both in the same project, you'll have to stick to common features and use compiler directives and using statements to "rename" and compile to certain ones.

You'd have to run both nunit and mstest though to figure out if all your tests really pass, so I'd strongly advise against trying to use both. Pick one.

If you want to run tests on a build server, probably the best bet is NUnit, unless you want to install VS on your build server as well. TFS may work without installing VS though, but you'll have to check your documentation.

Andy
  • 8,432
  • 6
  • 38
  • 76
  • I was referring to use MSTest and NUnit in the same project: MSTest as test runner with its IDE integration and NUnit for its asserts (just as @serbrech stated in his blog). Indeed the build server is a problem when choosing a testing framework. Afaik, using TFS as the build server should not have problems with running the unit tests and generating nice reports. – mcanti Feb 19 '11 at 22:24
  • So I guess I'm not sure what you're trying to ask. Personally I wouldn't try to use bits of both frameworks at once; seems like it would be more of a maintence issue that it's worth, just because some method names on an Assert class is different. – Andy Feb 20 '11 at 20:17