73

I'm on a Mac, running .NET Core 1.0 and Visual Studio Code.

I have a console project and a test project. I have setup launch.json so that I can debug the console project.

How do I set up a launch configuration that launches my unit tests and attaches the debugger?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
driis
  • 161,458
  • 45
  • 265
  • 341

6 Answers6

188

If you install the latest software and library, it is super easy to debug:

enter image description here

As you can see from the screenshot, just click "debug test" and debug it!

Tyler Liu
  • 19,552
  • 11
  • 100
  • 84
  • 7
    How do you debgu and/or run all tests? – gerrard00 Jul 19 '16 at 21:34
  • 4
    @gerrard00 I don't know how to debug all tests. To run all tests, `dotnet test`. – Tyler Liu Jul 20 '16 at 01:10
  • 4
    just to add that in your test project's project.json you may want to add this to your buildOptions section. "debugType": "portable". This will solve the problem that "No Symbol loaded for this document" – Sul Aga Sep 04 '16 at 21:54
  • 1
    Does this work with nunit tests? My install isn't showing this option. – Ryan Walls Nov 23 '16 at 21:45
  • 2
    @RyanWalls Frankly speaking I don't know. I am afraid that there isn't very seemless integration for NUnit in VSCode (yet). And that's the reason why I learned xUnit. – Tyler Liu Nov 24 '16 at 03:31
  • Did you have to add a launch config to launch.json before the debug test link shows up? – Warren P Dec 20 '16 at 14:27
  • @WarrenP I remember that the editor prompt me to create a launch config file. I don't remember I changed anything in that file manually. – Tyler Liu Dec 21 '16 at 01:52
  • What configuration does this debug use? Because call too Directory.GetCurrentDirectory() gives me root folder and not the folder where my Startup.cs is located I am serving my images with custom file provider which uses this information. – Dživo Jelić Jan 23 '17 at 00:11
  • its visible for me but nothing happens when i click on "debug test" – rodrigorf Apr 24 '17 at 02:43
  • Did you install any plugin, I am Not getting like this in new vscode. Also the code has no json files to configure – Saravanan Jul 26 '17 at 03:41
  • @Saravanan Yes install this plugin: https://github.com/OmniSharp/omnisharp-vscode – Tyler Liu Jul 26 '17 at 07:40
  • 1
    That was not easy to find. To make that "debug test" link show up, you need to right-mouse click the test section, select "Run Code" then those links will show up. Then you can click the "debug test" section afterward. – fletchsod Aug 01 '17 at 21:14
  • 2
    I doesn't work for me with "csproj". According to official Microsoft page: "During the development of the .NET Core tooling, an important design change was made to no longer support project.json files and instead move the .NET Core projects to the MSBuild/csproj format." – LukaszTaraszka Dec 21 '17 at 16:28
  • Comment by [user Daniel](https://stackoverflow.com/users/3319644/daniel): " you can debug all tests in a similar way by using the context menu entry 'Debug All Tests' at the class containing the tests" – Mr. T Jul 27 '18 at 11:19
  • 2
    is there any difference in Linux version of VSCode. I have the latest deb and also latest version of C# extension but still that Debug button is not appearing. – Laurence Mar 25 '19 at 09:05
  • If you're not seeing the option to run/debug your tests, make sure VSCode has Code Lens enabled. It's the thing about functions that says how many times it's been used, I purposely disabled this but you need it for xunit. – Luke Garrigan Sep 06 '19 at 16:17
  • 2
    I wasn't able to see the Run / Debug Test options until I made my projects part of a solution and added the Test Project to the solution (on linux, fwiw). https://github.com/OmniSharp/omnisharp-vscode/issues/3753#issuecomment-624176681 – l p Jun 03 '20 at 00:12
  • 1
    Only problem with this answer, is some people hate code lens for all the other stuff it shows that I don't need or want to see – PandaWood May 29 '22 at 03:27
  • the 'latest library'? please explain. I have the latest, and don't see that option. – john k Mar 08 '23 at 04:47
14

See Tyler Long's answer. The steps below are not required in the newest versions of Visual Studio Code :)


I made a repository to demonstrate.

First off, the only way I could get the debugger to hit the test was to add a file, Program.cs, take control of the entry point from xUnit, and manually add code to test. It's not ideal, but I imagine you aren't going to be doing this very often, and it's easy to flip it back to normal.

Program.cs:

using System;
namespace XUnitDebugging
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var test = new TestClass();
            test.PassingTest();
            Console.WriteLine("Enter text...");
            Console.ReadLine();

        }
    }
}

Next, in project.json add the following:

  "buildOptions": {
    "emitEntryPoint": true,
    "debugType": "portable"
  },

project.json:

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
  "buildOptions": {
    "emitEntryPoint": true,
    "debugType": "portable"
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0"
    },
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      }
    }
  }
}

This will allow you to debug an xUnit unit test project.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Acosta
  • 1,890
  • 17
  • 19
  • 2
    It works for me. By the way, you don't need to comment/uncomment `"testRunner": "xunit"` and it just works. – Tyler Liu Jul 14 '16 at 09:31
  • 3
    I'll accept this answer since it was correct at the time I posted the question - there was indeed no polished way of debugging unit tests in VSCode 1.2. – driis Jul 15 '16 at 18:16
  • Is this available on VSCode for MacOS as well? – ataravati Nov 17 '19 at 20:23
12

I was able to run the debugger on an entire xUnit project with the following complicated launch config. I inspected the calls the "debug test" link (in @Tyler Long response above) was making through the C# (Omnisharp) VS Code extension to figure this out. Things to note: 1) you must provide the absolute path to the dotnet program 2) you must provide the absolute path (i.e. you cannot use ~/ or $HOME/) to .nuget/packages folders 3) in the example below, the name of my test project namespace is Tests. Once you have this launch config in place, you can place breakpoints(s), launch the debugger using this config and it should hit all the breakpoints.

{
  "name": "Debug xunit tests",
  "type": "coreclr",
  "request": "launch",
  "preLaunchTask": "build",
  "program": "/usr/local/share/dotnet/dotnet",
  "args": [
    "exec",
    "--runtimeconfig",
    "${workspaceRoot}/AppNameHere/bin/Debug/netcoreapp1.0/AppNameHere.runtimeconfig.json",
    "--depsfile",
    "${workspaceRoot}/AppNameHere/bin/Debug/netcoreapp1.0/AppNameHere.deps.json",
    "--additionalprobingpath",
    "/Users/jdoe/.nuget/packages",
    "/Users/jdoe/.nuget/packages/dotnet-test-xunit/1.0.0-rc2-build10015/lib/netcoreapp1.0/dotnet-test-xunit.dll",
    "${workspaceRoot}/AppNameHere/bin/Debug/netcoreapp1.0/AppNameHere.dll",
    "-namespace",
    "Tests"
  ],
  "cwd": "${workspaceRoot}",
  "stopAtEntry": false
}
riQQ
  • 9,878
  • 7
  • 49
  • 66
Brady Holt
  • 2,844
  • 1
  • 28
  • 34
  • 3
    Thank you, this is great! One minor thing that I did to make this a little easier to share with coworkers is to add a symbolic link to the .nuget directory in the root of the workspace: `ln -s ~/.nuget`, and then to change `/Users/jdoe` to `${workspaceRoot}` -- It's not ideal, but it's a one-time setup. (Also, remember to ignore `.nuget/` in your VCS config!!) – Andrew Theken Nov 05 '16 at 13:41
  • 1
    @Brady Holt how were you able to inspect the "debug test" calls? Trying to replicate this but my omnisharp extension is using a different package than the one specified. – Archibald Jan 26 '20 at 17:52
10

Tyler's answer of clicking the debug test code lens icons is the easiest way of debugging a single test.

A way of testing all unit tests would be to add while(!Debugger.IsAttached) Thread.Sleep(500); inside the tests. This will make the tests wait until you attach a debugger.

using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;

namespace SomeNamespace
{
    [TestFixture]
    public class SomeClassTests
    {
        [Test]
        public void ShouldDoTest()
        {
            while(!Debugger.IsAttached) Thread.Sleep(500);
            Assert.That(true, Is.True);
        }

        [Test]
        public void ShouldDoTest2()
        {
            while(!Debugger.IsAttached) Thread.Sleep(500);
            Assert.That(true, Is.True);
        }
    }
}

This then allows you to attach the Visual Studio Code debugger to the running testhost.dll. Simple select .NET Core Attach and then the dotnet testhost.dll.

Debug .NET Core Attach

riQQ
  • 9,878
  • 7
  • 49
  • 66
PiTrick
  • 109
  • 1
  • 5
0

Edit: A quick fix (per Cyrille Belfort):

  1. Ctrl+Shift+P
  2. Search for and select OmniSharp: Select Project
  3. Select the test project
  4. Enter

Original answer: To have OmniSharp detect both projects without having to switch between them, you can create a solution.

  1. Create a solution:

    cd YourProjectRootDirectory
    dotnet new sln --name YourSolutionName
    
  2. Add your projects:

    dotnet sln add .\src\YourProject.csproj
    dotnet sln add .\test\YourProject.Tests.csproj
    
  3. Reload VS Code:

    1. Ctrl+Shift+P
    2. Search for and select Developer: Reload window
    3. Enter
  4. Select the solution:

    1. Ctrl+Shift+P
    2. Search for and select OmniSharp: Select Project
    3. Select your solution
    4. Enter

Before:

A test method with only "0 references" above it

After:

A test method with "0 references | Run Test | Debug Test" above it

Thanks to @lp for the comment suggesting this.

Eric Eskildsen
  • 4,269
  • 2
  • 38
  • 55
0

@Eric Eskildsen, Helped me solve for myself

To be noted I only used Step 4 and selected the csproj I was working on rather than building a solution.

The strange thing is only those CodeLens test link seemed affected. Omnisharp hints and/or inlay hints are working throughout the workspace.