
Overview
The xUnit testing framework is a popular choice in .NET 8 for writing and executing unit tests in C# and is an integral part of software development. We will cover essential concepts and provide code examples in this comprehensive guide as we explore the fundamentals of unit testing with xUnit.
Prerequisites
To follow this article, ensure we have the following installed.
- .NET SDK 8.0 or later (Download .NET SDK)
- Visual Studio Code or Visual Studio (optional but recommended)
Creating a Sample Project
The first step is to create a simple C# console application and its corresponding unit test project using xUnit.
# Create a new console application
dotnet new console -n ZiggyRafiqConsoleApp
# Create a new xUnit test project
dotnet new xunit -n ZiggyRafiqConsoleApp.Tests
Write Our Console App
For the Program.cs we just printed my name Ziggy Rafiq and this article title. As the code example below shows us.
Console.WriteLine("Hi and Welcome to Ziggy Rafiq Article on Unit Testing with xUnit in .NET 8: A Comprehensive Guide");
Create Our Calculator Class
We just create a simple Calculator class with simple functions such as Add, Subtract, Multiply, Divide, and the key one Dispose of as the code example below.
namespace ZiggyRafiqConsoleApp
{
public class Calculator : IDisposable
{
// Add method
public int Add(int a, int b)
{
return a + b;
}
// Subtract method
public int Subtract(int a, int b)
{
return a - b;
}
// Multiply method
public int Multiply(int a, int b)
{
return a * b;
}
// Divide method
public double Divide(int a, int b)
{
if (b == 0)
{
throw new ArgumentException("Cannot divide by zero.");
}
return (double)a / b;
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Dispose of managed resources here go here
}
// Dispose of unmanaged resources goes here , if any
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~Calculator()
{
Dispose(false);
}
}
}
Data Source
We will create two files for our Data Source one is an interface and the other is a class as code example below.
namespace ZiggyRafiqConsoleApp
{
public interface IDataSource
{
string GetData();
}
}
namespace ZiggyRafiqConsoleApp
{
public class DataService
{
private readonly IDataSource _dataSource;
public DataService(IDataSource dataSource)
{
_dataSource = dataSource;
}
public string ProcessData()
{
return _dataSource.GetData().ToUpper();
}
}
}
Now we are all set to create our Unit Tests 😊
Writing Our First Test
Our preferred code editor should open the ZiggyRafiqConsoleApp.Tests project. XUnit automatically creates a test file named UnitTest1.cs.
To replace UnitTest1.cs with the following code example, please refer to the below code example. As an example, we will create a simple test method named TestAddition. The [Fact] attribute indicates that this method is a test.
namespace ZiggyRafiqConsoleApp.Tests
{
public class ZiggyRafiqConsoleAppTests
{
[Fact]
public void TestAddition()
{
// Arrange
int a = 2;
int b = 3;
// Act
int result = a + b;
// Assert
Assert.Equal(5, result);
}
}
}
Join the conversation! Your thoughts help the community grow.