driis
driis

Reputation: 164281

Debugging xUnit tests in .NET Core and Visual Studio Code

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?

Upvotes: 75

Views: 59869

Answers (7)

Phillip Ngan
Phillip Ngan

Reputation: 16086

You can use C# Dev Kit, and use the command Test: Debug. This will present a range of options including debugging tests at the Cursor, All tests, Failed tests.

Upvotes: 1

Eric Eskildsen
Eric Eskildsen

Reputation: 4759

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.

Upvotes: 0

Cyrille Belfort
Cyrille Belfort

Reputation: 91

@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.

Upvotes: 0

PiTrick
PiTrick

Reputation: 109

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

Upvotes: 10

Brady Holt
Brady Holt

Reputation: 2924

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
}

Upvotes: 13

Nick Acosta
Nick Acosta

Reputation: 1910

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.

Upvotes: 14

Tyler Liu
Tyler Liu

Reputation: 20356

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!

Upvotes: 187

Related Questions