我可以使 NUnit GUI 在断点/失败的测试中中断到 Visual Studio 调试器吗?
使用 NUnit GUI 运行单元测试时,是否有办法:
- 在测试内设置断点,
- 或者在测试失败时中断到 Visual Studio 即时调试器?
When using the NUnit GUI to run unit tests, is there a way to either:
- Set breakpoints inside the tests,
- or break into the Visual Studio Just-in-Time debugger upon failure of a test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的第一个要求非常简单。将 VS 调试器附加到 NUnit GUI(工具 -> 附加到进程)并相应地设置断点。当附加调试器运行测试时,将命中断点。
第二个要求也很简单,但我还没有验证它是否有效(也就是说,我知道它会损坏,但我不知道它会损坏到用户代码多远)。当单元测试失败时,NUnit 框架会引发
NUnit.Framework.AssertionException
。将调试器设置为在引发此异常时中断,并且您不需要在代码中设置断点。为此,请访问调试->异常...,然后选择添加...。选择公共语言运行时异常并输入 NUnit 异常的完整类型名称(包括命名空间)。最后,在原始例外屏幕中,选择新例外并单击抛出。同样,您需要使用附加的调试器来运行测试,以便它在引发异常时捕获。
Your first requirement is very straightforward. Attach the VS debugger to the NUnit GUI (Tools->Attach to Process) and set your breakpoints accordingly. When the tests run with the debugger attached, the breakpoints will be hit.
The second requirement is also straightforward, but I haven't verified it to work (that is, I know it will break, but I don't how far from user-code it will break). When a unit test fails, the NUnit framework raises an
NUnit.Framework.AssertionException
. Set the debugger to break when this exception is thrown, and you won't need to set breakpoints in your code. To do this, visit Debug->Exceptions..., then select Add.... Select Common Language Runtime Exception and enter the full typename (including namespace) of the NUnit exception. Finally, in the original exception screen, select your new exception and click Thrown.Again, you will need to run your tests with the debugger attached in order for it to catch when the exceptions are thrown.
将 Visual Studio 调试器附加到 NUnit 进程,然后在 NUnit GUI 中运行测试。
Attach the Visual Studio debugger to the NUnit process, and then run your tests in the NUnit GUI.
如果将 Visual Studio 调试器附加到 nunit 进程并在 nunit 中执行测试,则将使用 Visual Studio 中的断点。
我不知道测试失败后如何做到这一点。
If you attach your Visual Studio debugger to the nunit process and execute your test in nunit, then the breakpoints in Visual Studio will be used.
I'm not aware of how this might be done upon failure of a test.
除了在运行测试时附加到 NUnit 进程之外,您还可以临时添加到
SetUpFixture 或 TestFixtureSetUp 中,甚至添加到捕获预期异常的 try catch 中。
In addition to attaching to the NUnit process while running tests, you can temporarily add
either in a SetUpFixture or TestFixtureSetUp or even in a try catch where the anticipated exception is caught.
不必担心附加到流程的麻烦。那很快就会变老。相反,请转到单元测试项目的属性,然后在“调试”选项卡中,将“启动外部程序:”设置为指向 nunit.exe,然后在“命令行参数”文本框中添加输出 dll 名称(例如 UnitTests .dll)
Don't bother with the hassle of attaching to a process. That gets old very quickly. Instead, go to the properties for your unit test project, and and the Debug tab, set "Start external program:" to point to nunit.exe, and add the output dll name in the "Command line arguments" text box (eg UnitTests.dll)