简体   繁体   English

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

[英]Debugging xUnit tests in .NET Core and Visual Studio Code

I'm on a Mac, running .NET Core 1.0 and Visual Studio Code.我在 Mac 上运行 .NET Core 1.0 和 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.我已经设置了 launch.json 以便我可以调试控制台项目。

How do I set up a launch configuration that launches my unit tests and attaches the debugger?如何设置启动我的单元测试并附加调试器的启动配置?

If you install the latest software and library, it is super easy to debug:如果安装最新的软件和库,调试起来超级容易:

在此处输入图片说明

As you can see from the screenshot, just click "debug test" and debug it!从截图中可以看到,只需单击“调试测试”并进行调试即可!

See Tyler Long's answer .参见Tyler Long 的回答 The steps below are not required in the newest versions of Visual Studio Code :)在最新版本的 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.首先,让调试器进行测试的唯一方法是添加一个文件 Program.cs,从 xUnit 控制入口点,然后手动添加代码以进行测试。 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:程序.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:接下来,在 project.json 中添加以下内容:

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

project.json:项目.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.这将允许您调试 xUnit 单元测试项目。

I was able to run the debugger on an entire xUnit project with the following complicated launch config.我能够使用以下复杂的启动配置在整个 xUnit 项目上运行调试器。 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.我检查了“调试测试”链接(在上面的@Tyler Long 响应中)通过 C# (Omnisharp) VS Code 扩展进行的调用,以弄清楚这一点。 Things to note: 1) you must provide the absolute path to the dotnet program 2) you must provide the absolute path (ie you cannot use ~/ or $HOME/ ) to .nuget/packages folders 3) in the example below, the name of my test project namespace is Tests .注意事项:1) 必须提供 dotnet 程序的绝对路径 2) 必须提供 .nuget/packages 文件夹的绝对路径(即不能使用~/$HOME/ 3)在下面的示例中,我的测试项目命名空间的名称是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
}

Tyler's answer of clicking the debug test code lens icons is the easiest way of debugging a single test. Tyler单击debug test代码镜头图标的答案是调试单个测试的最简单方法。

A way of testing all unit tests would be to add while(!Debugger.IsAttached) Thread.Sleep(500);测试所有单元测试的一种方法是添加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.然后,这允许您将 Visual Studio Code 调试器附加到正在运行的 testhost.dll。 Simple select .NET Core Attach and then the dotnet testhost.dll .简单地选择.NET Core Attach ,然后选择dotnet testhost.dll

调试 .NET Core 附加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在.NET Core和Visual Studio Code中调试用F#编写的xunit测试? - Debugging xunit tests written in F# in .NET Core and Visual Studio Code? Visual Studio Code.Net Core 调试错误“源代码与原始代码不同” - Visual Studio Code .Net Core debugging error 'source code is different from original' Visual Studio 2010不调试.NET 2.0代码 - Visual Studio 2010 Not Debugging .NET 2.0 Code Visual Studio Code 调试默认的 ASP.NET Core MVC WebApp:不起作用 - Visual Studio Code debugging the default ASP.NET Core MVC WebApp: does not work ASP.NET Core 在 Linux 上的 Visual Studio Code 中调试时修改视图 - ASP.NET Core Modify Views While Debugging in Visual Studio Code on Linux 如何配置visual studio以运行xUnit.net测试? - How do I configure visual studio to run xUnit.net tests? Visual Studio Code - 调试 - Visual Studio Code - Debugging 在 Visual Studio 2019 中调试时将输入重定向到 .NET Core Console App - Redirect input to .NET Core Console App on debugging in Visual Studio 2019 Visual Studio和.net,以任何方式在代码中执行操作-仅在调试时? - Visual Studio and .net, any way to perform an action in code - only if debugging? 如何在visual studio中禁用本机.net代码调试? - How do I disable native .net code debugging in visual studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM