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 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 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
    public void Test1()
        string str1 = "Hello";
        string str2 = " ";
        string str3 = "World!";

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

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

NUnit Test Example

public class StringTests
    public void Test1()
        string str1 = "Hello";
        string str2 = " ";
        string str3 = "World!";

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

        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





Sitecore 144
Commerce 100
Web Development 100
Sitecore Commerce 83
Sitecore Experience Commerce 9 64
Sitecore Experience Commerce 59
Content Management 55
eCommerce 48
B2B eCommerce 42
Sitecore Experience Platform 39
Sitecore Platinum Partner 39
Architecture 34
Insite 29
User Experience 26
Strategy 22
B2C eCommerce 21
B2B Commerce Blogs 21
commerceconnect 21
CloudCraze 20
SaaS (Software as a Service) 20
Cloud 17
Commerce Server 17
Salesforce B2B Commerce Cloud 16
Mobile 13
Search 13
Plugins 12
Analytics 12
Application Development 12
Digital Transformation 11
Sitecore Symposium 11
Helix 10
Social 8
Business Process 7
Authentication 7
5 Reasons 7
BI and Big Data 6
Data Visualization 6
Sitecore Presentation 6
Coveo 6
NET Development 5
Microsoft Azure 5
Automation 5
Front-End Development 4
SaaS (Software as a Service) 4
Digital Strategy 4
Brightcove 4
Press Release 4
Avalara Tax 3
Sitecore Experience Accelerator (SXA) 3
Sitecore Layouts 3
Video 3
Multi-site 3
Multi-lingual 3
Accessibility 3
Habitat 3
Vault 3
Identity 2
Managed Services 2
Cryptocurrency 2
Sitecore Forms 2
Sitecore Experience Commerce 9 Promotions 2
Uncategorized 2
Conversational Commerce 2
Sitecore SaaS 2
Security 2
Unit Testing 2
Headless Architecture 2
Sitecore Experience Awards 2
Google 1
Content Delivery Network 1
Configure Price Quote 1
Blockchain 1
Coupons 1
Sitecore Rss 1
Artificial Intelligence 1
Machine Learning 1
Okta 1
RFP Process 1
Flex Accelerator for Sitecore 1
Reviews 1
Page Labels 1