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”.
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 🙂

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.
1 2 3 4 |
public string TestResult() { return "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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestingExample.UT { [TestClass] public class ProgramTests { [TestMethod] public void ShouldReturnTestString() { //Arange Program prog = new Program(); //Act string result = prog.TestResult(); //Assert Assert.AreNotSame("test", result); } } } |
Be sure to add those Attributes because without them you will not be able to run unit test. Give it a shot.
Leave a Reply