This brief tutorial will show you how to create Unit Test with VisualStudio 2017.

There are a few ways to create unit test using VS 2017.

1) Create unit test From the class

Create new Console Application

And in a Program class add public method.

Right click on a method and chose “Create Unit Tests”.

How to add an Unit test project from a class.

 

 

 

 

 

 

Be sure to add a public method because without it you will get an error:

Create Unit Tests is supported only within a public class or a public method

An error is pretty straight forward 🙂

"Create Unit Tests is supported only within a public class or a public method" error.

Error you get when adding Unit test project from an empty class or class that has only static members.

 

 

 

 

Then you can chose additional options or additional Testing frameworks

Unit test options

 

 

 

 

 

2) Manually adding a test project

Create new method that returns string with value test.

Right click solution and add Unit Testing project

Choosing unit test project

 

 

 

 

 

 

Then add an reference to project you wish to unit test

And create a new class ProgramTests

Decorate class with a [TestClass] Attribute

Also add a ShouldReturnTestString void method decorated with a [TestMethod] Attribute

Be sure to add those Attributes because without them you will not be able to run unit test. Give it a shot.