在 .NET Core 和 Visual Studio 代码中调试 xUnit 测试

Debugging xUnit tests in .NET Core and Visual Studio Code

我正在使用 Mac、运行 .NET Core 1.0 和 Visual Studio 代码。

我有一个控制台项目和一个测试项目。我已经设置 launch.json 以便我可以调试控制台项目。

如何设置启动配置以启动我的单元测试并附加调试器?

参见。 Visual Studio 代码的最新版本不需要以下步骤:)


我做了a repository来演示

首先,我能让调试器进行测试的唯一方法是添加一个文件,Program.cs,从 xUnit 控制入口点,然后手动添加代码进行测试。这并不理想,但我想你不会经常这样做,而且很容易将其恢复正常。

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();

        }
    }
}

接下来,在 project.json 中添加以下内容:

  "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"
        }
      }
    }
  }
}

这将允许您调试 xUnit 单元测试项目。

如果安装最新的软件和库,调试起来超级容易:

从截图中可以看出,点击"debug test"调试即可!

我能够使用以下 复杂 启动配置在整个 xUnit 项目上 运行 调试器。我检查了“调试测试”link(在上面的@Tyler Long 回复中)通过 C# (Omnisharp) VS Code extension 进行的调用来解决这个问题。注意事项:1)您必须提供 absolute 路径到 dotnet 程序 2)您必须提供 absolute 路径(即您不能使用 ~/$HOME/) 到 .nuget/packages 文件夹 3) 在下面的示例中,我的测试项目命名空间的名称是 Tests。一旦你有了这个启动配置,你就可以放置断点,使用这个配置启动调试器,它应该会命中所有断点。

{
  "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
}

单击 debug test 代码镜头图标是调试单个测试的最简单方法。

测试所有单元测试的一种方法是在测试中添加 while(!Debugger.IsAttached) Thread.Sleep(500);。这将使测试等到您附加调试器。

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);
        }
    }
}

这样您就可以将 Visual Studio 代码调试器附加到 运行 testhost.dll。简单 select .NET Core Attach 然后 dotnet testhost.dll.