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 cod 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 above mentioned problem statement, there is no efficient and developer friendly solution which a traditional unit tests 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 empower 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).

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.

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.

Let’s begin with Red, Refactor, Green

As 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:

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

Now, if you will run the Tests again, by clicking “Run All” option in the Test Explorer, then you shall observe that 1/3 Test case is passing, as shown in the image below.

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 developer will change 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.

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 Live Unit Testing tool from the setup menu, and selecting 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.

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

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, you 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

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. The  symbol means that all code statements in this function or block are covered by the Unit Tests.

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 test. I.e. no unit test case(s) exist for these code blocks. You may want to take it to next level by taking code from my GitHub repo, and expand 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.

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.

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 a developer is refactoring the code.