Practical Introduction to Unit testing in C# and Visual Studio

Learn what a unit test is and how to get started writing unit tests in C# with Visual Studio and NUnit.

If you liked the video, please share it with your friends and colleagues and subscribe to the newsletter to be the first to receive news and updates from Productive C#.

Transcript Of The Video

In this video I want to give you a quick and extremely practical introduction to unit testing in C# and Visual Studio.

I have a solution with a library and a test library. I have a class called Math that contains a simple method called Abs that actually calculate the module of a number. If the number is positive will stay the same, if the number is negative will return the opposite number. Then I have a test project that is referencing this library, Productive C# and I also added a reference to NUnit. This is a NuGet package that basically provides a unit testing framework.

NUnit is one of the the many unit testing frameworks available in .NET. It is one of the most commontly used and I am going to use this for the purpose of this video.

Let's have a look at the AbsTests. This is a class. I am refering the namespace NUnit.Framework at the top and you can see an example of a Test Fixture. Each class that describe a set of tests need to be decorated with a test fixture attribute. Each test is acually a method and is decorated with a Test attribute. 

If you have the Math.Abs method how you go about testing without using any sort of unit tests? You probably would created a console application, just try to create the Math object calling Abs with differnet numbers: a positive number, a negative number and zero that is a bit of an edge case. Then you see the console output what the results is.

So you might think, why do I need unit tests?
What's actually a unit test?

Unit Tests is an automatic way of verifying the behavior of a particular unit of work very quickly. A unit test is something that runs extremely quickly. Runs in memory. Runs code that does touch any external systems like file system or databases. Unit tests allows you to verify the behavior of your system. 

The idea is that if you have a system that contains a lot of code, you will have a large numbers of unit tests that cover that particular code. They can be run automatically to verify that the behavior of the system is correct. Having so manu tests basically allows you to have a safery net. If you are going to change the code in the future, adding new features or refactor in the code to make it bette.

You have a safety net that allows you to make sure your changes are safe and you are not changing the behavior of the system. One of the key element is that unit tests usually run automatically. Often, professional projects use continuous integration. That means that every single time a commit is pushed to a source control server, the build kicks in and all the unit tests runs automatically to verify that the system works.

The amount of feedback that you get from unit tests is incredible. You increase the confidence that you have in chaning your code. 

In this case, I want to show you, we actually create a test that checks that calling Abs with -10 is actually returning 10. We are testing that calling Abs with zero will actually results in zero and if we call Abs with 5 we actually get 5. The system is like that. Assert is a class provided by NUnit and you are actually asserting that expected value 10 is equal to the result of this expression that is the execution of Abs. It's as simple as that. So even if you are not familiar and are completely new to unit tests you can actually easily read this code. 

How can you actually run the unit tests locally in your machine in Visual Studio?

You can Visual Studio or you can use ReSharper that is a paid extension that I use all the time that I definitely reccomend you to use but you can certainly use Visual Studio for free if you want.

In order to run your tests in Visual Studio you need to reference the NUnit3testAdapter package. As soon as you do it you can see a Test menu in your Visual Studio menu and you can do "Run All Tests".

What happen is that Visual Studio build the solution and now the tests runs. You will be able to see a report of which tests passes and which not. As you can see here all tests passed. And this is pretty much what you get.

Let's try for example to go in the implementation and change it slightly. Let's say I add + 1 here, of course this is not correct. I could go here, right click and do run selected tests and we probably expect that the test fail. And you see a message. "Expected 10 but was 11".

It's very important that your unit tests when they fail say very clearly what is failing. So this unit test actually gives you the safety net that if you go and change somethign about the particular unit of test you know that are still actually satisfying the behavior. 

The other way of running the tests is to actually using ReSharper. You can do it easily from the ReSharper menu or you can go to the left and click "Run all". This will run all the tests in this particular test fixture. Samething similar happens, a test session opens and you can see the results of running the tests. 

Okay, so what we actually have here? 

We have a piece of code, Math and we have same tests that test the Abs functionality of Math. Tese tests are unit tests that verify that the behavior of Abs is correct.

This is actually a very short and simple introduction to unit testing in C#. We are going to explore a lot of more in future videos. Thank you very much for watching.


Sharing is Caring
  • […] Practical Introduction to Unit testing in C# and Visual Studio […]

  • >