An example repository demonstrating how to debug C# .NET 9 NUnit tests in Cursor AI, with proper setup for both automatic and manual debugging scenarios.
Until the Cursor team fixes their C# extension to allow debugging tests, we can work around the limitation with only the launch.json file.
Use the coreclr type and attach to a specific Process ID.
To avoid needing to manually find the PID and/or select the process from a list, we can get the PID via an input.
{
"name": "Debug Unit Tests (Automatic)",
"type": "coreclr",
"request": "attach",
"processId": "${input:spawnTestsGetPid}",
"justMyCode": false
}The input needs to start the test host running, find its PID and return it so the debugger can attach to the test host.
{
"id": "spawnTestsGetPid",
"type": "command",
"command": "shellCommand.execute",
"args": {
"useFirstResult": true,
"command": "pwsh -NoProfile -Command \"$env:VSTEST_HOST_DEBUG='1'; $dotnet = Start-Process dotnet -PassThru -WindowStyle Hidden -ArgumentList 'test','test/Unit/Unit.csproj','--configuration','Debug','--no-build'; $processId = $null; for($i=0; $i -lt 50 -and -not $processId; $i++){ $t = Get-Process -Name testhost -ErrorAction SilentlyContinue; if($t){ $processId = $t.Id; break }; Start-Sleep -Milliseconds 100; }; $processId\""
}
}Here's the PowerShell command in a more readable format:
# Instruct the test host to wait for a debugger to attach
$env:VSTEST_HOST_DEBUG = '1';
# dotnet test test/Unit/Unit.csproj --configuration Debug --no-build
$dotnet = Start-Process dotnet -PassThru -WindowStyle Hidden -ArgumentList 'test', 'test/Unit/Unit.csproj', '--configuration', 'Debug', '--no-build';
# Wait for the test host to start and get its process ID
$processId = $null;
for ($i = 0; $i -lt 50 -and -not $processId; $i++)
{
$t = Get-Process -Name testhost -ErrorAction SilentlyContinue
if ($t)
{
$processId = $t.Id;
break
}
# retry after a short delay
Start-Sleep -Milliseconds 100;
}
# return the process ID to be used in the launch configuration
$processIdThis repository provides working examples and configurations for debugging C# .NET NUnit test projects in Cursor AI.
License restrictions disallow non-Microsoft IDEs/tools from using their C# extensions, so Anysphere (Cursor AI) released their own C# extension that uses the open source Samsung netcoredbg .NET debugger. It's a bit rough around the edges in its current state, and things like Debug Test in the CodeLens above an NUnit test method doesn't work - the debugger doesn't attach, the test doesn't run, breakpoint doesn't get hit.
There are a lot of posts and confusion about debugging .NET code in Cursor AI - some of it pre-dating the Anysphere C# extension. I found that their (relatively) new extension allowed me to debug a console application just fine, but not so much with an NUnit test. There are a few other shortcomings, but not being able to debug an NUnit test is a big deal for myself and my team. So, I set out to find a workable solution that didn't require manual interaction, and hopefully learn a few things along the way since my .NET Debugging experience has almost exclusively been in Visual Studio with ReSharper. The ReSharper extension for VS Code (and Cursor AI) has actually gotten a lot better, but it doesn't yet support debugging tests, either.
cursor-debug/
βββ src/
β βββ Console1/ # Main console application
β β βββ Program.cs # Entry point with debugging examples
β β βββ Console1.csproj
β βββ Lib1/ # Library project
β βββ Extensions.string.cs # Extension methods
β βββ Lib1.csproj
βββ test/
β βββ Unit/ # NUnit test project
β βββ UnitTest1.cs # Sample tests
β βββ Unit.csproj
βββ .vscode/ # VSCode/Cursor configuration
βββ launch.json # Debug configurations - this is the key for debugging NUnit tests
βββ settings.json # C# debugging settings
βββ tasks.json # Build tasks
-
Clone the repository
git clone <repository-url> cd cursor-debug
-
Build the solution
dotnet build
-
Open in Cursor and start debugging!
This repository provides two approaches for debugging NUnit tests:
Perfect for: Quick debugging without manual process selection
- Set a breakpoint in your test method (
test/Unit/UnitTest1.cs) - Go to
Run and Debugpane - Select
Debug Unit Tests (Automatic) - Click the play button
- The debugger automatically attaches and breaks at your breakpoint
How it works: Uses a PowerShell script that automatically starts the test process and finds the testhost process ID for attachment.
Perfect for: When you need more control over the debugging process
-
Start the test process:
$env:VSTEST_HOST_DEBUG="1"; dotnet test test/Unit/Unit.csproj --configuration Debug --no-build
-
Set breakpoints in your test methods
-
Attach debugger:
- Go to
Run and Debugpane - Select
Debug Unit Tests (Manual) - Choose the
testhost.exeprocess from the list
- Go to
-
Continue execution - the debugger will break at your breakpoints
- Open
src/Console1/Program.cs - Set a breakpoint on any line
- Press
F5or go toRun and Debugβ.NET Core Launch (console) - The debugger will stop at your breakpoint
The repository includes three debug configurations:
Debug Unit Tests (Automatic)- Automated test debugging with PowerShellDebug Unit Tests (Manual)- Manual process attachment for tests.NET Core Launch (console)- Standard console app debugging
Optimized C# debugging settings including:
justMyCode: false- Debug into framework codesuppressJITOptimizations: true- Better debugging experience- Symbol server configuration for Microsoft and NuGet symbols
- Enhanced logging for troubleshooting
public static bool IsNullOrEmpty(this string? @this)
{
return string.IsNullOrEmpty(@this);
}[TestCase("Hello" , 5, false)]
[TestCase("Hello World", 11, false)]
[TestCase("" , 0, true )]
[TestCase(null , 0, true )]
public void Test1(string? str, int expectedLength, bool expectedIsNullOrEmpty)
{
var actualLength = str?.Length ?? 0;
var actualIsNullOrEmpty = str.IsNullOrEmpty();
Assert.That(actualLength , Is.EqualTo(expectedLength) );
Assert.That(actualIsNullOrEmpty, Is.EqualTo(expectedIsNullOrEmpty));
}- .NET 9.0 or later (earlier versions will likely as well, but I am using .NET 9)
- Cursor AI (I have the Anysphere C# extension and the ReSharper extension)
- PowerShell 7 (for automatic test debugging)
This repository is designed to help the Cursor AI community with debugging configurations. If you have improvements or additional debugging scenarios, contributions are welcome!
MIT
Created to address common debugging questions in the Cursor AI community forums, particularly around NUnit test debugging which is not well-documented.
