How can I debug my .NET Core unit tests using Visual Studio with WSL 2?

Viewed 1557

With Visual Studio, I can debug my .NET Core applications running in Windows and WSL 2. For WSL 2, I use this extension. This is working fine. I can also debug my unit tests in Windows. This is working fine too. However, I do not know how how to debug my unit tests with Visual Studio running in WSL 2. I hope someone can help me and explain how I can debug not just my console apps and web apps in WSL 2, but also my unit tests. Please let me know.

2 Answers

Update 2022 Feb - VS 2022

Run on a win 10 dev machine

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Reboot!

Run on the dev machine

wsl --set-default-version 2
wsl --install -d Ubuntu

Run in your new wsl machine

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb 
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update && sudo apt upgrade -y
sudo apt install apt-transport-https git 
sudo apt update && sudo apt install dotnet-sdk-6.0 -y

Add testenvironments.json to your solution root

{
    "version": "1",
    "environments": [
        {
            "name": "Ubuntu",
            "type": "wsl",
            "wslDistribution": "Ubuntu"
        }
    ]
}

Visual Studio will show a new dropdown in the test explorer, select "Ubuntu" and run or debugg your tests.

enter image description here

This setup does not depend on Docker. Winver shows "20h2"

Old

Debugging tests isn't supported using ".NET Core Debugging with WSL 2" extension.

This tooling does not support running tests in WSL2, but I do know our sister team has this on their backlog to enable! .NET Core Debugging with WSL 2 - Q&A

Related