top of page

C# Tutorial For Beginners: Setup NUnit Test Runner

Updated: Jan 18, 2022

In this post, I will show you how you can install NUnit and how you can use it to increase the clarity, readability, and efficiency of your test structure.

What Is NUnit?

NUnit is an open-source unit testing framework for the .NET Framework and Mono. It serves the same purpose as JUnit does in the Java world and is one of many programs in the xUnit family. - Wikipedia


NUnit is the Unit Testing framework supported by Visual Studio and Selenium WebDriver. NUnit is the most widely used Unit Testing framework. Net applications. NUnit presents the test results in a readable format and allows a tester to debug the automated tests.


Steps to install NUnit Framework:

We need to install NUnit Framework and NUnit Test Adapter onto Visual Studio to use it.


Step1: Open the "Manage Packages for Solution " window

Open Visual Studio -> Navigate to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution

Step 2: Manage Packages for Solution

  1. in the search, menu -> Change to "Browse" option instead of "installed."

  2. Search for NUnit latest version.

  3. Select the search result

  4. Select one of your projects

  5. Click Install


Step 3: License Acceptance

Step 4: The below message will appear once the installation is complete.

Now that we deployed the NUnit testing framework, we still need to run the NUnit Test Adapter, which allows you to run NUnit tests inside Visual Studio.


Step 2: Manage Packages for Solution

  1. in the search menu -> Change to "Browse" option instead of "installed."

  2. Search for NUnitTestAdapter latest version.

  3. Select the search result

  4. Select one of your projects

  5. Click Install

Once install is done, you will see the following message:

Selenium and NUnit framework:

Integrating selenium with NUnit allows us to increase code clarity by differentiating between various test classes. In addition, it provides a solid structure of code by using annotations such as SetUp, Test, and TearDown to perform actions before and after running the test.


The integration between the two frameworks is done by creating an NUnit test class and running the test class using the NUnit framework:



How to create a Test Suite in your project

let's see a simple example of creating a test class that we can use in our project.


Step 1:

In the Solution Explorer, Right-click on project > Add > Class

Step 2: Provide a name to the class -> Click on Add button

Now that we set the infrastructure for our tests, it's time to write some code:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;

namespace SeleniumWeb
{
    class TestClass
    {
        IWebDriver driver;

        [SetUp]
        public void startBrowser()
        {
            driver = new ChromeDriver("Site Url");
        }

        [Test]
        public void test()
        {
            Assert.AreEqual(driver.Title, "Expected Result");
        }

        [TearDown]
        public void closeBrowser()
        {
            driver.Close();
        }
    }
}

Step 3) Click on 'Build' -> 'Build Solution'

Step 4) Open the Test Explorer window. Click on Test -> Windows -> Test Explorer




258 views0 comments