|
| 1 | +--- |
| 2 | +title: Live Unit Testing |
| 3 | +description: Learn about Live Unit Testing during application development, including supported frameworks and how to configure Live Unit Testing. |
| 4 | +ms.date: 04/07/2020 |
| 5 | +ms.topic: how-to |
| 6 | +helpviewer_keywords: |
| 7 | +- Live Unit Testing |
| 8 | +author: mikejo5000 |
| 9 | +ms.author: mikejo |
| 10 | +ms.workload: |
| 11 | +- dotnet |
| 12 | +monikerRange: 'vs-2019' |
| 13 | +--- |
| 14 | +# How to configure and use Live Unit Testing |
| 15 | + |
| 16 | + [!INCLUDE [Visual Studio](~/includes/applies-to-version/vs-windows-only.md)] |
| 17 | + |
| 18 | +As you're developing an application, Live Unit Testing automatically runs any impacted unit tests in the background and presents the results and code coverage in real time. As you modify your code, Live Unit Testing provides feedback on how your changes impacted existing tests and whether the new code you've added is covered by one or more existing tests. This gently reminds you to write unit tests as you're making bug fixes or adding new features. |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> Starting in Visual Studio 2022 version 17.3, Live Unit Testing has been updated. For new content, see [link](./live-unit-testing.md). |
| 22 | +
|
| 23 | +> [!NOTE] |
| 24 | +> Live Unit Testing is available for C# and Visual Basic projects that target .NET Core or .NET Framework in the Enterprise edition of Visual Studio. |
| 25 | +
|
| 26 | +When you use Live Unit Testing for your tests, it persists data about the status of your tests. Using persisted data allows Live Unit Testing to offer superior performance while running your tests dynamically in response to code changes. |
| 27 | + |
| 28 | +## Supported test frameworks |
| 29 | + |
| 30 | +Live Unit Testing works with the three popular unit testing frameworks listed in the following table. The minimum supported version of their adapters and frameworks is also shown. The unit testing frameworks are all available from NuGet.org. |
| 31 | + |
| 32 | +|Test Framework |Visual Studio Adapter minimum version |Framework minimum version | |
| 33 | +|---------|---------|---------| |
| 34 | +|xUnit.net |xunit.runner.visualstudio version 2.2.0-beta3-build1187 |xunit 1.9.2 | |
| 35 | +|NUnit |NUnit3TestAdapter version 3.5.1 |NUnit version 3.5.0 | |
| 36 | +|MSTest |MSTest.TestAdapter 1.1.4-preview |MSTest.TestFramework 1.0.5-preview | |
| 37 | + |
| 38 | +If you have older MSTest-based test projects that reference Microsoft.VisualStudio.QualityTools.UnitTestFramework, and you don’t wish to move to the newer MSTest NuGet packages, upgrade to Visual Studio 2019 or Visual Studio 2017. |
| 39 | + |
| 40 | +In some cases, you may need to explicitly restore the NuGet packages referenced by a project in order for Live Unit Testing to work. You can do this either by doing an explicit build of the solution (select **Build** > **Rebuild Solution** from the top-level Visual Studio menu) or by restoring packages in the solution (right-click on the solution and select **Restore NuGet Packages**). |
| 41 | + |
| 42 | +## Configure |
| 43 | + |
| 44 | +Configure Live Unit Testing by selecting **Tools** > **Options** from the top-level Visual Studio menu bar, and then selecting **Live Unit Testing** in the left pane of the **Options** dialog. |
| 45 | + |
| 46 | +> [!TIP] |
| 47 | +> After Live Unit Testing is enabled (see the next section, [Start, pause, and stop Live Unit Testing](#start-pause-and-stop)), you can also open the **Options** dialog by selecting **Test** > **Live Unit Testing** > **Options**. |
| 48 | +
|
| 49 | +The following image shows the Live Unit Testing configuration options available in the dialog: |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +The configurable options include: |
| 54 | + |
| 55 | +- Whether Live Unit Testing pauses when a solution is built and debugged. |
| 56 | + |
| 57 | +- Whether Live Unit Testing pauses when a system's battery power falls below a specified threshold. |
| 58 | + |
| 59 | +- Whether Live Unit Testing runs automatically when a solution is opened. |
| 60 | + |
| 61 | +- Whether to enable debug symbol and XML documentation comment generation. |
| 62 | + |
| 63 | +- The directory in which to store persisted data. |
| 64 | + |
| 65 | +- The ability to delete all persisted data. This is useful when Live Unit Testing is behaving in an unpredictable or unexpected way, which suggests that the persisted data has become corrupted. |
| 66 | + |
| 67 | +- The interval after which a test case times out. The default is 30 seconds. |
| 68 | + |
| 69 | +- The maximum number of test processes that Live Unit Testing creates. |
| 70 | + |
| 71 | +- The maximum amount of memory that Live Unit Testing processes can consume. |
| 72 | + |
| 73 | +- The level of information written to the Live Unit Testing **Output** window. |
| 74 | + |
| 75 | + Options include no logging (**None**), error messages only (**Error**), error and informational messages (**Info**, the default), or all detail (**Verbose**). |
| 76 | + |
| 77 | + You can also display verbose output in the Live Unit Testing **Output** window by assigning a value of "1" to a user-level environment variable named `VS_UTE_DIAGNOSTICS`, and then restarting Visual Studio. |
| 78 | + |
| 79 | + To capture detailed MSBuild log messages from Live Unit Testing in a file, set the `LiveUnitTesting_BuildLog` user-level environment variable to the name of the file to contain the log. |
| 80 | + |
| 81 | +## Start, pause, and stop |
| 82 | + |
| 83 | +To enable Live Unit Testing, select **Test** > **Live Unit Testing** > **Start** from the top-level Visual Studio menu. When Live Unit Testing is enabled, the options available on the **Live Unit Testing** menu change from a single item, **Start**, to **Pause** and **Stop**: |
| 84 | + |
| 85 | +- **Pause** temporarily suspends Live Unit Testing. |
| 86 | + |
| 87 | + When Live Unit Testing is paused, coverage visualization does not appear in the editor, but all the data that was collected is preserved. To resume Live Unit Testing, select **Continue** from the Live Unit Testing menu. Live Unit Testing does the necessary work to catch up with all the edits that have been made while it was paused and updates the glyphs appropriately. |
| 88 | + |
| 89 | +- **Stop** completely stops Live Unit Testing. Live Unit Testing discards all data that it has collected. |
| 90 | + |
| 91 | +> [!NOTE] |
| 92 | +> If you start Live Unit Testing in a solution that does not include a unit test project, the **Pause** and **Stop** options appear on the **Live Unit Testing** menu, but Live Unit Testing does not start. The **Output** window displays a message that begins, "No supported test adapters are referenced by this solution...". |
| 93 | +
|
| 94 | +At any time, you can temporarily pause or completely stop Live Unit Testing. You may want to do this, for example, if you're in the middle of a refactoring and know that your tests will be broken for a while. |
| 95 | + |
| 96 | +## View coverage visualization |
| 97 | + |
| 98 | +After it's enabled, Live Unit Testing updates each line of code in the Visual Studio editor to show you whether the code you're writing is covered by unit tests and whether the tests that cover it are passing. The following image shows lines of code with both passing and failing tests, as well as lines of code that are not covered by tests. Lines decorated with a green "✓" are covered only by passing tests, lines decorated with a red "x" are covered by one or more failing tests, and lines decorated by a blue "➖" are not covered by any test. |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +Live Unit Testing coverage visualization is updated immediately when you modify code in the code editor. While processing the edits, visualization changes to indicate that the data is not up-to-date by adding a round timer image below the passing, failing, and not covered symbols, as the following image shows. |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +## Get information about test status |
| 107 | + |
| 108 | +By hovering over the succeeded or failed symbol in the code window, you can see how many tests are hitting that line. To see the status of the individual tests, select the symbol: |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +In addition to providing the names and result of tests, the tooltip lets you rerun or debug the set of tests. If you select one or more of the tests in the tooltip, you can also run or debug just those tests. This allows you to debug your tests without having to leave the code window. When debugging, in addition to observing any breakpoints you may have already set, program execution pauses when the debugger executes an <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> method that returns an unexpected result. |
| 113 | + |
| 114 | +When you hover over a failed test in the tooltip, it expands to provide additional info about the failure, as shown in the following image. To navigate directly to a failed test, double-click it in the tooltip. |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +When you navigate to the failed test, Live Unit Testing visually indicates in the method signature the tests that have: |
| 119 | + |
| 120 | +- passed (indicated by a half-full beaker along with a green "✓") |
| 121 | +- failed (a half-full beaker along with a red "🞩") |
| 122 | +- are not involved in Live Unit Testing (a half-full beaker along with a blue "➖") |
| 123 | + |
| 124 | +Non-test methods are not decorated with a symbol. The following image illustrates all four types of methods. |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +## Diagnose and correct test failures |
| 129 | + |
| 130 | +From the failed test, you can easily debug the product code, make edits, and continue developing your application. Because Live Unit Testing runs in the background, you don't have to stop and restart Live Unit Testing during the debug, edit, and continue cycle. |
| 131 | + |
| 132 | +For example, the test failure shown in the previous image was caused by an incorrect assumption in the test method that non-alphabetic characters return `true` when passed to the <xref:System.Char.IsLower%2A?displayProperty=fullName> method. After you correct the test method, all the tests should pass. You don't have to pause or stop Live Unit Testing. |
| 133 | + |
| 134 | +## Live Unit Testing window |
| 135 | + |
| 136 | +**Live Unit Testing**, similar to **Test Explorer**, provides an interface that lets you run and debug tests and analyze test results. When Live Unit Testing is enabled, the status of unit tests in **Test Explorer** is updated immediately. You don't need to explicitly run the unit tests. When Live Unit Testing is not enabled or is stopped, **Live Unit Testing** displays the status of unit tests the last time a test was run. After you restart Live Unit Testing, a source code change is required to rerun the tests. |
| 137 | + |
| 138 | +> [!TIP] |
| 139 | +> Start Live Unit Testing by selecting **Test** > **Live Unit Testing** > **Start** from the top-level Visual Studio menu. You can also open the **Live Unit Testing** window using **View** > **Other Windows** > **Live Unit Testing Window**. |
| 140 | +
|
| 141 | +You may notice in the **Live Unit Testing** window that some tests are faded out. For example, when you stop and restart Live Unit Testing, the **Live Unit Testing** window fades out all the tests, as the following image shows. Faded-out test results indicate that the test was not a part of the latest Live Unit Test run. Tests only run when a change to the test or the test's dependencies is detected. If there is no change, it avoids unnecessarily running the test. In this case, the grayed out test result is still "up-to-date" though it was not a part of the latest run. |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +You can rerun any tests that appear faded by making a code change. |
| 146 | + |
| 147 | +There are some differences between Live Unit Testing automatically running and updating test results and explicitly running tests from **Test Explorer**. These differences include: |
| 148 | + |
| 149 | +- Running or debugging tests from the Test Explorer window runs regular binaries, whereas Live Unit Testing runs instrumented binaries. |
| 150 | +- Live Unit Testing does not create a new application domain to run tests, but rather runs tests from the default domain. Tests run from the **Test Explorer** window do create a new application domain. |
| 151 | +- Live Unit Testing runs tests in each test assembly sequentially. In the **Test Explorer** window, you can choose to run multiple tests in parallel. |
| 152 | + |
| 153 | +## Large solutions |
| 154 | + |
| 155 | +If your solution has 10 or more projects, Visual Studio displays the following dialog when you: |
| 156 | + |
| 157 | +- start Live Unit Testing and there is no persisted data |
| 158 | +- select **Tools** > **Options** > **Live Unit Testing** > **Delete Persisted Data** |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +The dialog warns you that dynamic execution of large numbers of tests in large projects can severely impact performance. If you select **OK**, Live Unit Testing executes all tests in the solution. If you select **Cancel**, you can select the tests to execute. The following section explains how to do this. |
| 163 | + |
| 164 | +## Include and exclude test projects and test methods |
| 165 | + |
| 166 | +For solutions with many test projects, you can control which projects and individual methods in a project participate in Live Unit Testing. For example, if you have a solution with hundreds of test projects, you can select a targeted set of test projects to participate in Live Unit Testing. There are a number of ways to do this, depending on whether you want to exclude all the tests in the project or solution, include or exclude most tests, or exclude individual tests. Live Unit Testing saves include/exclude state as a user setting and remembers it when a solution is closed and reopened. |
| 167 | + |
| 168 | +### Exclude all tests in a project or solution |
| 169 | + |
| 170 | +To select the individual projects in unit tests, do the following after Live Unit Testing is started: |
| 171 | + |
| 172 | +1. Right-click the solution in **Solution Explorer** and choose **Live Unit Testing** > **Exclude** to exclude the entire solution. |
| 173 | +1. Right-click each test project that you'd like to include in the tests and choose **Live Unit Testing** > **Include**. |
| 174 | + |
| 175 | +### Exclude individual tests from the code editor window |
| 176 | + |
| 177 | +You can use the code editor window to include or exclude individual test methods. Right-click on the signature of the test method in the code editor window, and then select one of the following options: |
| 178 | + |
| 179 | +- **Live Unit Testing** > **Include \<selected method>** |
| 180 | +- **Live Unit Testing** > **Exclude \<selected method>** |
| 181 | +- **Live Unit Testing** > **Exclude All But \<selected method>** |
| 182 | + |
| 183 | +### Exclude tests programmatically |
| 184 | + |
| 185 | +You can apply the <xref:System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute> attribute to programmatically exclude methods, classes, or structures from reporting their coverage in Live Unit Testing. |
| 186 | + |
| 187 | +Use the following attributes to exclude individual methods from Live Unit Testing: |
| 188 | + |
| 189 | +- For xUnit: `[Trait("Category", "SkipWhenLiveUnitTesting")]` |
| 190 | +- For NUnit: `[Category("SkipWhenLiveUnitTesting")]` |
| 191 | +- For MSTest: `[TestCategory("SkipWhenLiveUnitTesting")]` |
| 192 | + |
| 193 | +Use the following attributes to exclude an entire assembly of tests from Live Unit Testing: |
| 194 | + |
| 195 | +- For xUnit: `[assembly: AssemblyTrait("Category", "SkipWhenLiveUnitTesting")]` |
| 196 | +- For NUnit: `[assembly: Category("SkipWhenLiveUnitTesting")]` |
| 197 | +- For MSTest: `[assembly: TestCategory("SkipWhenLiveUnitTesting")]` |
| 198 | + |
| 199 | +## See also |
| 200 | + |
| 201 | +- [Code testing tools](https://visualstudio.microsoft.com/vs/testing-tools/) |
| 202 | +- [Live Unit Testing blog](https://devblogs.microsoft.com/visualstudio/live-unit-testing-in-visual-studio-2017-enterprise/) |
| 203 | +- [Live Unit Testing FAQ](live-unit-testing-faq.yml) |
| 204 | + |
0 commit comments