Live Unit Testing Preview: Better and Faster
Live Unit Testing is the automatic test running feature in Visual Studio Enterprise. As you make code changes, Live Unit Testing detects what tests are impacted and runs them in the background. This way you always know if the tests covering a certain line of code are passing or failing. You can see what your code coverage is for each line of your product code at a glance. You get instant feedback in the editor with pass/fail icons in the margin and you can easily run or debug the impacted tests from the Live Unit Testing icons. Live Unit Testing will help you catch if new code changes cause tests to fail as you develop so you won’t forget to do a test run before pushing changes ever again.
Live Unit Testing involves building impacted projects in order to run tests. A ton of the work in this update was focused on making those builds scoped to only what is needed, build projects in parallel, and scale better.
What’s new and how can I get it?
In Visual Studio Enterprise 2022 version 17.2 Preview 2 and later, you can enable the new Live Unit Testing experience in Tools > Options > Preview Features. Enable the option New build experience for Live Unit Testing (requires restart). If you have any issues with the new experience you can disable this option, but please report it so we can make the experience better.
Note: In Visual Studio Enterprise 2022 version 17.3 and later, there is no need to click the checkbox as this is out of preview and is the default experience.
Faster scoped builds
Live Unit Testing now only builds projects that are relevant to the edits you make and that match the tests that you have included in Live Unit Testing.
Live Unit Testing can now build projects in parallel, further speeding up your test runs as you make edits.
Better default behavior
For large solutions, we encourage users to select only a subset of tests in Live Unit Testing. This leads to a shorter build time, less memory footprint, and a shorter test run and coverage analysis time. Live Unit Testing has also been optimized now to only build and run the tests that you have included.
Large solution support
With all the improvements made, Live Unit Testing can now work against larger solutions. For example, Live Unit Testing now works on the C# Compilers.sln. In the gif below, tests from one class have been included in Live Unit Testing.
More dependable builds
The build mechanism that Live Unit Testing now uses is closer to how you would use msbuild on the command-line.
Wider solution support
With dependable builds and support for large solutions, Live Unit Testing now supports a wide variety of solutions out of the box.
Configuration and setup
We now offer a configuration experience for Live Unit Testing through a wizard. When you start Live Unit Testing for the first time in a solution, you will see the below wizard UI.
Note: In Visual Studio Enterprise 2022 version 17.3 and later, the option to generate PDBs is enabled by default and no longer configurable in the Live Unit Testing configuration wizard.
Live Unit Testing needs to instrument your projects on building to determine test coverage so that when you make an edit in a file, it knows which tests to run. To ensure that your regular dev workflow remains unaffected, Live Unit Testing operates on a duplicate private workspace. Live Unit Testing utilizes the Windows Projected File System optional Windows feature to maintain this private workspace.
Note: Windows Projected File System should be enabled automatically when Visual Studio is installed. But in case it happens to be disabled, you can enable it via the “Turn Windows features on or off” dialog or using PowerShell. See docs.
Live Unit Testing now supports a .lutignore file (similar to a .gitignore) which you can use to indicate the set of files that need to be synchronized from the repository root to the Live Unit Testing’s private workspace. The main purpose of the .lutignore file is to ensure that build artifacts from your regular build output folders are excluded from this synchronization (so that the private workspace folder will only contain build artifacts that have been instrumented for Live Unit Testing).
You have the option to use your repository’s existing gitignore file for this. Alternatively, if you select the “Common artifacts eg. bin/, obj/, *.dll, *.obj” or “Custom” option in the dropdown shown below, Live Unit Testing will create one for you.
To access the wizard again, you can go to Test -> Live Unit Testing -> Configure Live Unit Testing for solution. (You will need to stop Live Unit Testing to enable this command.)
Note: The settings configured above are persisted in a file with extension .lutconfig that is placed next to your solution. Both the .lutconfig file as well as the .lutignore file can be checked in into your repository.
Previously, if you had a test that was hanging or timing out, you would have to wait for the default timeout time until your new edits could be tested. Live Unit Testing now offers a way to cancel the in-progress test run if you have made newer code edits. This makes it easier for you to get feedback on your latest code change if your tests take a long time to run.
Live Unit Testing also offers a way to cancel previous test runs on File Save. The Option can be found in Tools -> Options -> Live Unit Testing -> “Restart test run on document save”
The Live Unit Testing window now indicates running tests with a spinner, similar to the Test Explorer window.
Live Unit Testing also has new keyboard shortcuts to include tests from the editor (e.g., Ctrl + R, I will include the test selected in the editor).
Thanks for trying it out and filing bugs!
Please try out Live Unit Testing and file feedback with the built-in Report a Problem tool in Visual Studio. Note, this feature is only available in Visual Studio Enterprise. Documentation on the new Live Unit Testing experience will be coming soon.