Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using NUnit for Unit Testing in C#

Introduction

NUnit is a popular open-source unit testing framework for .NET languages, particularly C#. It allows developers to write and run tests to ensure their code behaves as expected. This tutorial will guide you through the process of setting up and using NUnit from start to finish.

Setting Up NUnit

First, you need to install NUnit and the NUnit3TestAdapter to your project. You can do this using the NuGet Package Manager in Visual Studio or by running the following commands in the Package Manager Console:

Install-Package NUnit

Install-Package NUnit3TestAdapter

Creating a Test Project

It's a good practice to keep your test code separate from your production code. To do this, create a new Class Library project in Visual Studio for your tests. Then, reference your main project within this test project.

In Visual Studio:

  • Right-click on your solution in Solution Explorer.
  • Select Add > New Project.
  • Choose Class Library (.NET Core) and name it appropriately (e.g., "MyProject.Tests").
  • Right-click on the test project and select Add > Reference. Add a reference to your main project.

Writing Your First Test

Now that you have your test project set up, you can start writing tests. NUnit uses attributes to indicate test methods and test classes.

Here is an example of a simple test:

using NUnit.Framework;

namespace MyProject.Tests
{
    [TestFixture]
    public class CalculatorTests
    {
        [Test]
        public void Add_TwoNumbers_ReturnsSum()
        {
            // Arrange
            var calculator = new Calculator();

            // Act
            var result = calculator.Add(2, 3);

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

In this example:

  • [TestFixture] indicates a class that contains tests.
  • [Test] indicates a method that is a test.
  • The Assert.AreEqual method is used to verify that the actual result matches the expected result.

Running Your Tests

To run your tests, open the Test Explorer in Visual Studio (Test > Windows > Test Explorer). You should see your test listed there. Click "Run All" to execute all tests.

If your test passes, you will see a green check mark. If it fails, you will see a red cross, and you can click on the test to see the details of the failure.

Advanced Testing Techniques

NUnit offers various advanced features for more sophisticated testing scenarios:

Testing Exceptions

You can test if a method throws an expected exception using the Assert.Throws method:

using NUnit.Framework;

namespace MyProject.Tests
{
    [TestFixture]
    public class ExceptionTests
    {
        [Test]
        public void Divide_ByZero_ThrowsException()
        {
            // Arrange
            var calculator = new Calculator();

            // Act & Assert
            Assert.Throws(() => calculator.Divide(10, 0));
        }
    }
}

Parameterized Tests

Parameterized tests allow you to run the same test method with different parameters. Use the [TestCase] attribute for this:

using NUnit.Framework;

namespace MyProject.Tests
{
    [TestFixture]
    public class ParameterizedTests
    {
        [TestCase(2, 3, 5)]
        [TestCase(-1, -1, -2)]
        [TestCase(0, 0, 0)]
        public void Add_VariousInputs_ReturnsSum(int a, int b, int expected)
        {
            // Arrange
            var calculator = new Calculator();

            // Act
            var result = calculator.Add(a, b);

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

Conclusion

NUnit is a powerful framework for unit testing in C#. By following this tutorial, you should now have a good understanding of how to set up NUnit, write and run tests, and use some of its advanced features. Happy testing!