Unit Testing in .NET Core with NUnit

Introduction

Unit testing is an essential practice in software development that ensures the correctness and reliability of individual components of an application. NUnit is a popular unit testing framework for .NET, providing a wide range of features to create and run tests. In this article, we will explore the usage of NUnit in .NET Core, demonstrating how to set up and write unit tests, run them, and interpret the results.

Setting Up NUnit in .NET Core

Before we can start writing tests, we need to set up our .NET Core project to use NUnit. Follow these steps to create a new .NET Core project and install the necessary packages.

Step 1. Create a New .NET Core Project.

Open your terminal or command prompt and run the following command to create a new .NET Core console application.

dotnet new console -n NUnitExample
cd NUnitExample

Step 2. Add NUnit Packages.

Add the NUnit and NUnit3TestAdapter packages to our project. The NUnit3TestAdapter package is required to run NUnit tests in Visual Studio and other test runners.

dotnet add package NUnit
dotnet add package NUnit3TestAdapter

Unit Tests with NUnit

With the packages installed, we can now create our test project and write some unit tests.

Step 3. Create a Test Project.

In the same directory, create a new class library project for our tests.

dotnet new classlib -n NUnitExample.Tests
cd NUnitExample.Tests

Step 4. Add NUnitExample Project Reference.

Add a reference to the main project in the test project.

dotnet add reference ../NUnitExample/NUnitExample.csproj

Step 5. Create a Test Class.

Create a new file named CalculatorTests.cs in the NUnitExample.Test the project and add the following code.

using NUnit.Framework;
using NUnitExample;

namespace NUnitExample.Tests
{
    [TestFixture]
    public class CalculatorTests
    {
        private Calculator _calculator;

        [SetUp]
        public void Setup()
        {
            _calculator = new Calculator();
        }

        [Test]
        public void Add_ShouldReturnCorrectSum()
        {
            // Arrange
            int a = 5;
            int b = 3;

            // Act
            int result = _calculator.Add(a, b);

            // Assert
            Assert.AreEqual(8, result);
        }

        [Test]
        public void Subtract_ShouldReturnCorrectDifference()
        {
            // Arrange
            int a = 5;
            int b = 3;

            // Act
            int result = _calculator.Subtract(a, b);

            // Assert
            Assert.AreEqual(2, result);
        }
    }
}

Step 6. Create the Calculator Class.

create a new file named Calculator.cs and add the following code.

namespace NUnitExample
{
    public class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }

        public int Subtract(int a, int b)
        {
            return a - b;
        }
    }
}

Step 7. Run the Tests.

In the terminal, navigate to the NUnitExample.Test the directory and run the following command.

dotnet test

Sample Output

The output of the test run should indicate that all tests have passed.

Sample Output

Conclusion

NUnit is a powerful and flexible unit testing framework for .NET, making it easy to write and run tests for your applications. In this article, we demonstrated how to set up a .NET Core project with NUnit, write unit tests, and run them. By following these steps, you can ensure the correctness and reliability of your code, ultimately improving the quality of your software.

Up Next
    Ebook Download
    View all
    Learn
    View all