allBlogsList

Comparing xUnit and NUnit for Unit Testing

While some developers don’t like unit testing and some even hate it, I think that most will agree that it’s a valuable discipline. In this blog, I give a brief overview and compare two commonly used unit-testing frameworks used for .NET, NUnit and xUnit.

According to nunit.org, NUnit is a unit-testing framework for all .NET languages, originally ported from JUnit (unit testing for Java) and version 3 completely been rewritten with many new features. The framework is open source under the MIT license and hosted on GitHub. At this moment, the NUnit repository has 1605 stars and 140 contributors.

Similarly, according to xunit.net, xUnit is open source and a community focused unit testing tool. Their site states that xUnit is the latest technology for unit testing C#, F#, VB.NET and other .NET languages. Licensed under Apache License Version 2, the GitHub repository has 2067 stars with 87 contributors.

If you are working with .NET Core solution in Visual Studio 2017, adding support for xUnit or NUnit is very easy since the project templates for both frameworks ship with Visual Studio. You can do this by right clicking your solution and selecting Add > New Project > Visual C# > .NET Core, then select either the xUnit Test Project (.NET Core) or NUnit Test Project (.NET Core) project template.

Visual Studio Add New Test Project

When working with .NET Framework solutions, you need to create a class library project, targeting .NET 4.5.2 (or later). You must install a couple of NuGet packages which will install core assemblies and the adapter needed for integration with Visual Studio Test Explorer. Examples shown below use NuGet Package Manager Console.

xUnit Packages

PM> Install-Package xunit
PM> Install-Package xunit.runner.visualstudio

NUnit Packages

PM> Install-Package NUnit
PM> Install-Package NUnit3TestAdapter
PM> Install-Package Microsoft.NET.Test.Sdk

Now let’s write a couple of basic tests. The Arrange, Act, Assert pattern is a common way of writing unit tests, which we will use here. When working with NUnit, we will decorate our unit test methods with [Test] attribute while xUnit uses [Fact] attribute.

xUnit Test Example

public class StringTests
{
    [Fact]
    public void Test1()
    {
        //Arrange
        string str1 = "Hello";
        string str2 = " ";
        string str3 = "World!";

        //Act 
        string result = string.Concat(str1, str2, str3);

        //Assert
        Assert.Equal("Hello World!", result);
    }
}

NUnit Test Example

public class StringTests
{
    [Test]
    public void Test1()
    {
        //Arrange
        string str1 = "Hello";
        string str2 = " ";
        string str3 = "World!";

        //Act 
        string result = string.Concat(str1, str2, str3);

        //Assert
        Assert.AreEqual("Hello World!", result);
    }
}


At this point our unit tests look similar. However, there is a difference as it relates to how each framework runs the tests. NUnit will run all the tests using the same class instance, while xUnit will create a new instance for each test. Also, with NUnit you will use [SetUp] and [TearDown] attributes to setup and teardown common logic for your tests, while xUnit uses a parameterless constructor and IDisposable interface to achieve the same results.

In conclusion, I would say that both frameworks are great unit testing tools, and I have used both in the past. However, I do favor xUnit for most greenfield projects.

View more blogs and tutorials about Sitecore

Learn about our Sitecore work