Live Unit Testing In Visual Studio Enterprise 2017

Live Unit Testing

Live Unit Testing automatically runs the impacted unit tests in the background as you edit code, and visualizes the results and code coverage, live in the editor.
 
This feature is available in the Enterprise edition of Visual Studio 2017 and for C# and VB projects only. The Live Unit Test framework runs in three test frameworks, as given below with supporting version.
  1. xUnit  version > 2.0
  2. NUnit  version > 3.5.0 and  NUnit3TestAdapter > 3.5.1
  3. MSTest  : TestFramework Version > 1.0.5-preview and MSTest.TestAdapter version > 1.1.4-preview 
In this article, I am using MSTest Framework to implement the LUT.
 
Prerequisites
  1. Install Live Unit Testing Component. For this, run the VS Setup -> Individual components -> Development activities -> Live Unit Testing -> Install it.



  2. Create a sample console application ( Ex : UnitTestSample).
  3. Install the MS Test framework from NuGet Package into your sample project (that is created in step 2).

    Go to Tools -> NuGet Package Manager -> Package Manager -> Console

    Execute the below commands one by one.

    a .Install-Package MSTest.TestFramework -Pre
    b. Install-Package MSTest.TestAdapter -Pre



  4. Write the below sample code to test into Live Unit Testing feature.
    1. public static class MyMath {  
    2.     public static int Add(int value1, int value2) {  
    3.         return value1 + value2;  
    4.     }  
    5.   
    6.     public static int Sub(int value1, int value2) {  
    7.         return value1 - value2;  
    8.     }  
    9. }  
  5. First, we have to create the Unit Test project in order to execute the LUT feature.

    Right click "MyMath" class & create a Unit Test Project.



  6. In Unit Test function, write the below sample code. If the number is equal to "5", the Unit Test will pass; otherwise it will fail.
    1. public class MyMathTests {  
    2.     [TestMethod()]  
    3.     public void AddTest() {  
    4.         var result = MyMath.Add(2, 5);  
    5.         Assert.IsTrue(5 == result);  
    6.     }  
    7.   
    8.     [TestMethod()]  
    9.     public void SubTest() {  
    10.         Assert.Fail();  
    11.     }  
    12. }  
  7. Start Live unit Testing.

    Goto Test Menu -> Live Unit Testing -> Start .

    Live Unit Test starts running in the background.



  8. Once LUT starts running, the status will be updated into the Visual Studio editor, a few seconds. (In background, the Live Unit Testing runs the code and updates the status).



  9. Edit the source code. Automatically, Live Unit Testing executes the code and updates the status in the VS Editor.


Hope you understood how to implement the Live Unit Test feature.


Similar Articles