NUnit is one of the most popular testing frameworks for .NET applications. In this post, we'll walk through how to set up and use NUnit in C# using Visual Studio.
NUnit Test in C#: A Bigner's Guide
Getting Started
Unit testing is a critical part of software development, helping ensure your code works as expected and remains stable over time.
NUnit is a popular and widely used open-source unit testing framework for .NET applications, enabling developers to write tests that ensure their code functions as intended. It is one of the most established and mature testing frameworks in the .NET ecosystem.
Demonstration
Here we will perform unit testing using NUnit framework on a project developed using C# and .Net, lets say we have a project named Calculator in Calculator and the project has a function called Add to perform addion want to perform a unit test using NUnit framework on it.
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
Create Caluculator Project
- Open Visual Studio.
- Click on "Create a new project."
- Choose Console App (.NET Core) or Class Library depending on your needs.
- Name your project as "Caluculator"and click Create.
Create NUnit Project
- Create Project
- Right-click on your solution → Add > New Project.
- Choose Class Library and name as
Tests. - Reference your main project in the test project(Caluculator):
- Right-click the test project → Add > Project Reference → select your main project.
- Install NUnit Packages
- Right-click your solution or project in Solution Explorer.
- Select Manage NuGet Packages.
- Search for and install the following packages:
- NUnit
- NUnit3TestAdapter
- Microsoft.NET.Test.Sdk
Tool
│
├── Calculator
│ └── Calculator.cs
│
└── Tool.Tests
└── CalculatorTests.cs
Note: Make sure Caluculator class exists in your main project with a method Add(int a, int b).
Writing NUnit Test in C#
Create a class like this in yourTests project:
using NUnit.Framework;
using Tool.Calculator
namespace Tests
{
[TestFixture]
public class TestCalculator
{
[Test]
public void TestAdd()
{
// Arrange
Calculator calculator = new Calculator();
// Act
var result = calculator.Add(2, 3);
// Assert
Assert.Equals(5, result);
}
[TestCase(2, 3, 5)]
[TestCase(-1, -1, -2)]
//Parameterized Test
public void TestAdd(int a, int b, int expected)
{
// Arrange
var calculator = new Calculator();
// Act
var result = calculator.Add(a, b);
// Assert
Assert.Equals(expected, result);
}
}
}
Common NUnit Attributes
[TestFixture]:Marks a class that contains tests.[Test]:Marks a method as a test case.[SetUp]:Runs before each test method.[TearDown]: Runs after each test method.[OneTimeSetUp]: Runs once before all tests in the class.[OneTimeTearDown]: Runs once after all tests in the class.[TestCase(...)]: Defines parameterized test cases.
Summary
That’s it! You now know how to use NUnit with Visual Studio to create, run, and manage unit tests in your C# projects. With automated tests in place, you'll have more confidence in your code's stability and quality.
Thanks