Live Unit Testing With Visual Studio 2017

Introduction 

Visual Studio 2017 v15.3 or higher versions support Live Unit Testing. This feature executes the already-written test cases automatically as a programmer makes the code changes. Live Unit Testing helps a developer to do the following:
  1. It executes all the impacted Test Cases as a programmer modifies the code and makes sure changes do not break Unit Test Cases.
  2. Through the Visual Studio editor, the programmer sees the code coverage in a realistic view.
  3. It also indicates which line is covered or not using Live Unit Testing.
  4. Live Unit Testing is supported in  both .NET Framework and .NET Core.

Enabling

Live Unit Testing is only available in Visual Studio 2017 with the Enterprise edition. We can enable the Live Unit Testing by going to the following menus - Test > Live Unit Testing > Start.

After enabling the Live Unit Testing for a project, Live Unit Testing shows options as Start, Pause, Stop and Reset.

Configuration

The default configuration can be changed from the Visual Studio Menu Tool > Options > Live Unit Testing > General. We can configure the Live Unit Testing for the Battery and Memory usages, Testcase Timeout, and Logging Level can be set as needed.
 
Live Unit Testing With Visual Studio 2017 

Setup Project Solution

  1. Open the Visual Studio 2017.
  2. Create one Windows Library Project as “ClassLibrary1” and Change the “Class1.cs” to the “AreaCalculator.cs”.
  3. Right click on the solution and add a Unit Test Project as “UnitTestProject1” and change the “UnitTest1.cs” to the “AreaCalculatorTest.cs”
  4. Add the Reference of the “ClassLibrary1” project to the “UnitTestProject1”.
  5. Add the using statement “using ClassLibrary1;” to the “AreaCalculatorTest.cs” in the using statement section.
  6. Build the Solution.

Demo

Open the AreaCalculator.cs and add the code as per the below screenshot.

Open the AreaCalculatorTest.cs and paste the below code. I have not added the Test Method for the triangle’s area calculation and incorrectly calculated the Test Method of the square’s area, just to show some behavior of Live Unit Testing.

Replace the below code in the AreaCalculatorTest.cs file

Now start the Live Unit Testing from the Test menu, then an automatic background process triggers and we see an icon in-line with code. Description of each icon is mentioned here

Live Unit Testing With Visual Studio 2017Green Tick Mark represents that this Method is covered for the unit testing.

Live Unit Testing With Visual Studio 2017Red Cross Mark represents that this method is covered for the unit testing, but the test case failed during execution.

Live Unit Testing With Visual Studio 2017Blue Negative Mark represent that this method is not covered for the unit testing so a test case needs to be created.

Below screenshots represent each icon of the Live Unit Testing

Icon’s in AreaCalculator.cs

Live Unit Testing With Visual Studio 2017 

Icon’s in AreaCalculatorTest.cs

Live Unit Testing With Visual Studio 2017 

So, with the help of Live Unit Testing of Visual Studio, a developer will know about the impact of the changes he is making, and if it's covered for unit testing or not. This is an awesome feature of Visual Studio 2017.


Similar Articles