Live Unit Test In Visual Studio 2017

Introduction 

Live Unit testing is one new feature added in Visual Studio 2017, where the unit test happens automatically in background, once your code gets edited. 
 
Creating a console Application
 
Open Visual Studio 2017 -> New-> Project -> Installed templates-> Visual C#-> Windows Classic Desktop-> Console App, I named the project as SampleProject. 

 
 
Write the code given below in Program.cs. 
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.   
  6.            Program p = new Program();  
  7.            int result = p.AddTest(10, 20);  
  8.            WriteLine(result);  
  9.            ReadLine();  
  10.        }  
  11.        public int AddTest(int x, int y)  
  12.        {  
  13.            int result = x + y;  
  14.            return result;  
  15.        }  
  16.   
  17.    }  
From the code given above, you can observe, the AddTest function is defined to add two numbers. Now, let's test this function using the live unit test
 
Create a new test project
 
Right click on Solution->Add new project-> choose Test from templates -> Unit Test Project, I named it as AddUnitTestProject.

 
 
Rename the default cs file. I named it as UnitTestAdd.cs.
 
Write the code given below in UnitTestAdd.cs.  
  1. [TestClass]  
  2.    public class UnitTestAdd  
  3.    {  
  4.        [TestMethod]  
  5.        public void TestMethod1()  
  6.        {  
  7.   
  8.            Program _AddTest = new Program();  
  9.            var result = _AddTest.AddTest(10, 20);  
  10.            Assert.IsTrue(result == 30);  
  11.   
  12.        }  
  13.    }   
Run Live Unit test
 
To start Live Unit testing, go to Test-> Live Unit Testing ->Start as shown below.
 
 
 
Click Start to run the Unit test.  
 
 
 
Now, I have edited the code. 
 
UnitTestAdd.cs 
  1. [TestMethod]  
  2.       public void TestMethod1()  
  3.       {  
  4.   
  5.           Program _AddTest = new Program();  
  6.           var result = _AddTest.AddTest(10, 20);  
  7.           Assert.IsTrue(result == 20);  
  8.   
  9.       }  
Once you edit the code, Unit test will start automatically and run in the background, as shown below.


I hope, you have enjoyed this blog. Your valuable feedback, questions and comments about this blog are always welcome.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now