How To Add Unit Tests To Your C# Project Using MSTest & Visual Studio

Alex Norris
Strategio
Published in
3 min readFeb 18, 2022

Let’s say you started working on a project in C# and want to write automated tests to make sure your requirements are met. In this article, I will walk you through how to get started!

Requirements

  • Visual Studio: I will be using VS Community 2019
  • Existing C# Project: I will be using a simple calculator for an example.

Add MSTest Project To Solution

  1. Right-click the solution in the Solution Explorer, select Add, then select New Project.

2. In the resulting window, search MSTest and make sure you select the C# version.

3. Furthermore, ensure you are using the same Target Framework as your existing project.

4. Right-click the new MSTest project, select Add, then select Project Reference.

5. Click the toggle button next to your existing project and click okay.

6. Last, but not least, go to the Preferences of your existing project and set the Output type to Class Library.

Add Tests And Run Them

  1. Add the using statement to include your existing project. For me it is “using a calculator;”.

2. Add tests to ensure proper execution of your project’s functions.

3. Click Test at the top of the window, then select Run All Tests.

4. This will open the Test Explorer and you can navigate through your tests to view the results!

Conclusion

Congratulations! Now you can get started writing away at your unit testing! Be sure to hit all of your acceptance criteria with these to make for a solid test-driven development!

Thanks for reading. Feel free to comment with questions and concerns. Also, consider following!

--

--