4.1 KiB
Cross-Platform Test Debugging in VS Code
Most test suites in the NuGet codebase are able to run cross-platform on .NET Core. This guide is a walkthrough of how to debug tests on all supported platforms, as well as remotely on Linux. These instructions require VS Code.
Setting up .NET Core
Please follow the documentation on installing the .NET Core SDK for your target operating system and architecture. For the time being, you will want version 3.1 unless you're developing something that requires .NET 5.0.
Setting up VS Code
- Install VS Code for your desired development platform. If debugging remotely, you only need VS Code installed on the machine you're connecting from.
- Install the C# extension.
- In the root of the NuGet repo on your local machine, create a
.vscode/launch.jsonfile, and paste the following into it:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Attach (Test)",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}",
"symbolOptions": {
"searchPaths": [
// Change this path to the test project dll path that you want to debug, and add any additional search paths.
"${workspaceFolder}/test/NuGet.Core.FuncTests/NuGet.Packaging.FuncTest/bin/Debug/netcoreapp5.0"
]
}
}
]
}
Running and Debugging a Test Suite
- Open a terminal in VS Code.
cdinto the path of the test project you want to debug.- Edit the
searchPathsarray in your.vscode/launch.jsonto be the directory containing the assemblies of the test project you're debugging. - Run
dotnet build - Launch the test suite in debug mode:
- On Windows:
$env:VSTEST_HOST_DEBUG=1To set the environment variable.dotnet vstest bin\debug\netcoreapp5.0\My.Test.Project.dll /Tests:<TestFilterHere>
- On OSX and Linux:
VSTEST_HOST_DEBUG=1 dotnet vstest bin/debug/netcoreapp5.0/My.Test.Project.dll /Tests:<TestFilterHere>
- On Windows:
- When the terminal prompts you to attach, go to the debugger tab in VS Code and launch the
.NET Core Attach (Test)configuration, then enter the PID of the process in the terminal. - Once attached, resume execution and then disconnect, letting that part run -- we're not at the actual tests yet.
- Once the first process completes, the terminal will prompt you again to attach to a process. Lanch the attach process again and enter the new PID.
- You are now attached to the running test and can debug it, place breakpoints, inspect stack frames and values, etc. For more information on debugging using VS Code, see its documentation.
Remote Development and Debugging Using VS Code
If you're trying to debug on a remote Linux machine, you can use VS Code to develop and debug on a remote codebase. When doing so, you'll need to run the dotnet commands in the remote terminal, probably in VS Code itself, not on your remote machine.
Running sudo commands on *nix machines without entering a password
In some situations, you might need to run a test suite in sudo mode because the tests are making changes to the local system. In this case, you'll need to use visudo to add a line to your sudoers file:
- Run
sudo visudoand enter your login password. - Use the arrow keys to navigate to the line with your login username.
- If there's no such line, simply add the line instead of editing it (below)
- Press the
ikey to enter "Insert mode". - Use the arrow keys and backspace to erase the line with your username (skip if no such line exists)
- Press
Enterto create a new line. - Enter this as your line:
$your-login-username-here ALL = (ALL) NOPASSWD: ALL - Press
Escapeto leave insert mode. - Type
:wqand pressEnterto exitvisudo.
For more information on vi keybindings, please go to this documentation.