Ms Test Vs NUnit Vs xUnit... Which Ones The Best? YOU DECIDE!

On any .NET project you should be writing unit tests.  I won't go into the benefits of unit testing here, but when you're new to unit testing, after understanding the importance of testing and deciding to get stuck in, the next thing you need to decide is, which test framework should you use?  The main contenders are Ms Test, NUnit and the newer kid on the block, xUnit.  In today's tutorial, I'm going to quickly cover all three and hopefully give you enough information to get going.

MSTest

MSTest has been around since Visual Studio 2015, at least. When it first came out, didn't have a way to pass parameters into your unit tests. For this reason, a lot of people opted to use NUnit instead. Since V2 MSTest also supports parameters, so the difference between the frameworks on a day-to-day basis has lessoned a lot. A very basic test class using MSTest will look like this:

[TestClass]
public class MyClass
{
  [TestInitialize()]
  public void Initialize() {}

  [TestCleanup()]
  public void Cleanup() {}
    
	[TestMethod]
	public void MyTest() {
	}
}

NUNit

NUNit has been around since 2006 and has probably been the testing-framework of choice for most teams ever since. Like most things, .NET, NUnit is installed via a NuGet package. It integrates with Resharper test suite well, so you can run your tests within Visual Studio.

An NUnit test class will look like this:

[TestFixture]
public class MyClass
{
  [Setup()]
  public void Setup() {}

	[Test]
	public void MyTest() {
	}
}

I guess syntax is down to personal preference, I prefer NUnits attribute naming conventions as I find it a little simpler and cleaner, but, in the grand scheme of life, it's a very tiny difference.  The main reason I picked NUnit was the ability to pass parameters into it, e.g.

[TestCase(1)]
[TestCase(2)]
public void MyTest(int testValue) {
}

xUnit

The makers of NUnit didn't like a few things about it so they went off and created xUnit. So xUnit has been built from the learnings of NUnit and it definitly changes the game in terms of its approach compared to its competitors. In xUnit you don't have a test set-up and tear-down like you would in MsTest and NUnit. In xUnit you also don't have one test attribute, instead, you have Facts and Theories! Obvs, as the newer kid XUnit is also installed via NuGet. So a 'Fact' is a test that doesn't take any input parameters and a 'Theory' is a test that does.

public class Tests
{
    [Fact]
    public void MyFact()
    {
        Assert.True(true); 
    }

    [Theory]
    [InlineData(false)]
    [InlineData(true)]
    public void MyTheory(bool testData)
    {
        Assert.True(boolean);
    }
}

 

Conclusion

All three frameworks will probably do 99% of the things that you need to test on a day-to-day basis. 3 or 4 years ago the lack of certain features in Ms Test made NUnit a better consideration. Today, that gap has narrowed so the choice between NUnit and MsTest is less. From my experience with unit testing, historically I've had unit testing maintenance nightmares with the set-up code. Packages like Autofxiture will help alleviate those issues, but choosing a framework that forces the team to write more modular and flexible code feels like a better choice nowadays. If I had to pick, I would go with xUnit, if that's not possible for whatever reason, then I use NUnit.

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top