For C# developers starting out with testing, xUnit offers a modern, flexible, and widely adopted framework that makes it easy to get going. This beginner-friendly guide will walk you through the basics of writing and running unit tests using xUnit in a C# application.
XUnit Testing in C#: A Bigner's Guide
Getting Started
xUnit also known as xUnit.net is a modern, free, and open-source unit testing framework for .NET applications. It belongs to the broader family of 'xUnit' testing frameworks, which began with JUnit for Java and follows a similar testing philosophy.
As software applications increase in complexity, the need for reliable, long-term functionality becomes more critical. Unit testing helps address this challenge by ensuring code behaves as expected now and in the future. For C# developers new to testing, xUnit.net is a powerful, modern, and widely-used framework that provides an excellent starting point.
xUnit embraces modern programming practices and integrates smoothly with the .NET SDK and development tools like Visual Studio and JetBrains Rider.
Basic Steps to Write an xUnit Test
- Create a Solution and Projects
- Install xUnit NuGet packages
- Write test methods using
[Fact]or[Theory] - Use assertions to verify behavior
Create a Solution and Projects
- Open Visual Studio from your Start menu or desktop.
- On the start screen, click on “Create a new project”.
- Choose a Project Template
- Choose “Empty Project”
- Click Next
- Configure Project
- Project name: Enter a name for your project (e.g., CalculatorApp).
- Location: Choose the folder where the project will be saved.
- Solution: Choose to place the project in a new or existing solution.
- Click Next.
- Choose the target framework
- Click Create.
- Right click on the Solution-> Add-> New Project...
- Choose a Project Template(Class Library)
- Click Create.
CalculatorApp/
├── src/
│ └── CalculatorApp/
│ └── Calculator.cs
└── tests/
└── CalculatorApp.Tests/
└── CalculatorTests.cs
Install xUnit
Install the necessary packages using NuGet or the NuGet Package Manager in Visual Studio: dotnet add package xunit
dotnet add package xunit.runner.visualstudio
dotnet add package Microsoft.NET.Test.Sdk
Write the Code to Test
In theCalculatorApp project, create a simple calculator class:
// File: Calculator.cs
namespace CalculatorApp
{
public class Calculator
{
public int Add(int a, int b) => a + b;
public int Subtract(int a, int b) => a - b;
public int Multiply(int a, int b) => a * b;
public int Divide(int a, int b)
{
if (b == 0) throw new DivideByZeroException("Cannot divide by zero.");
return a / b;
}
}
}
Writing Tests with xUnit
In the CalculatorApp.Tests project, add the following test class: // File: CalculatorTests.cs
using CalculatorApp;
using Xunit;
namespace CalculatorApp.Tests
{
public class CalculatorTests
{
private readonly Calculator _calculator;
public CalculatorTests()
{
_calculator = new Calculator();
}
[Fact]
public void Add_ReturnsCorrectSum()
{
var result = _calculator.Add(2, 3);
Assert.Equal(5, result);
}
[Fact]
public void Subtract_ReturnsCorrectDifference()
{
var result = _calculator.Subtract(5, 3);
Assert.Equal(2, result);
}
[Fact]
public void Multiply_ReturnsCorrectProduct()
{
var result = _calculator.Multiply(4, 5);
Assert.Equal(20, result);
}
[Fact]
public void Divide_ReturnsCorrectQuotient()
{
var result = _calculator.Divide(10, 2);
Assert.Equal(5, result);
}
[Fact]
public void Divide_ByZero_ThrowsException()
{
Assert.Throws<DivideByZeroException>(() => _calculator.Divide(10, 0));
}
[Theory]
[InlineData(2, 3, 5)]
[InlineData(-1, 1, 0)]
public void Add_ReturnsExpectedResult(int a, int b, int expected)
{
var result = _calculator.Add(a, b);
Assert.Equal(expected, result);
}
}
}
Summary
Unit testing is an essential skill for any developer. With xUnit and C#, writing tests becomes clean, readable, and integrated with your development workflow. By starting with small, meaningful tests, you can build the foundation for robust, maintainable applications.
Thanks