Unit Testing with Visual Studio

Leave a comment

In this post, I will show how you can use Unit Testing in Visual Studio. We will go through an example where we create some simple code and then unit test it.

First, we will create a Visual Studio project as a class library:

We are going to write a simple function to check if a character passed in is a vowel.

Next, add a new project for our Unit Tests. Select Test from the left and then Unit Test Project:

In the Test project, add a reference to the original project:

Now we can write some unit tests. Open the unit test and write some code that checks for conditions when the test is run. In this case, our code is in 3 parts:

  • Arrange – we initialize our variables and set our expected variable to what the outcome should be
  • Actual – we call our original class and receive output based on what we send to it
  • Assert – based on the expected and actual result, we inform whether the unit test has passed as we expect it or not

Below, we are sending an integer to our CheckVowel procedure. We expect an integer to return false as this is not a vowel, so our Expected variable is false:

To run the code, in Visual Studio go to the Test menu and select Run->All Tests:

The Test Explorer opens and shows us the output – this test passed:

We can add more tests to the project to test for more conditions. Remember to include [TestMethod] or the test will be ignored:




Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube


Leave a Reply

Your email address will not be published. Required fields are marked *