调试.NET Core和Visual Studio代码中的xunit测试 [英] Debugging xunit tests in .NET Core and Visual Studio Code
问题描述
我有一个控制台项目和一个测试项目。我设置了launch.json,以便我可以调试控制台项目。
如何设置启动我的单元测试并附加调试器的启动配置?
编辑:请参阅泰勒龙答案。 VS代码的最新版本中不需要以下步骤:)
我做了一个 repo 来演示。
首先,我可以让调试器进行测试的唯一方法是添加一个Program.cs,从XUnit控制入口点,并手动添加代码进行测试。这不是很理想,但我想你不会经常这样做,很容易将其翻转回正常。
Program.cs:
using System;
命名空间XUnitDebugging
{
public class Program
{
public static void Main(string [] args)
{
var test = new TestClass();
test.PassingTest();
Console.WriteLine(Enter text ...);
Console.ReadLine();
}
}
}
project.json将以下内容添加到您的project.json中:
buildOptions:{
emitEntryPoint:true ,
debugType:portable
},
project.json:
{
version:1.0.0- *,
testRunner:xunit,
buildOptions:{
emitEntryPoint:true,
debugType:portable
},
依赖关系:{
Microsoft.NETCore.App:{
type:platform,
version:1.0.0
}
xunit:2.2.0-beta2-build3300,
dotnet-test-xunit:2.2.0-preview2-build1029
},
框架:{
netcoreapp1.0:{
dependencies:{
Microsoft.NETCore.App:{
type:platform,
version:1.0.0
}
}
}
}
}
这将允许您调试xunit单元测试项目。
I'm on a Mac, running .NET Core 1.0 and 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.
How do I setup a launch configuration that launches my unit tests and attaches the debugger ?
EDIT: See Tyler Long's answer. The steps below are not required in the newest versions of VS Code :)
I made a repo to demonstrate.
First off, the only way I could get the debugger to hit the test was to add a 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.
Program.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, project.json add the following to your project.json:
"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"
}
}
}
}
}
This will allow you to debug an xunit unit test project.
这篇关于调试.NET Core和Visual Studio代码中的xunit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!