.NET Core Unit Testing Framework

The main contenders are Ms Test, NUnit and the newer kid on the block, xUnit.

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.

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 definitely 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.

Introduce Yourself

SENIOR SOFTWARE ENGINEER, with 7 years of experience in software design/development, including web development (client-side and server-side), database design/programming and service-oriented development. Proficient and certified in .NET and Java technologies. Excellent teamwork and passion for quality.
In free time love to explore new technology. My Interests are big data, data mining and machine learning.

If I am not working I usually spend time with family. I love watching a movie in all languages. I love adventure sports. I love playing Badminton.

Why do this?

  • This blog is basically about data structure , design discussion.

Design a site like this with WordPress.com
Get started