As a developer, you love to code and fit all the pieces of the puzzle. But there are a few things most developers don’t like. One of them is documentation. It feels like it takes twice as long as developing an application. Another aspect we don’t really like is unit testing. Why make tests of code we just made? Well, there are a lot of reasons to create unit tests. In this blog, I am going to explain the 10 reasons why to use unit tests.
First, I will explain what unit testing is. After that, I will give you 10 reasons why you should unit test. They are in random order since I think each reason is essential.
What is unit testing?
As you might know, unit testing is really important to proof your code. In other tutorials, I explained how you can use unit testing with C# projects. These are basic scenarios concerning unit testing. But what happens if the class or method you are testing needs external data, like a database of a file? Or maybe the method is depending on a different class.
To solve this problem we use mocking, which means creating objects that simulate the behavior of real objects. This is exactly what we want.
Unit testing is not just a way to prove your code works, there are many more reasons to use it. Unit testing isn’t limited to C#, other languages can benefit from this way of working as well.
Reason 1: Proof your code works
Maybe this is the most important reason for us developers. With unit tests, we can test and prove our code works in certain conditions and with different parameters. If you continue on classes and other methods it could happen that some unit tests will succeed first, but fail when you change code. In general, the unit test is leading, unless the specifications change.
Another reason unit testing is helpful is when something goes wrong in the application. If an end-user enters a complaint through the support desk because of an error, support will tell you there is a problem with the code. To avoid being told you didn’t do your job, you can see if the code really breaks, or if it’s a user error, or maybe it’s a design flaw, or the specs weren’t clear.

Reason 2: work together in a team
If you work in a team, or multiple teams are working on the same project, it might happen someone is changing the code unwillingly. In the past, people would just submit the code. Or even worse: Send it to production without fully testing the code. I experienced a really big issue in a webshop that had costs 6000 Euro… Per 15 minutes. The website was down for 1 hour. They couldn’t find the problem.
With unit tests, you could avoid these problems. If you write code with unit tests you can set up CI/CD (Continuous integrations and deployment) that will stop publishing the code to a production environment as soon as there is a problem.
Reason 3: Test your code without GUI
When you need to test if something works you create a proof of concept or POC for short. It means you create a small project dedicated to the one element you want to try.
For example I was creating a project that can push posts to Twitter, Facebook, Instagram, and other social media. This all works and the project was pretty big. Next, I wanted to automate it, and set specific dates and times when something should be pushed to social media. I wanted to use Azure Automation for this, but I had no idea how to use it. So I created a new project outside the existing project to discover Azure Automation.
This way of working doesn’t break or pollute the main project. But I don’t have a GUI to test if everything works. That’s why I create a unit test project where I can build and test different aspects of the POC.
Using unit tests like this also improves performance and time. A unit test starts and finishes way quicker than a GUI.
Reason 4: It's in the pipeline
Continuous integrations and deployment (CI/CD) are really important if you work on bigger projects. It helps you code and deploy way faster. Azure DevOps uses pipelines to set up, configure, and run CI/CD.
Another big advantage of these pipelines is that you can run unit tests during the CI/CD. First, the pipeline will download your code from GIT or Azure GIT, then build it, and run unit tests. If a unit test fails the whole pipeline will stop and notify you.
This is especially convenient if you work with multiple people and someone forgets to check the unit tests before committing the code.
Reason 5: Discover the errors
Imagine you are coding your project. Some music, coffee… The lot. But then disaster strikes: You get an error and you can’t find it. Unit tests can help you with this.
You can create a unit test that simulates all your input and output and hit the error. This could make you realize a certain input or output of another method isn’t correct.
Unit tests can be created and you don’t have to rely on user input or external data. You don’t have to use a real database. This will help you pinpoint errors.
If you found the error, you can debug the unit tests, and go even deeper into the rabbit hole until you find the location of the error. Then you fix it (duh), but you keep the unit test.
Reason 6: Documentation
Unit tests are actually user scenarios, telling what happens if certain actions, parameters, or situations are used. Therefore you can use the unit tests as documentation. They tell you what methods do and how they react to certain situations or what kind of exceptions are thrown.
It’s not like you can export the unit tests to a PDF and give it to the end user. It’s more of a documentation for other developers.
When I start a new job and I see the code, the first thing I do is read the unit tests. It usually gives me a very clear idea of what the code does and how it works. If there are no unit tests I usually try to convince people to create them so I can discover the projects.
Reason 7: Code coverage
Proving your code or making sure no bad code is sent to the production is nice and all, but what happens if not all code is tested? That is why we use code coverage.
It means that you can see what kind of percentage is being tested in your projects. Most developers say that 80% of your code should be tested. I say 90%, but that is personal.
Visual Studio has code coverage built-in, but not in the community version. But don’t sigh yet! There are extensions you can install. Fine Code Coverage is one of them and I have been using this one for a long time. You can find it by going to Extensions -> Manage Extensions in Visual Studio and searching for coverage. Then download and install it. If you run your tests after installation, you will see a window like this:

As you can see I have tested 66% of the code. It allows you to zoom in on what you didn’t test yet. In the code editor, it will add green, yellow, and red markings on the left side. Green indicates the code is tested, yellow means semi-tested (usually if-statements), and red means you haven’t tested it yet.
In the beginning, I said that there is no particular order to these reasons, but I think code coverage is one of the most important reasons why you should unit test.
Reason 8: Performance
Although it is possible, I would not recommend it to use unit tests for performance testing. It’s kind of a hack and unit tests are usually run against local data. Meaning you don’t do external API calls, use a real database, etcetera.
But if you really want to try it, there is a simple piece of code that can help you with that. All you need is a stopwatch and something to execute.
[Fact]
public void Should_NotTakeLongerThanThreeSeconds()
{
MyMovies myMovies = new();
Assert.True(Time(() => myMovies.Delete(1)) <= TimeSpan.FromSeconds(2));
}
private TimeSpan Time(Action toTime)
{
var timer = Stopwatch.StartNew();
toTime();
timer.Stop();
return timer.Elapsed;
}
Reason 9: Readability
If you make your unit tests via specifications, your code will become better to understand. The tests will become your documentation, but building your tests also allows you to create better code. You will think more about the flow of your methods (we test methods, not classes).
Especially when you use TDD, which means test-driven development. Here you create your test first before you create your implementation. Most people find it hard to use this approach, but it solves 99% of the common errors that could crash your app.
Reason 10: Think about it
Creating unit tests, with or without TDD, lets you think about the flow of your methods. You take more time to discover all the possible outcomes in the methods you are testing.
Even when you are writing tests for existing code you will discover new outcomes and you can add new tests, making your code even better.
Conclusion
These are my 10 reasons to create unit tests, maybe there are more or maybe you don’t agree on some, which is fine.
The most important part is that you keep your code healthy, bug-free, and easy to read. Although it’s not 100% bug-free, you can try to make it to at least 90%. Unit tests aren’t bad, they don’t take that much time, and they are super handy. It makes my life a lot easier.