Writing your first unit test

Welcome back!

I hope you have found the last few posts informative. Today we get to the practical side of things. Today we write our first unit test.

So, first step, what are we using?

For this example I will be using Visual Studio 2019.

Open Visual Studio and open a new project. In my example I will be using Console App (.Net Framework).

Next right click on the Solution. That is the top line in the Solution Explorer. Here we select “Add” and add a new project. This time we will be adding a unit test project, note: make sure that you select the .Net Framework unit test file.

I will also be using NUnit for this example. Both of these are free to use, and to access NUnit in Visual Studio by downloading the Nuget packages which are required.

Now that is all set up, now delete everything which is already filled out in the public void TestMethod1 method. Also delete the TestClass attribute, and update the attribute above the method to say Test. That’s it, we’ve set up the skeleton of our first unit test!

It should look like this:

Next we will start building on that skeleton.

First, we need to tell the IDE what class we are testing. I know we’ve not set anything up yet so let’s be sure to add that in. For this example let’s build a bank account system. We’ll name the class an Account. To run the test, we will need to create a new instance of the account within the test class. I will call this “sut”, which stands for system under test. You can name it whatever works for you but you will see many developers stick to “sut” in their testing environment.

var sut = new Account();

As we haven’t created a class called Account, you’ll notice the IDE will complain. There is an easy fix for this with Visual Studio, where you can right click on the class name and you will get the option where Visual Studio will generate an internal class with that name.

Next we need to set up the method within the class. Let’s say that we want to add money to our account, we will need to create a method which will update the account when money is deposited. We will call this the “AddMoney” method. In the test method we will create another variable which will establish an instance of that method.

var addMoney = sut.AddMoney();

However, you will see the IDE will complain again as the Account class does not contain an AddMoney method. Like the class, right click on the alert and Visual Studio will generate that method. As we wrote sut.AddMoney() the IDE will understand that the AddMoney method needs to be within the Account class.

So now we have the test skeleton set up, we have the Account class initiated as the system that we want to test, and we have created the method within the class that we want to set up.

Now let’s step away from the test class for now and set up that AddMoney method so that it actually does something which we can test. For the purposes of this example I’m not going to make it too fancy, instead let’s focus on having the method return the value that is added. To do this we will need to pass in an integer. I’m then going to tell the method to simply return the value that I pass in.

Your method should look like below:

Ok, let’s go back to the test class. Now we are going to write the Assert part of the test. Remember my previous post where we discussed the three As of unit testing. Well this is the final one: Assert. And it’s easy enough to spot because we need to use the Assert object.

Within this object you can access a variety of methods, however the most common one to use recently is Assert.That(). You can still use other methods however these are not maintained at the time of writing this post so it would be good practice to get used to That().

Ok so what do we want to check? Ok well for this one let’s just make sure that the value which is returned is equal to what we expect it to be. So in this example, we will pass in a value of 100. Therefore we will expect the value that is returned will be 100.

So how do we write this? Well, it looks like a sentence:

var addMoney = sut.AddMoney(100);
Assert.That(addMoney, Is.EqualTo(100));

And our final product? It should look like this:

And our Account class should look like:

Now that that’s all set up, let’s try running the test! To do that we go to the Test menu and run the tests. The Test Explorer will pop up as the tests are being run, and you will see a list of the tests which you have written.

Once the tests have run you will either see a green circle with a tick beside the test name (as in the screenshot below) which shows the test passed, a red circle with a cross which indicates a failing test or a question mark if the test did not run.

Ours have passed! That’s great news. Just to check if this wasn’t just a fluke then we can change any bit of the test. I’ll change the returned value in the AddMoney method so that it returns 0 no matter what is passed in. Then I will run the test again.

This time the test not only fails but it will also return an error message in the Test Explorer.

That’s exactly what I was expecting. We told the test it should return 100 however the test will only return 0. That proves that we have set the test up properly and we can build up from there. But for now, congratulations and well done, you’ve successfully written your first unit test!

2 responses to “Writing your first unit test”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: