我在Mac上运行.NET Core 1.0和Visual Studio Code。
我有一个控制台项目和一个测试项目。我已经设置了launch.json,以便我可以调试控制台项目。
如何设置启动单元测试并附加调试器的启动配置?
答案 0 :(得分:120)
答案 1 :(得分:13)
见Tyler Long's answer。最新版本的Visual Studio代码中不需要以下步骤:)
我让a repository进行演示。
首先,我可以让调试器完成测试的唯一方法是添加一个文件,Program.cs,控制xUnit的入口点,并手动添加代码进行测试。它并不理想,但我想你不会经常这样做,并且很容易将它恢复正常。
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"
},
{
"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单元测试项目。
答案 2 :(得分:12)
我能够使用以下复杂启动配置在整个xUnit项目上运行调试器。我检查了调用" debug test"链接(在上面的@Tyler Long响应中)正在通过C#(Omnisharp)VS Code extension来解决这个问题。注意事项:1)您必须提供绝对路径到dotnet程序2)您必须提供绝对路径(即您不能使用ls
或{ {1}})到.nuget / packages文件夹3)在下面的例子中,我的测试项目命名空间的名称是ls
。一旦你有这个启动配置,你可以放置断点,使用这个配置启动调试器,它应该击中所有的断点。
~/
答案 3 :(得分:10)
debug test
代码镜头图标的Tyler's answer是调试单个测试的最简单方法。
测试所有单元测试的方法是在测试中添加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。简单选择.NET Core Attach
,然后选择dotnet testhost.dll
。