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. Program p = new Program();
  6. int result = p.AddTest(10, 20);
  7. WriteLine(result);
  8. ReadLine();
  9. }
  10. public int AddTest(int x, int y)
  11. {
  12. int result = x + y;
  13. return result;
  14. }
  15. }
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. Program _AddTest = new Program();
  8. var result = _AddTest.AddTest(10, 20);
  9. Assert.IsTrue(result == 30);
  10. }
  11. }
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. Program _AddTest = new Program();
  5. var result = _AddTest.AddTest(10, 20);
  6. Assert.IsTrue(result == 20);
  7. }
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.