NUnit Test in C#: A Bigner's Guide

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
  1. Open Visual Studio.
  2. Click on "Create a new project."
  3. Choose Console App (.NET Core) or Class Library depending on your needs.
  4. Name your project as "Caluculator"and click Create.

Create NUnit Project
  1. 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.
  2. 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

Project Structure
 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 your Tests 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

  1. [TestFixture] :Marks a class that contains tests.
  2. [Test] :Marks a method as a test case.
  3. [SetUp]:Runs before each test method.
  4. [TearDown]: Runs after each test method.
  5. [OneTimeSetUp]: Runs once before all tests in the class.
  6. [OneTimeTearDown]: Runs once after all tests in the class.
  7. [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

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال