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.

   Abstract

Microsoft launched Visual Studio 2017 RC on November 16, 2016 and it’s a free trial for all .NET Enthusiasts and developer community. Once a developer downloads the free trial, its only valid for certain period and this article will help you to learn how to check the validity date of VS 2017 RC.

If you have not yet installed the Visual Studio 2017 RC yet, then you might want to read my previous article on Installing Visual Studio 2017 RC

What is the benefit of Free trial?

Release Candidate is a free trail for developers to try the another latest and greatest features of new IDE and tooling around it; and that gives sufficient time to anyone learn and try what’s new in that new shiny tool and when you or your organization is ready to upgrade to a paid licensed version; you can continue to use the greatest and latest.

So when this free trial will end?

Unlike Visual Studio Community edition which is a Free download, Visual Studio 2017 Developer and Enterprise Editions are Free trial.

Free Download

Visual Studio Community is a Free Download and it remains free the whole life time of the application while it’s installed on your machine.

Free Trial

Visual Studio 2017 Professional and Enterprise are NOT FREE but offer Free Trial, which means that Trial will end after certain date and then you will be no longer able to use that product unless you buy or upgrade to a licensed version of that product.

Until when is RC Valid

Well, usually Microsoft Product Teams know well in advance and work towards releasing the product after few months of RC. But to be sure until when your installed Visual Studio 2017 RC is valid, you can follow the steps below.

  1. Launch Visual Studio 2017 RC2.Go to Help menu and click on “About Microsoft Visual Studio”3.From the opened dialog as shown below, click on “License Status”4.Now you will see a dialog as shown below and you can clearly see the final date of the end of Free trial.

Introduction

Microsoft Visual Studio 2017 is the most exciting version of Visual Studio. You can download the RC from https://www.visualstudio.com/vs/visual-studio-2017-rc/

Visual Studio 2017 has a lot of cool features, tool support and best developer experience right from Installation to code to debug to test to deploy (even it’s better if it’s to Azure).

System Requirements

To know minimum requirements for successful installation of Visual Studio 2017 product family, please visit this URL
https://www.visualstudio.com/en-us/productinfo/vs2017-system-requirements-vs

Installation

Run the downloaded .exe and you will see a very unique launch screen as shown below.

Option Selection

Visual Studio 2017 offers a brand new way of choosing the tool set’s. You can choose one of the following:

  • workloads I.e. what exactly you do or want to do.
  • Individual Components I.e. hand pick all components individually. I feel it could be messy unless you want to cherry pick.
  • Language Packs I.e. select language of your choice.

Workloads

Workloads is the easiest and most effective way of installing all components and dependencies of your chosen field of development. For example, web, desktop, cloud, node, data science etc.

This is so much loaded that you can select up to 8 “Web & Cloud” workloads. As shown in the image below.

Individual Components

This is a tricky one and can cause issues if someone really starts to pick and choose options from here. However, this will be perfect choice when you just want to install “.NET Core Runtime” or so.

Language Packs

This enables you to pick language of your choice. Based on your operating System, it will automatically default to a language; as shown in the image below.

Making Selection

Once selection is made; preferably from Workloads tab; you will see that Summary section is populated with all the components which will be installed. If you want you can expand the each listed item under summary and see all the components installed under each item to a very granular level.

Now, Let’s Begin the Installation

I personally feel that this is one of the fastest Visual Studio setup since 1st version I had my hands on back in 1996

 

 

Setup Complete

Visual Studio 2017 RC setup requires you to Restart your PC. You may not want to do that; but I recommend to “Restart” it.

Welcome Visual Studio 2017

Notice the Recently added section and see all the Visual Studio 2017 components.

Check the Installed .NET Frameworks

Below image shows that all the .NET Frameworks are successfully installed. Which includes .NET Framework until 4.6.x and .NET 1.0 Preview 3. You can run commands as shown below to check those in any machine.

“dotnet –version” is a very handy command to be executed from Developer Command Prompt to identify if you have .NET Core or not.

Visual Studio Installer

In the installed items, there is an item listed at the very end “Visual Studio Installer” this is to Modify, Repair, Uninstall etc.

Let’s Fire-up Visual Studio 2017

Here comes the Most Awaited IDE