Home/How to Unit Test: A Guide to a Clean Testing Environment (PART 2)

In part 1 we talked about how to choose the right technology for a greenfield and a brownfield project and what to keep an eye on. In the second part of this blog series, we dive deeper into unit testing and how to set it up.

Unit Tests Scope and Execution

When talking about the unit tests scope, two different meanings come to mind, and we’ll talk about both.

First is the regular testing that tries to achieve multiple goals depending on the scenarios. Unit tests are written in three different scenarios:

  • TDD (Test Driven Development) – you write your test code before writing the actual code. That approach automatically covers the happy case and a couple of error scenarios
  • You write unit tests to ensure that the happy case works the way you expect it to and some error scenarios
  • You reproduce a bug using a unit test to then ensure that this bug will never happen again by adding the new unit test

All in all, what we try to achieve by unit testing is to improve software quality while reducing the development cost. Better code for less money, which customer wouldn’t want that. And luckily unit tests are on the cheaper side of improving the code quality. But we’ll talk about that in the next chapter.

The second is a bit more unorthodox usage of unit tests. One example would be to validate the signatures of the files as part of the build using the unit tests. Another would be to enforce some specific code quality rules without using additional static analysis frameworks. 

Let’s quickly dive into how to set up a unit test in Visual Studio 2019 and Visual Studio 2022 and then create a very simple test and run it. The reason we’re doing it for both 2019 and 2022 is that it was simplified in VS2022, which is cool.

Visual Studio 2019 Unit Test Setup

VS2019 supports neither nUnit nor xUnit projects. We’ll create a .NET 4.8 Class Library solution.

Just to showcase both options, we’ll go with nUnit for VS 2019 and xUnit for VS 2022 as the unit testing framework of choice. Both frameworks are similar, but we’ll talk about them in more detail in a different post. What we need to do next is to add theStyleCop unit to the project.

Test report combining test steps with test input, expected & actual result

NUnit Installation through Nuget

Now if you have ReSharper, that’s all you must do, if you don’t, you also have to add the NUnit3TestAdapter to your project. The attributes “TestFixture” and “Test” that you can see here is to mark our unit test class and unit test for the runner

Example of a simple Unit Test

Otherwise, you can access the Test Explorer through View -> Test Explorer. Which then will allow you to run the unit test.

Visual Studio 2019 Test Explorer

Visual Studio 2022 Unit Test Setup

It doesn’t matter whether you liked or disliked the way adding the ability to run unit tests in VS 2019 was done. You’ll love the way it’s available in VS 2022.

All you must do is select the xUnit or NUnit template when creating the project and everything else will be done for you. The test runner is accessed at the same spot View -> Test Explorer.

VS 2022 Test Project Creation

VS 2022 Test Project Creation

Code Quality

We went all the way through the CI up to the point of writing and running simple tests. Since we already have such a good setup of having both CI and unit tests, let’s also briefly touch down on improving the code quality.

We already established the fact that unit tests help us in ensuring the code does what we expect it to do, provides an early error detection system, and is in general a great and cost-efficient thing. Unfortunately, it’s not very efficient to test every possible scenario with unit tests. Luckily some tools allow us to do exactly that and even more.

Tool Short description

StyleCop

StyleCop

StyleCop

Performs static code analysis to enforce the certain style and consistency rules. There is pretty much no reason to not use it.

 

It’s free, it’s open-source and the defined rules are mostly valid. For those that you want to change or remove, you can freely do so. It’s highly modifiable.

ReSharper

Loved by many, hated by some. ReSharper. In my opinion, a must-have tool for any developer using one of the older Visual Studio versions.

 

It does cost some money, so if you’re using Visual Studio Code or Visual Studio 2022, count yourself lucky, ReSharper is less of a necessity. Still, it’s a great static analysis tool that allows you to adopt the best coding practices.

SonarQube

You must have realized by now that I’m a bit of a Microsoft products fanboy when it comes to developing, which is most definitely not the reason I just have to mention SonarQube.

 

In a fully-fledged environment, it becomes an extremely powerful tool that is capable of very in-depth code analysis that detects vulnerabilities, cyclomatic complexity and can also check and enforce coding guidelines.

 

 

 

Besides unit testing, it’s always good to have End-to-End testing up and running automated. Not sure which tool to use? TestResults.io is always a good choice 😉 The only test automation tool that developers and testers ❤ love and QA Engineers, Test Analysts, Test Automation Engineers, Business Analysts, Management, …