.net Visual Studio“调试单元测试”无法命中断点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4332112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio "Debug Unit Test" fails to hit breakpoints
提问by sym3tri
When using Visual Studio 2008 and debugging my unit tests... sometimes the debugger works fine. However very often after modifying some code then clicking "Debug Unit Test" Visual Studio will fail to hit the breakpoints in the code. The debugger basically hangs and eventually the tests runs with the new code anyway, but never stops to let me see what is going on.
当使用 Visual Studio 2008 并调试我的单元测试时......有时调试器工作正常。然而,在修改一些代码之后,经常点击“调试单元测试”Visual Studio 将无法命中代码中的断点。调试器基本上挂了,最终测试还是用新代码运行,但永远不会停下来让我看看发生了什么。
I'm assuming this has something to do with some type of assembly caching done by the debugger, but not matter what I do (clean project, delete bin folders, restart VS, etc) I can never get the right assembly to load. Has anyone else seen this behavior? Any solutions?
我假设这与调试器完成的某种类型的程序集缓存有关,但无论我做什么(清理项目、删除 bin 文件夹、重新启动 VS 等),我都无法加载正确的程序集。有没有其他人看到过这种行为?任何解决方案?
By the way, using Resharper 4.5, and .NET 3.5 on Win XP.
顺便说一下,在 Win XP 上使用 Resharper 4.5 和 .NET 3.5。
回答by Helo
I just had a problem hitting breakpoints in VS2015.
我只是在 VS2015 中遇到断点问题。
I am always using the solution configuration called Debug but for some reason my solution was set to build the Release version.
我总是使用名为 Debug 的解决方案配置,但出于某种原因,我的解决方案被设置为构建发布版本。
Switching from Release to Debug in the dropdown at the top of Visual Studio fixed my problem.
在 Visual Studio 顶部的下拉列表中从 Release 切换到 Debug 解决了我的问题。
回答by huha
Another workaround: Force the debugger to be launched from within your unit test:
另一个解决方法:强制从单元测试中启动调试器:
System.Diagnostics.Debugger.Launch();
回答by Andreas Forsl?w
One problem that I stumbled upon when trying to debug a test method was that it was private. Simply changing the method from privateto publicfixed my problem.
我在尝试调试测试方法时偶然发现的一个问题是它是私有的。只需将方法从 更改private为public解决我的问题。
I don't know why this is a problem, but it probably has something to do with the implementation of the [Test]attribute of NUnit.
我不知道为什么会出现这个问题,但可能与[Test]NUnit的属性的实现有关。
回答by derekantrican
What happened to be the solution for me: make sure all your nuget package versions match. My Unit Test project was using a version of Newtonsoft.Jsonthat was newer than the Newtonsoft.Jsonreference on the project I was testing. Once I updated all nuget packages to the latest version I was able to hit the breakpoint
我的解决方案是什么:确保所有 nuget 包版本都匹配。我的单元测试项目使用的版本Newtonsoft.Json比Newtonsoft.Json我正在测试的项目的参考版本更新。一旦我将所有 nuget 包更新到最新版本,我就能够达到断点
回答by Arthur
回答by huha
The breakpoint is not hit when starting debugging from the "Unit Test Sessions" window (Resharper - Windows - Unit Test Sessions) which comes from ReSharper.
从来自 ReSharper 的“单元测试会话”窗口(Resharper - Windows - 单元测试会话)开始调试时,未命中断点。
But when starting the test from the "Test Explorer" window(Test - Windows - Test Explorer) of VS it hits the breakpoint.
但是当从VS的“测试资源管理器”窗口(测试 - Windows - 测试资源管理器)开始测试时,它会遇到断点。
VS Enterprise 2017 V15.5.6, ReSharper 2017.2.2
VS Enterprise 2017 V15.5.6、ReSharper 2017.2.2
The latest ReSharper 2017.3.1 is not an option because it has other bugs
最新的 ReSharper 2017.3.1 不是一个选项,因为它有其他错误
回答by Frank Socha
I had the same problem, although I don't have permanent solution, this is a quick one time fix: Debug the unit test (Ctrl-T, Ctrl-D), then go to "Immediate Window", enter anything (e.g. 'a' or null) and press enter. After this the break point will be hit.
我遇到了同样的问题,虽然我没有永久解决方案,但这是一次快速修复:调试单元测试(Ctrl-T、Ctrl-D),然后转到“立即窗口”,输入任何内容(例如 ' a' 或 null),然后按 Enter。在此之后,断点将被击中。
回答by Frank Socha
If you have [HostType("ASP.NET")], remove it and Test -> Debug -> Run your tests again
如果有[HostType("ASP.NET")],请删除它并测试 -> 调试 -> 再次运行测试
回答by Mike
Make sure you are debugging the correct test!
确保您正在调试正确的测试!
I have some tests with very similar names except for the last word in the test name. I had the break point set in the first test and was using Visual Studios "Test Explorer" window to "Debug Selected Tests" on the second test, which didn't have a breakpoint set.
除了测试名称中的最后一个单词外,我有一些名称非常相似的测试。我在第一个测试中设置了断点,并在第二个测试中使用 Visual Studios“测试资源管理器”窗口“调试选定的测试”,第二个测试没有设置断点。
Test names
测试名称
PublishAsync_Valid_Acked
PublishAsync_Valid_Nacked
回答by karthik kasubha
Ensure [TestMethod] attribute is present for the method, [TestClass] is present for class.
确保方法存在 [TestMethod] 属性,类存在 [TestClass]。

