简体   繁体   English

使用 VS Code 在 C# 中调试 xUnit 测试时,调试器不会启动

[英]Debugger does not launch when debugging xUnit tests in C# with VS Code

I am trying to debug some xUnit tests in VS code but I get this error when I try to manually debug the tests one by one (instead of just running do.net test which works fine) and I get this error:我正在尝试在 VS 代码中调试一些 xUnit 测试,但是当我尝试一个接一个地手动调试测试时出现此错误(而不是仅仅运行do.net test ,它工作正常)并且出现此错误:

----- Debugging test method TournamentTests.OrganizeIntoPairsIncludesAllElements -----

Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error MSB3883: Unexpected exception:  [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error : DirectoryNotFoundException: Could not find a part of the path '/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/bin/Debug/net6.0/ref/best-song.dll'. [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :    at Microsoft.CodeAnalysis.BuildTasks.CopyRefAssembly.Copy() [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]
/usr/local/share/dotnet/x64/sdk/6.0.200/Microsoft.Common.CurrentVersion.targets(4650,5): error :  [/Users/janneschyffert/Documents/Kurser/INDA/best-song/src/best-song.csproj]

Build FAILED.

It appears that the debugger tries to access a directory that doesn't exist, but building and running the project works just fine.似乎调试器试图访问一个不存在的目录,但构建和运行项目工作正常。

Alright, I appear to have found a temporary solution: The debugger looks for reference libraries in bin/Debug.net6.0/ref which doesn't exist.好吧,我似乎找到了一个临时解决方案:调试器在bin/Debug.net6.0/ref中查找不存在的参考库。 The libraries can however be found in obj/Debug.net6.0/ref and simply copying them over to bin seems to solve the problem albeit temporarily, I guess this must be done every time you rebuild the project but at least the debugger runs now.然而,这些库可以在obj/Debug.net6.0/ref中找到,简单地将它们复制到 bin 似乎可以解决问题,尽管是暂时的,我想每次重建项目时都必须这样做,但至少调试器现在运行.

Add the following to the project your tests are targeting:将以下内容添加到您的测试所针对的项目中:

  <PropertyGroup>
    <ProduceReferenceAssemblyInOutDir>true</ProduceReferenceAssemblyInOutDir>
  </PropertyGroup>

For example, say you have a project MyProject.csproj and another MyProject.Tests.csproj where your tests reside.例如,假设您有一个项目MyProject.csproj和另一个MyProject.Tests.csproj ,您的测试就在其中。 You want to add the ProduceReferenceAssemblyInOutDir element to your MyProject.csproj file.您想要将ProduceReferenceAssemblyInOutDir元素添加到MyProject.csproj文件中。 This ensures the output dll is added to the ref folder and then the built in debugger should work again.这确保 output dll被添加到ref文件夹,然后内置调试器应该再次工作。

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - VS Code - launch:program... does not exist - C# - VS Code - launch:program ... does not exist C#/ VS2013 - 为什么这段代码没有调试就失败了? - C# / VS2013 - Why does this code fail without debugging? C#:VS 2008调试器执行属性代码 - C#: VS 2008 debugger executing property code 调试C#代码时出现符号问题 - Symbol issue when debugging C# code 为什么连接调试器时,即时编译的C#代码不起作用? - Why does C# code compiled on the fly not work when a debugger is attached? 在VS 2008中进行调试时,为什么调试器会出现在第二个return语句上? - When debugging in VS 2008 why does the debugger land on a second return statement? c#中的单元测试项目和带有xunit的类库之间的区别 - Difference between Unit tests project in c# and a class library with xunit 尝试使用VS代码调试C#程序时,提示“prelaunchtask 'build'已终止,退出代码为1”,如何解决? - when trying to debugging an C# program by using VS code,says“prelaunchtask 'build'has been terminated, exit code is 1”,how to solve it? 调试在启动时崩溃的C#可执行文件 - Debugging a C# executable that crashes on launch C#调试器从进程中分离时运行代码 - Running code when C# debugger detaches from process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM