Visual Studio 2017 has a new feature called Live Unit Testing. Live Unit Testing is currently in the Enterprise edition of Visual Studio 2017 and it’s available for C# and VB projects that target the .NET Framework. This Live Unit Testing automatically runs the impacted unit tests in the background as we edit code, and visualizes the results and code coverage live, in the editor, in real-time.
In this blog, we will discuss how Live Unit Test in Visual Studio 2017 works.
How to start Live Unit Testing
Enabling Live Unit Testing is as simple as going to the Test command at the top-level menu bar and starting it as shown in image below.
Live Unit Testing works with three popular unit testing frameworks: MSTest, xUnit and NUnit. When using these, we will need to ensure that the adapters and frameworks meet or exceed the minimum versions given below:
- For xUnit we will need xunit.runner.visualstudio version 2.2.0-beta3-build1187 and xunit 2.0 (or higher versions)
- For NUnit we will need NUnit3TestAdapter version 3.5.1 and NUnit version 3.5.0 (or higher versions)
- For MSTest we will need MSTest.TestAdapter 1.1.4-preview and MSTest.TestFramework 1.0.5-preview (or higher versions)
Live Unit Testing experience
Once we enabled Live Unit Test, its helps us to quickly see whether the code we are writing is covered and if the tests that covers it are passing, without leaving the editor. Unit test results and coverage visualizations appear on a line-by-line basis in the code editor as shown in sample image below:
Note: The dash indicates that the code does not have any test coverage. The red X signifies the line was executed by at least one unit test and failed. The green check mark signifies that the code was executed and any tests that ran, passed.
The live feedback also serves to notify us instantly if thee change has broken the program – if inline visualizations shift from green “✓”s to red “×”s, we know we broke one or more tests.
At any point in time we can hover over the “✓” or “×” to see how many tests are hitting the given line as seen in image below.
We can click on the check or “×” to see what tests are hitting the given line as shown in image below.
When hovering over the failed test in the tool tip, it expands to provide additional info to give more insight into the failure as shown in image below.
At any time, we can temporarily pause or completely stop live unit testing; for example, when we are in the middle of a refactoring and we know that our tests will be broken for a while. It is as simple as going to the Test command in top level menu bar and clicking the desired action, as shown below.
Live Unit Testing will improve our developer productivity, test coverage and quality of software.