How to unit test in Visual Studio 2019

ZeroesAndOnes
8 min readMay 15, 2020

The Microsoft unit test framework is installed with Visual Studio and provides a framework for testing .NET code. In this article I will walk you through step-by-step showing you hot to properly create a unit test project and write a unit test to test a specific unit of work in an MVC project.

1 — Create a Unit Test project

To create a new Unit Test project and associate it with your solution the first thing that you must do is right click at the root level of the solution in the Solution Explorer panel. Then find the “Add” option from the context menu and in the sub-menu find the “New Project” option and click on it.

The “Add a new project” window will appear in Visual Studio. On that screen, you can scroll down through the list to find the “Unit Test Project (.NET Framework)” and then click the “Next” button.

Additionally, you can filter down the list of projects by project types and choose the “Test” option which will only show projects that are of type test. This can speed up your search process instead of having to scroll down through the list.

Then on the next screen, “Configure your new project”, you will need to provide a Project name. This is the name of the unit test project and it can be anything you want it to be. You will also need to provide the location where you want the project to be saved. I usually like to save this project in the same root directory of the entire solution. Finally, you will need to specify the .NET Framework version if you are using .NET Framework.

Once you provide all the require information click on the “Create” button. This will create the unit test project in the solution. If you expand the “Solution Explorer” pane you will notice that a new project, “UnitTest”, has been…

--

--