Inside Out - Live Unit Testing With VS 2017

What is Live Unit Testing?

Live Unit Testing is a brand-new technology, made available in Visual Studio 2017 version 15.3 or above. Live Unit Testing enables the IDE to execute unit tests automatically in real time without code being built, and as you make changes to the code.

Which frameworks and tooling support Live Unit Testing?

Live Unit Testing is available for C# and Visual Basic projects using MSTest, NUnit, or xUnit Test frameworks that target the .NET Core or .NET Framework in the Enterprise Edition of Visual Studio 2017. 

Problem Statement

Unit Tests and code have a very close relationship, and both depend heavily on each other; i.e. a change in source code must impact the unit test(s) code for what was being tested. Similarly, a change in a unit test must impact the code coverage of the source code, which is being exercised via unit tests.

In the light of the above-mentioned problem statement, there is no efficient and developer friendly solution which a traditional unit test can solve. Moreover, validating code changes through unit tests may quickly turn out to be a tedious task, as you must run all the unit tests from Test Explore after every code change.

Benefits of Live Unit Testing

It empowers the developers to refactor and change their code with greater confidence. Live Unit Testing graphically depicts code coverage in real time and provides a quick visual view of the code coverage, and which code statements are passing in unit tests. As per the name “Live Unit Test”, you don’t have to build the code and run the tests again via Test Explorer to validate the changes. I.e. soon after code changes are made, unit tests will be automatically executed to reflect the impact (pass, fail).

A quick look at a traditional Unit Test

As explained in my previous article Inside Out : TDD using C# there is always a system under test or code which needs to be exercised via unit tests. This code is available at my GitHub, and this code will work just fine for Unit Test, and Live Unit Test (if you have the required Visual Studio 2017) purposes.

The code below, shows unit tests written for Account.cs class’s IsAccountActive() function.

Visual Studio

 

What is being tested?

As shown in the image above, Account.cs class’s IsAccountActive() method is being exercised. I.e. Account.cs is our System Under Test.

Visual Studio

Let’s begin with Red, Refactor, Green

This code is showcasing Test Driven Development, i.e. write a failing test, and then write code to make that test pass, and so on.

First, let’s write/refactor code to make TestAccountStatus_Active_Success() test case pass. To do so, copy the code from TestCase#1 code snippet from TextFile1.txt (included in the UnitTestBankApplication at my github

Account.cs code when TestAccountStatus_Active_Success() was failing,

  1. public bool IsAccountActive(string accNumber)  
  2. {  
  3.    return accountStatus;  
  4. }  

Account.cs code changes to make TestAccountStatus_Active_Success() pass,

  1. public bool IsAccountActive(string accNumber)  
  2. {  
  3.     if (accNumber != null)  
  4.     {  
  5.         accountStatus = true;  
  6.     }  
  7.     else  
  8.     {  
  9.         accountStatus = false;  
  10.     }  
  11.   
  12.     return accountStatus;  
  13. }  

Now, if you will run the tests again, by clicking the “Run All” option in the Test Explorer, then you shall observe that  of the1/3 test cases pass, as shown in the image below.

Visual Studio

 

But what exactly is the problem here?

Unit Test code or source code which is being tested doesn’t explicitly tell which code lines are being covered, not covered, or passed via unit tests; i.e. whenever the developer changes the code, test cases need to be run over and over to identify the impact, and that is without any visual clue in the code files. Hence, with traditional unit testing, a developer can’t clearly tell which code statements are covered, or not covered, through passed or failing unit tests.

Turn-on Live Unit Testing

Navigate to the Test menu, expand Live Unit Testing, and then click on Start, as shown in the image below.

Visual Studio

 

If you don’t see this option then you might not have the appropriate version of Visual Studio 2017 Enterprise Edition, which offers the Live Unit Testing feature. However, if you have Visual Studio 2017 enterprise edition, and you don’t see this option, then you can add the Live Unit Testing tool from the setup menu, and select it from the Individual components tab.

Live Unit Testing in Action

As soon as you have started the Live Unit Testing, your IDE will instantly start tracking the code changes in the background, as shown by X, ─, and symbols on the left of code statements in the IDE, as shown in the image below.

Visual Studio

 

As of now, there is only one test passing, let’s take the Test Case#2 code from TestFile1.txt,

  1. if (accNumber != null)  
  2.         {  
  3.             accountStatus = true;  
  4.         }  
  5.         else  
  6.             if (String.IsNullOrWhiteSpace(accNumber))  
  7.             {  
  8.                 throw new ArgumentException("Can't Pass Null account number");  
  9.             }  
  10.             else  
  11.             {  
  12.                 accountStatus = false;  
  13.             }  
  14.   
  15.         return accountStatus;  

and put it in Account.cs class’s IsAccountActive() function, and soon after putting the code in there, without building the code explicitly, you shall observe the changes, and notice that now, two tests are passing. Alongside, you shall observe the changes to the code line symbols X, ─, and on the left.

Now, you may want to work on passing the third unit test, and to make that happen as per TDD guidelines, your code should refactor the code logic to accomplish that. Put the Test Case#3 code from TestFile1.txt, into Account.cs class’s IsAccountActive() function,

  1. if (String.IsNullOrWhiteSpace(accNumber))  
  2.      {  
  3.          throw new ArgumentException("Can't Pass Null account number");  
  4.      }  
  5.      else  
  6.      {  
  7.          accountStatus = true;  
  8.      }  
  9.   
  10.  return accountStatus;  

As soon as the code is refactored, again without any explicit build and running tests from Test Explorer, Live Unit Testing observed the code changes and ran the tests. As shown in the image below, now all three unit tests are passing, and all code lines in Account.cs class’s IsAccountActive() shows symbol on their left.  symbol means that all code statements in this function or block are covered by the Unit Tests.

Visual Studio

 

Now, what’s next?

As you can see in the image above towards the bottom-half, ─ symbol in front of other code blocks, which means that these code blocks are covered by 0 tests; i.e. no unit test case(s) exist for these code blocks. You may want to take it to the next level by taking code from my GitHub repo and expanding it.

Pause or Stop Live Unit Testing

Once Live Unit Testing has been started, now it can be either paused or stopped. You can navigate to the Test menu, select Live Unit Testing, and then choose either Pause or Stop, as shown in the image below. Based on the choice you make; Live Unit Testing will be either paused or stopped completely.

Visual Studio

Excluding individual Unit Tests from Live Unit Testing

Ideally, all the tests in all the projects will be covered once Live Unit Testing has been started.

But, you can use the following attributes to specify in the source code that you want to exclude targeted test methods from Live Unit Testing,

  • For MSTest - [TestCategory("SkipWhenLiveUnitTesting")]
  • For xUnit - [Trait("Category", "SkipWhenLiveUnitTesting")]
  • For NUnit - [Category("SkipWhenLiveUnitTesting")]
Include/Exclude Unit Test Projects or Test class files

Live Unit Testing is IDE level, and it works for all test projects and all class files which are part of a test project. To include/exclude either a test project or a test class file, you can use the context menu and choose Live Unit Testing, Include or Exclude options. The image below shows these options on the UnitTestBankApplication Test Project, and you may want to try it on test class file as well.

Visual Studio

Wrapping up

Live Unit Testing appears to be a milestone in fast-paced, technology-heavy software development field. This certainly doesn’t take away the importance of knowing Unit Test fundamentals, rather, it enforces the TDD (Test Driven Development) mindset in an effective, and productive manner to the developer community, by showing visual clues in the IDE code window, and running the tests while the developer is refactoring the code.


Similar Articles