Environment Setup – NUnit Tutorial

There are two steps in configure NUnit project environment:

  1. Configure Project with NUnit assemblies
  2. Setup TestRunners which show the results of NUnit test cases

Configure Project with NUnit assemblies

We always creates separate project when creating project for NUnit. According to naming conventions test project name should be [Project Under Test].[Tests]. For example, if we are testing the project name “CustomerOrderService” then test project name should be “CustomerOrderService.Tests”.

  1. Visual Studio -> New Project -> Class Library -> Name: CustomerOrderService
  2. Right click on solution -> Add New Project -> Class Library -> Name: CustomerOrderService.Tests

Now we have two projects in our solution. In CustomerOrderService project, we write code for business logic and in second project CustomerOrderService.Tests we write test cases for CustomerOrderService project.

Our next step is to add Nunit assemblies.

  1. Right click on CustomerOrderService.Tests and choose “Manage NuGet Packages”.
  2. In NuGet search box, Choose Browse tab and type Nunit in search textbox.
  3. Choose NUnit and click on Install button.
  1. NUnit assembly (nunit.framework) is added to our test project.
  2. Add reference of our CustomerOrderService class library to test project.
  3. Choose add reference in test project -> Project – Solution tab -> Mark the checkbox before the CustomerOrderService -> Click on OK button.

Now our test project is configured with Nunit assemblies. Our next step is to add TestRunners to our solution.

Setup TestRunners

For setup TestRunners, we need to add Nunit Test Adapter from NuGet packages. Follow the below steps:

  1. Right click on CustomerOrderService.Tests and choose ‘Manage NuGet Packages’
  2. Choose NUnit3TestAdapter and click on Install button.

Now we have to write sample test case to check whether every thing is setup successfully or not. Write a sample test case in class1 of CustomerOrderService.T

Include namespace NUnit.Framework in namespaces section. Write the above code exactly in Class. We’ll learn more about TestFixture attribute, Test attribute and Assert class in our next posts. Now Build the solution.

Choose visual studio Test Menu -> Windows ->  Test Explorer.

Text Explorer shows Test function in Not Run Tests section. Choose Run All button to execute test cases.

In below of Test Explorer, it will show the result of Test1 test result. In the below screenshot, it is showing the result ‘Test Passed – Test1’.


Now our Test project and TestRunner is configured properly. In next posts, we’ll learn more about Nunit attributes and classes.

Introduction to NUnit

NUnit is a unit testing framework for .NET. It is the most used framework for writing unit test cases.

We can write testing code in either C# or VB.NET. It is suggested to write testing code in different assemblies called Test Assemblies. These assemblies only contain testing code nothing else. We need to run these test assemblies to check whether all test cases are passed or failed. For that we required Test Runner.

Test Runners are UI tool which actually run NUnit test cases and show the result of test cases whether they are passed or failed. We’ll learn about test runners in Environment Setup in next post.

NUnit is very easy to use. It only provides some custom attributes and some static Assert classes. With the combination of custom attributes and static classes, we can write unit test cases easily.

Custom attributes provides hint to NUnit test runners that these classes or functions contains unit testing code. Assert classes is used to test the conditions whether system under test (SUT) satisfy a condition or not. If condition is satisfied then test is pass else fail.

Some of the custom attributes are:

  • TestFixture
  • Setup
  • TearDown
  • Test
  • Category
  • Ignore
  • TestCase
  • Repeat
  • MaxTime

In the next post, We’ll learn how to setup your environment for writing NUnit test cases.