|
| 1 | +--- |
| 2 | +title: Remote Testing in Visual Studio |
| 3 | +description: Learn how to use remote testing in Visual Studio Test Explorer to run tests from remote environments including containers, WSL2, or over SSH connections. This topic covers how to configure remote testing with a testenvironments.json for either local containers, WSL2, or SSH connections. |
| 4 | +ms.date: 08/26/2021 |
| 5 | +ms.topic: how-to |
| 6 | +author: mikejo5000 |
| 7 | +ms.author: mikejo |
| 8 | +manager: jmartens |
| 9 | +ms.technology: vs-ide-test |
| 10 | +monikerRange: '>= vs-2022' |
| 11 | +ms.workload: |
| 12 | + - multiple |
| 13 | +--- |
| 14 | +# Remote Testing (experimental preview) |
| 15 | + |
| 16 | +Remote testing enables developers to connect Visual Studio 2022 to remote environments for running and debugging tests. This functionality is useful for cross-platform developers who deploy code to multiple different target environments such as different Windows or Linux operating systems. For example, normally a developer would have to push changes to a CI pipeline to get feedback from a test running on Linux. With this feature, you can run Linux tests right from Visual Studio by connecting the Test Explorer to a remote environment. |
| 17 | + |
| 18 | +Requirements for using this experimental version of remote testing: |
| 19 | +* Visual Studio 2022 Update 17.0 Preview 3 or later |
| 20 | +* Only available for .NET tests. |
| 21 | + * If you're interested in remote testing support for other languages, please [file a suggestion](/visualstudio/ide/suggest-a-feature) or upvote an existing suggestion. [Supporting C++ remote testing](https://developercommunity.visualstudio.com/t/run-c-unit-tests-on-linux-with-visual-studio/1403357). |
| 22 | +* Currently, the bulk of the provisioning of the environment is left to the user’s specification. The user must install the necessary dependencies in your target environment. For example, if your tests target .NET 5.0, you need to make sure the container has .NET 5.0 installed via your Dockerfile. There may be a prompt to install .NET Core on the remote environment, which is needed to run and discover tests remotely. |
| 23 | +* Plan to monitor your connection status to the remote environment using the Output > Tests pane. For example, if the container was stopped a message will appear in Output > Tests pane. We may not detect all scenarios, so plan to check your output if it looks like the connection was lost. In particular, if the Output pane isn't set to "Test", you may not immediately see the message. If the connection is lost, you can use the environment drop-down in the Test Explorer to set the connection back to your local environment and then select the remote environment again to reinitiate the connection. |
| 24 | + |
| 25 | +## Set up the remote testing environment |
| 26 | + |
| 27 | +Environments are specified using `testenvironments.json` in the root of your solution. The json file structure follows the schema described here: |
| 28 | +```json |
| 29 | +{ |
| 30 | + "version": "1", // value must be 1 |
| 31 | + "environments": [ |
| 32 | + { "name": "<unique name>", ... }, |
| 33 | + ... |
| 34 | + ] |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Local container connections |
| 39 | + |
| 40 | +To connect to a container running locally, you must have [Docker Desktop](https://www.docker.com/products/docker-desktop) on your local machine. Optionally, [enable WSL2 integration](/windows/wsl/install-win10) for better performance. |
| 41 | + |
| 42 | +For a Dockerfile, the environment can be specified in `testEnvironments.json` in the root of your solution. It uses the properties described here. |
| 43 | +```json |
| 44 | + { |
| 45 | + "name": "<name>", |
| 46 | + "localRoot": "<path to local environment>", // optional |
| 47 | + "type": "docker", |
| 48 | + "dockerImage": "<docker image tag>", |
| 49 | + } |
| 50 | +``` |
| 51 | + |
| 52 | +The following example shows `testenvironments.json` for a local container image named \<mcr.microsoft.com/dotnet/core/sdk\>. |
| 53 | +```json |
| 54 | +{ |
| 55 | +"version": "1", |
| 56 | +"environments": [ |
| 57 | + { |
| 58 | + "name": "linux dotnet-core-sdk-3.1", |
| 59 | + "type": "docker", |
| 60 | + "dockerImage": "mcr.microsoft.com/dotnet/core/sdk" |
| 61 | + } |
| 62 | +] |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +The following example shows a Dockerfile for running tests targeting .NET 5.0. The second line makes sure the debugger can connect and run in your container. |
| 67 | +``` |
| 68 | +FROM mcr.microsoft.com/dotnet/core/sdk:5.0 |
| 69 | +
|
| 70 | +RUN wget https://aka.ms/getvsdbgsh && \ |
| 71 | + sh getvsdbgsh -v latest -l /vsdbg |
| 72 | +``` |
| 73 | + |
| 74 | +The container must have a built image on your local machine. You can build a container using the following command (including the "." at the end): `docker build -t <docker image name> -f <path to Dockerfile> .` |
| 75 | + |
| 76 | +### Local WSL2 connections |
| 77 | +To remotely run tests on WSL2, you must [enable WSL2 integration](/windows/wsl/install-win10) on your local machine. |
| 78 | + |
| 79 | +The environment can be specified in `testEnvironments.json` in the root of your solution using the following schema and replacing \<Ubuntu\> with whatever WSL2 Distribution you've installed. |
| 80 | +```json |
| 81 | +{ |
| 82 | +"version": "1", |
| 83 | +"environments": [ |
| 84 | + { |
| 85 | + "name": "WSL-Ubuntu", |
| 86 | + "type": "wsl", |
| 87 | + "wslDistribution": "Ubuntu" |
| 88 | + } |
| 89 | +] |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### SSH connections |
| 94 | + You can add or remove SSH connections in **Tools > Options > Cross Platform > Connection Manager**. Selecting "Add" will allow you to enter the host name, port, and any credentials you need. |
| 95 | + |
| 96 | +The environment can be specified in `testEnvironments.json` in the root of your solution using the following schema and replacing the `\<ssh://user@hostname:22\>` with your SSH remoteUri. |
| 97 | +```json |
| 98 | +{ |
| 99 | +"version": "1", |
| 100 | +"environments": [ |
| 101 | + { |
| 102 | + "name": "ssh-remote", |
| 103 | + "type": "ssh", |
| 104 | + "remoteUri": "ssh://user@hostname:22" |
| 105 | + } |
| 106 | +] |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +## Use the Test Explorer to run and debug remote tests |
| 111 | +* The active environment is selected via a drop-down in the Test Explorer tool bar. Currently, only one test environment can be active at a time. |
| 112 | + |
| 113 | +  |
| 114 | + |
| 115 | +* Once an environment is selected, tests are discovered and run in the new environment. |
| 116 | + |
| 117 | +  |
| 118 | + |
| 119 | +* You can now run your tests inside the remote and debug your tests in environments! |
| 120 | + |
| 121 | +  |
| 122 | + |
| 123 | +* Test Explorer may prompt you to install some missing environment prerequisites and attempt to install missing dependencies. However, the bulk of the provisioning of the remote environment is up to the user’s specification. |
| 124 | + |
| 125 | +## See also |
| 126 | + |
| 127 | +- [Debug unit tests with Test Explorer](../test/debug-unit-tests-with-test-explorer.md) |
| 128 | +- [Run a unit test as a 64-bit process](../test/run-a-unit-test-as-a-64-bit-process.md) |
| 129 | +- [Test Explorer FAQ](test-explorer-faq.md) |
0 commit comments