xUnit / NUnit for C# (.NET): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is this library and why is it needed



xUnit and NUnit are the two most popular libraries for unit testing in the .NET ecosystem (C#, F#, VB.NET). They allow developers to write automated tests that verify the correctness of individual program modules (classes, methods, functions).



Why is this needed?

  • Early bug detection: Tests run after every code change, allowing you to immediately identify regressions (when new code breaks old functionality).
  • Fearless refactoring: You can confidently rewrite code if you are sure tests cover it. If tests pass, everything works as before.
  • Documentation in action: Tests show exactly how a particular method should be used, what parameters it accepts, and what it returns.
  • CI/CD integration: Tests automatically run on build servers (GitHub Actions, Azure DevOps, Jenkins) before deployment.


Difference between xUnit and NUnit:

  • NUnit — an older library (ported from Java JUnit), uses attributes [Test], [SetUp], [TearDown]. Has a rich set of attributes for parameterization.
  • xUnit — a more modern library, created by the author of NUnit. Uses the [Fact] attribute for regular tests and [Theory] for parameterized ones. It is distinguished by a stricter approach to test isolation (each test gets a new instance of the class).


In this article, we will look at both libraries, but the main focus will be on xUnit, as it is recommended by Microsoft for new projects.



Installation



Installation is done via the NuGet package manager. You can use the Package Manager Console or the .NET CLI.



Installing xUnit

// Via .NET CLIdotnet add package xunitdotnet add package xunit.runner.visualstudio

// Via Package Manager ConsoleInstall-Package xunitInstall-Package xunit.runner.visualstudio


Installing NUnit

// Via .NET CLIdotnet add package NUnitdotnet add package NUnit3TestAdapter

// Via Package Manager ConsoleInstall-Package NUnitInstall-Package NUnit3TestAdapter


Important: To run tests in Visual Studio or Rider, you also need to install a test runner adapter (e.g., xunit.runner.visualstudio or NUnit3TestAdapter).



Quick start with code example



Suppose we have a simple class Calculator with an Add method. Let's write tests for it in xUnit and NUnit.



Class under test (Calculator.cs)

public class Calculator{    public int Add(int a, int b)    {        return a + b;    }

public int Divide(int a, int b) { if (b == 0) throw new DivideByZeroException("Cannot divide by zero!"); return a / b; }}


xUnit test (CalculatorTests.cs)

using Xunit;

public class CalculatorTests{ [Fact] public void Add_ShouldReturnSum_WhenTwoPositiveNumbers() { // Arrange var calculator = new Calculator(); int a = 5; int b = 3;

// Act int result = calculator.Add(a, b);

// Assert Assert.Equal(8, result); }

[Fact] public void Divide_ShouldThrowException_WhenDivideByZero() { var calculator = new Calculator();

Assert.Throws<DivideByZeroException>(() => calculator.Divide(10, 0)); }}


NUnit test (CalculatorTest

Recommendations