C#
What is xUnit?
Learn what the xUnit test framework is and how to use it.
What is xUnit?
xUnit is the most popular test framework for .NET. A test method is marked with the [Fact] attribute; its body contains assertion calls like Assert.Equal(...). A test runner discovers and runs these methods.
A unit test checks that a small piece (say, a method) produces the expected result. It locks behavior without touching the code and gives confidence during refactoring. Sibling frameworks are NUnit and MSTest; all share the same mindset.
Test method and assertion
A test method is typically a parameterless void method. The [Fact] attribute on top tells the runner "run me". Inside the body, an Assert.Equal(expected, actual) pair compares expected and actual; if they differ, the framework throws and the test fails.
Naming conventions like TestAdd or ShouldReturnFive keep tests readable. A readable test reveals the failure reason quickly.
Exercise Write your first unit test
Define a FactAttribute class (deriving from Attribute) and a static Assert class; Assert.Equal<T>(T expected, T actual) throws Exception when values differ. Add a Calculator.Add(int, int) method that sums the two ints. Mark CalculatorTests.TestAdd() with [Fact] and call Assert.Equal(5, Calculator.Add(2, 3)). In Main, run the test and print "Test passed: Add(2,3)=5" on success.
Show example solution
Attribute { }. Define static class Assert { public static void Equal<T>(T expected, T actual) { if (!EqualityComparer<T>.Default.Equals(expected, actual)) throw new Exception($"Expected {expected}, got {actual}"); } }. Write class Calculator { public static int Add(int a, int b) => a + b; }. Write class CalculatorTests { [Fact] public void TestAdd() { Assert.Equal(5, Calculator.Add(2, 3)); } }. In Main: var tests = new CalculatorTests(); tests.TestAdd(); Console.WriteLine("Test passed: Add(2,3)=5");
Example solution:
using System;
class FactAttribute : Attribute { }
static class Assert
{
public static void Equal<T>(T expected, T actual)
{
if (!EqualityComparer<T>.Default.Equals(expected, actual))
throw new Exception($"Expected {expected}, got {actual}");
}
}
class Calculator
{
public static int Add(int a, int b) => a + b;
}
class CalculatorTests
{
[Fact]
public void TestAdd()
{
Assert.Equal(5, Calculator.Add(2, 3));
}
}
class Program
{
static void Main()
{
var tests = new CalculatorTests();
tests.TestAdd();
Console.WriteLine("Test passed: Add(2,3)=5");
}
}
Test yourself
-
What kind of tool is xUnit?
- A .NET testing framework
- A database library
- A web framework
- A logging tool
Show answer
A .NET testing framework — Correct. xUnit is a unit testing framework for .NET.
-
Complete the test attribute
[____]
public void TestAdd() { Assert.Equal(5, Calculator.Add(2, 3)); }
Show answer
[Fact]
public void TestAdd() { Assert.Equal(5, Calculator.Add(2, 3)); }
-
Match testing terms
- [Fact]
- Marks a test method
- Assert
- Performs validation
- Runner
- Executes the tests
- Test method
- Method containing validation
Next lesson: [Fact]