在 .NET Core 和 Visual Studio Code 中调试 xUnit 测试
我使用的是 Mac,运行 .NET Core 1.0 和 Visual Studio Code.
I'm on a Mac, running .NET Core 1.0 and Visual Studio Code.
我有一个控制台项目和一个测试项目.我已经设置了 launch.json 以便我可以调试控制台项目.
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?
参见 Tyler Long 的回答.在最新版本的 Visual Studio Code 中不需要以下步骤:)
See Tyler Long's answer. The steps below are not required in the newest versions of Visual Studio Code :)
我制作了一个存储库来演示.
首先,让调试器进行测试的唯一方法是添加一个文件 Program.cs,从 xUnit 控制入口点,然后手动添加代码以进行测试.这并不理想,但我想您不会经常这样做,而且很容易将其恢复正常.
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.
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 中添加以下内容:
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"
}
}
}
}
}
这将允许您调试 xUnit 单元测试项目.
This will allow you to debug an xUnit unit test project.