Beginner's guide to Unit Testing with NUnit.
Tags: beginners, testing, csharp, tutorial
Published: 2024-01-10 09:33:44

Hello Everyone.

Today I'll be going through a short introduction to Unit Testing Using NUnit. To make it easy, we'll be testing simple CRUD(Create, Read, Update, Delete) Operations on a List.

Let's Begin

First Create a new Console Application, I have named mine UnitTestingLists

Next in your UnitTestingLists project, click on your Solution Explorer and create a folder named Interfaces, this will hold the interface our service will use. Then add a file to that folder named IListService

![Create IList](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0lwkx63e3k23kkwempk.png)

Next in your IListService, add the following code

```cs
public interface IListService
{
    void Add(string item);
    void Remove(string item);

    void Edit(string oldItem,  string newItem);

    List<string> GetItems();
}
```

These are all the operations we will create.

Next, in your UnitTestingLists project, create a folder named implementations. Then in this folder create a class called ListService. This is the service that Implements our Interface. 


![Create IList Implementation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1y883fzgxyo1hdn86q22.png)

Then in your ListService class, add the following code
```cs
public class ListService : IListService
{
    private static List<string> _list;
    public ListService()
    {
        _list = new List<string> {"Apple", "Orange", "Pear", "Watermelon", "Lemon","Lime" };
    } 
    public void Add(string item)
    {
        _list.Add(item);
    }

    public void Edit(string oldItem, string newItem)
    {
        if (_list?.Count > 0 && _list.Contains(oldItem)){
            int indexToRemove = _list.IndexOf(oldItem);
            _list[indexToRemove] = newItem;
        }
    }

    public List<string> GetItems()
    {
        return _list;
    }

    public void Remove(string item)
    {
        _list.Remove(item);
    }
}
```

We have added a lot of code so let's go over it. We created a static list that we will work with and added some items. We also added some simple methods that do different operations on our list.

Great, Next let's create a test project. Right-click on your solution and go to Add, then click on Add New Project

![Add New Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9b62u5114b2pvpvpdo6.png)

In the Add New Project window, search for "Nunit" and Click on "NUnit Test Project"

![Add NUnit Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9ygb7r1a7pg4f90c6d5.png)

Give your project a name and keep clicking "next" and then create. I have called mine "ListTestProject". So far, your entire project should look something like this.


![Project Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4tra0p4ssdmygsxr4mg.png)

OK, in your ListTestProject there should be a file called UnitTest1, when you click that, you should have something like this

![UnitTest1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mz3d7ypchu5xwjjohnv4.png)

Let's go ahead and write our first test.
Every test method has the decorator [Test] to indicate it is a test, so go ahead and add this code,

```cs
 [Test]
 public void List_Should_Contain_Item()
 {
     //Arrange
     var listService = new ListService();
     string fruit = "Watermelon";
     List<string> list = mockService.GetItems();

     //Act
     bool isThere = list.Contains(fruit);

     //Assert
     Assert.IsTrue(isThere);
 }
```

This is a test to check that one of the items we added when creating our listService is present.

Now, go ahead and right on the test project and click on Run Tests.


![Test Explorer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xsm2thpok8nxeu64fjjh.png)
As you can see, the item is in the list so we get a passing Test

Let's add a couple more tests and run them, Add the following code to your UnitTest1 test class

```cs
 [Test]
 public void List_Should_Add_Item()
 {
     //Arrange
     var listService = new ListService();
     string fruit = "Grapes";
     List<string> list = listService.GetItems();

     //Act
     listService.Add(fruit);

     //Assert
     Assert.Contains(fruit, list);
 }

 [Test]
 public void List_Should_Replace_Old_Item_With_New()
 {
     //Arrange
     var listService = new ListService();
     List<string> list = listService.GetItems();
     int indexOfRemove = 1;
     string oldFruit = "Orange";
     string newFruit = "Strawberry";

     //Act
     listService.Edit(oldFruit, newFruit);

     //Assert
     Assert.That(list[indexOfRemove], Is.EqualTo(newFruit));
 }

 [Test]
 public void List_Should_Delete_Item()
 {
     //Arrange
     var listService = new ListService();
     List<string> list = listService.GetItems();
     string deleteFruit = "Pear";

     //Act
     listService.Remove(deleteFruit);

     //Assert
     Assert.IsFalse(list.Contains(deleteFruit));
 }
```
Alright, we added 3 more tests to make sure the operations in our ListService are working correctly. 
So instead of running your tests from the project itself, in the top menu bar, click on test and open the Test explorer

![Test - Test Explorer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpw1mtpk6m1rxk24l09q.png)

Your test Explorer should then open this way


![Test Explorer Opened](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4v6j07m4yo46y7uz4e6x.png)

This way, you can then choose to run all tests or run tests Individually.
Let's Run all our tests to see.

![Run All Tests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efk5p6tg9hl253563iix.png)

And we see that all our tests passed.
You can decide to add more meaningful tests to ascertain other functions.
If you would like the source code used, you can get it here.