[英]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?如何设置启动我的单元测试并附加调试器的启动配置?
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.
这并不理想,但我想您不会经常这样做,而且很容易将其恢复正常。
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"
},
{
"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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.