[英]Debugging xunit tests written in F# in .NET Core and Visual Studio Code?
[英]Debugging xUnit tests in .NET Core and Visual Studio Code
我在 Mac 上運行 .NET Core 1.0 和 Visual Studio Code。
我有一個控制台項目和一個測試項目。 我已經設置了 launch.json 以便我可以調試控制台項目。
如何設置啟動我的單元測試並附加調試器的啟動配置?
參見Tyler Long 的回答。 在最新版本的 Visual Studio Code 中不需要以下步驟:)
我做了一個存儲庫來演示。
首先,讓調試器進行測試的唯一方法是添加一個文件 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 單元測試項目。
我能夠使用以下復雜的啟動配置在整個 xUnit 項目上運行調試器。 我檢查了“調試測試”鏈接(在上面的@Tyler Long 響應中)通過 C# (Omnisharp) VS Code 擴展進行的調用,以弄清楚這一點。 注意事項:1) 必須提供 dotnet 程序的絕對路徑 2) 必須提供 .nuget/packages 文件夾的絕對路徑(即不能使用~/
或$HOME/
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
}
Tyler單擊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 Code 調試器附加到正在運行的 testhost.dll。 簡單地選擇.NET Core Attach
,然后選擇dotnet testhost.dll
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.