如何从 Visual Studio 以调试模式运行 NUnit?
我最近正在为我一直在研究的一些 C# 构建一个测试框架。 我在工作区中设置了 NUnit 和一个新项目来测试该组件。 如果我从 Nunit (v2.4) 加载单元测试,一切都会很好,但我已经到了在调试模式下运行并设置一些断点非常有用的地步。
我已经尝试了几个指南中的建议,这些指南都建议更改测试项目的“调试”属性:
Start external program: C:\Program Files\NUnit 2.4.8\bin\nunit-console.exe
Command line arguments: /assembly: <full-path-to-solution>\TestDSP\bin\Debug\TestDSP.dll
我在那里使用控制台版本,但也尝试过调用 GUI。 当我尝试开始调试时,两者都会给我同样的错误:
Cannot start test project 'TestDSP' because the project does not contain any tests.
这是因为我通常将 \DSP.nunit 加载到 Nunit GUI 中,并且这就是进行测试的地方吗?
我开始认为问题可能是 VS 想运行它自己的测试框架,这就是为什么它找不到 NUnit 测试?
编辑:对于那些询问测试装置的人,我在 TestDSP 项目中的一个 .cs 文件大致如下所示:
namespace Some.TestNamespace
{
// Testing framework includes
using NUnit.Framework;
[TestFixture]
public class FirFilterTest
{
[Test]
public void Test01_ConstructorTest()
{
...some tests...
}
}
}
...我对 C# 和 NUnit 测试框架相当陌生,所以这是完全可能的我错过了一些关键信息;-)
最终解决方案:最大的问题是我使用的项目。 如果您选择其他语言 -> 视觉 C# -> 测试-> 测试项目
...当您选择项目类型时,据我所知,Visual Studio 将尝试使用它自己的测试框架。 您应该选择一个普通 C# 类库项目,然后我选择的答案中的说明将起作用。
I've recently been building a test framework for a bit of C# I've been working on. I have NUnit set up and a new project within my workspace to test the component. All works well if I load up my unit tests from Nunit (v2.4), but I've got to the point where it would be really useful to run in debug mode and set some break points.
I've tried the suggestions from several guides which all suggest changing the 'Debug' properties of the test project:
Start external program: C:\Program Files\NUnit 2.4.8\bin\nunit-console.exe
Command line arguments: /assembly: <full-path-to-solution>\TestDSP\bin\Debug\TestDSP.dll
I'm using the console version there, but have tried the calling the GUI as well. Both give me the same error when I try and start debugging:
Cannot start test project 'TestDSP' because the project does not contain any tests.
Is this because I normally load \DSP.nunit into the Nunit GUI and that's where the tests are held?
I'm beginning to think the problem may be that VS wants to run it's own test framework and that's why it's failing to find the NUnit tests?
Edit: To those asking about test fixtures, one of my .cs files in the TestDSP project looks roughly like this:
namespace Some.TestNamespace
{
// Testing framework includes
using NUnit.Framework;
[TestFixture]
public class FirFilterTest
{
[Test]
public void Test01_ConstructorTest()
{
...some tests...
}
}
}
...I'm pretty new to C# and the NUnit test framework so it's entirely possible I've missed some crucial bit of information ;-)
Final Solution: The big problem was the project I'd used. If you pick Other Languages -> Visual C# -> Test -> Test Project
...when you're choosing the project type, Visual Studio will try and use it's own testing framework as far as I can tell. You should pick a normal C# class library project instead and then the instructions in my selected answer will work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
当我需要调试 NUnit 测试时,我只需使用“调试|附加到进程”附加到 NUnit GUI 应用程序
nunit-agent.exe
并从 GUI 运行测试。 我的测试(或他们正在测试的代码)中的任何断点都会被命中。 我是否误解了你的问题,或者这对你有用吗?When I need to debug my NUnit tests, I simply attach to the NUnit GUI application
nunit-agent.exe
using "Debug|Attach to Process" and run the tests from the GUI. Any breakpoints in my tests (or the code they're testing) are hit. Am I misunderstanding your question, or will that work for you?我使用与您尝试 Jon 相同的技术,没有 /assemble 标志,即
TestDSP.dll 是否包含您所有的 TestFixtures?
由于我的测试项目不是解决方案中的启动项目,因此我通过右键单击测试项目并选择“调试 -->”来运行测试。 启动新实例
I use the same technique as you are trying Jon, without the /assembly flag, i.e.
Does TestDSP.dll contain all your TestFixtures?
As my test project is not the startup project in the solution, I run my tests by right-clicking on the test project and choosing Debug --> Start New Instance
删除类似的行即可
只需从项目文件中 。
这一行基本上告诉 VS.Net 这是一个测试项目,因此“无法启动测试项目”。 仅供参考,第一个 Guid 说“这是一个测试”,第二个 Guid 说“这是 C#”。
有关这些指南的信息: http://www.mztools.com/Articles/2008/ MZ2008017.aspx
Simply remove the line that looks like
from your project file.
This line basically tells VS.Net that it's a Test project, thus the "Cannot start test project". FYI here the 1st Guid says "it's a test", the 2nd says "it's C#".
For information on those Guids: http://www.mztools.com/Articles/2008/MZ2008017.aspx
除了 @Justin 提供的答案之外,这里还有 NUnit 2.6 的更多详细信息。
使用 NUnit 2.6 连接到 nunit.exe 或 nunit-console.exe,而不是代理。 @Justin 指出的配置略有不同。 以下是 nunit.exe.config 中的示例(与 nunit-console.exe.config 相同)。
对于 .NET 4 测试项目,要获得要命中的断点,您必须按照注释的建议注释掉或删除 v2.0 行。 完成此操作后,我就能够调试 .NET 4.0 测试项目。
In addition to the answer provided by @Justin here are some more details for NUnit 2.6.
Using NUnit 2.6 attach to nunit.exe or nunit-console.exe and NOT the agent. The configuration noted by @Justin is slightly different. Below is an example from nunit.exe.config (same for nunit-console.exe.config).
For .NET 4 test project, to get break points to hit, you will have to comment out or remove the v2.0 line as the comment suggests. Once I did that I was able to debug the .NET 4.0 test project.
如果您使用的是 NUnit 2.4 或更高版本,您可以将以下代码放入 <代码>SetUpFixture类。 (您可以使用旧版本执行此操作,但您需要执行与
SetUpFixture
相同的任何操作,或者将其复制到测试本身中。) .microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx" rel="noreferrer">
Debugger.Launch()
确实会导致以下对话框当您单击 NUnit 中的“运行”时会出现。然后,在项目打开的情况下选择正在运行的 Visual Studio 实例(我的屏幕截图中的第二个),然后选择将附加调试器,并且任何断点或异常都将显示在 Visual Studio 中。
If you are using NUnit 2.4 or newer you can put the following code in your
SetUpFixture
class. (You can do this with older versions but you will need to do whatever equivalent that has to theSetUpFixture
, or copy it in to the test itself.)What
Debugger.Launch()
does is cause the following dialog to show up when you click Run inside NUnit.You then choose your running instance of visual studio with your project open (the 2nd one in my screenshot) then the debugger will be attached and any breakpoints or exceptions will show up in Visual Studio.
在Nunit 3.0.1(我使用VS2013)中,从主菜单打开> 测试> 窗口> 测试资源管理器。 然后在“测试资源管理器”中,右键单击测试用例,您可能会看到:
希望这有帮助。
In Nunit 3.0.1 (I'm using VS2013), Open from main menu > Test > Windows > Test Explorer. Then in "Test explorer", right-click the test case, you might see:
Hope this helps.
安装 TestDriven.NET,这是 Visual Studio 的插件
从那里你可以右键单击你的单元测试程序集并单击“运行测试”以运行整个套件,右键单击 TestFixture 类以仅运行该类中的测试,或者右键单击测试方法以仅运行该方法。
如果您需要在调试模式下在测试中设置断点,您还可以选择使用调试器进行测试。
Install TestDriven.NET, which is a plugin for Visual Studio
From there you can right click on your unit test assembly and click Run Tests to run the whole suite, right click on a TestFixture class to run just the tests in that class, or right click on a Test method to run just that method.
You also have the option to Test With Debugger, if you need to breakpoint into your tests in debug mode.
尝试 NUnitit - 一个用于调试 NUnit 测试用例的开源 Visual Studio 插件
主页 - http://nunitit.codeplex.com/
Try NUnitit - a open source Visual Studio Addin for Debugging NUnit Test cases
HomePage - http://nunitit.codeplex.com/
从项目文件中删除 ProjectTypeGuids。
Remove ProjectTypeGuids from the project file.
现在有图片:
File -> 打开项目
从 bin 文件夹中选择您的测试
.dll
(C:\......\[SolutionFolder][ProjectFolder ]\bin\Debug\xxxxTests.dll
)转到 Visual Studio
调试 -> 附加到进程
(附加到进程窗口将打开)从列表中向下滚动并选择
nunit-agent.exe
,然后单击附加
此时,测试中的断点应该成熟红色(来自空心)。
在
Nunit Gui
上单击Run
,您应该会遇到断点...希望这可以节省您一些时间。
Now with pictures:
File -> Open Project
Select your test
.dll
from bin folder (C:\......\[SolutionFolder][ProjectFolder]\bin\Debug\xxxxTests.dll
)Go to Visual Studio
Debug -> Attach to process
(Attach to process window will open)From the list scroll down and select
nunit-agent.exe
then clickAttach
At this point breakpoints in your tests should turn ripe red (from hollow).
Click
Run
onNunit Gui
and you should get your breakpoint hit...Hope this saves you some time.
如果您能够使控制台/或 GUI 正常工作,但您的断点没有被命中,则可能是因为您的应用程序运行的 .NET 运行时与 NUnit 不同。 检查您的 nunit-console.exe.config / nunit.exe.config 是否指定了运行时。(配置与 nunit exe 位于同一目录中。)使用启动节点指定运行时:
If you are able to get the console / or GUI working, but your breakpoints are not being hit, it may be because your app is running a different .NET runtime than NUnit is. Check to see if your nunit-console.exe.config / nunit.exe.config has the runtime specified.(The configurations live in the same directory as the nunit exe's.) Specify the runtime using the startup node:
如果项目路径包含空格,例如路径
\bin\Debug\New Project\Quotes.Domain.Tests.dll
中的“New Project”,则将启动选项括起来 --> 命令行参数项目路径用双引号引起来。我花了很多时间来弄清楚这一点。
If project path contains spaces e.g. "New Project" in path
<path>\bin\Debug\New Project\Quotes.Domain.Tests.dll
then enclose the Start Option --> Command Line Arguments project path in double quotes.I spent a lot of time to figure this out.
关于Patrick McDonald先生所说的
我尝试申请我的测试类库,但在路径方面遇到一些错误,因此我尝试删除“命令行参数”,幸运的是它运行良好且符合预期。
Regarding what Mr. Patrick McDonald said
I tried to apply for my test class library but got some error regarding the path, so I tried to remove the 'Command Line Arguments', and luckily it worked well and as expected.
听起来您正在尝试使用错误的库。 仅当您使用的 dll 包含 TestFixtures 时,NUnit 才能启动。
在 TestDriven.Net 上+1。 我有机会多次使用它。
您可以根据 http://testdriven.net/purchase_licenses.aspx< 的许可证下载个人版本用于评估目的/a>.
It sounds like you are trying to use the wrong library. NUnit can only start if the dll you are using contains TestFixtures.
+1 on TestDriven.Net. I've had the chance to use it a number of times.
You can download the personal version for evaluations purposes according the the license at http://testdriven.net/purchase_licenses.aspx.
我在 MSTest 中遇到了同样的错误。 我发现在测试输出窗口中,某些测试有重复的ID,无法加载。 我删除了所有重复的测试,现在我可以在启动项目时运行测试。
I got the same error with MSTest. I found that in the Test Output window, some of the tests had duplicate IDs and could not be loaded. I removed all duplicate tests and now I was able to run the tests when i start the project.
现在还有一个扩展“Visual NUnit”,它允许您从 Visual Studio 中运行测试,就像内置测试框架处理一样。 在扩展管理器中查看它。
There is also an extension now "Visual NUnit" that will allow you to run the tests from within Visual studio much like the build in test framework handles. Check it out its in the extension manager.
打开Visual Studio ---> 您的项目---> 选择“属性”---> 选择“调试”--> 选择“启动外部程序”并在那里设置 NUnit 的路径(例如:启动外部程序 = C:\Program Files\NUnit 2.6.2\bin\nunit.exe) ----> 保存
设置后只需单击调试
Open Visual Studio ---> your Project---> Select 'Properties'---> Select 'Debug' --> Select 'Start external program' and set the path of your NUnit there(Eg: Start external program = C:\Program Files\NUnit 2.6.2\bin\nunit.exe) ---->Save
After setting this just click Debug
对我来说,解决方案是调整 nunit 配置文件。 要将 nunit 与 4.5-.Net 框架和 x64 构建选项一起使用,我必须在启动标记(支持的运行时版本)中添加一行。
之后,我可以通过右键单击 Testproject Debug -> 开始 启动新实例。
之前,我需要再次手动将项目附加到流程中。
我的调试属性是,
C:\Program Files (x86)\NUnit 2.6.4\bin\nunit.exe
带有要测试的 .dll 位置的参数。
更多信息:nunit 用于使用 .NET 4.0 进行测试
For me solution was to adapt nunit configuration file. To use nunit with 4.5-.Net framework and x64 build option, I had to add one line to startup tag (supported runtime-version).
Afterwards, I could start by right-click on the Testproject Debug -> Start new instance.
Before, I needed to again manually attach the project to the process.
My Debug properties were,
C:\Program Files (x86)\NUnit 2.6.4\bin\nunit.exe
with argument of the location of the .dll to be tested.
More information: nunit for testing with .NET 4.0
看看这是否有帮助..
如何在 Visual Studio 中添加 NUnit
(RighteousRant)虽然我个人不喜欢这种方法。如果您在测试驱动代码时需要调试器,那么这是一种“气味”,因为您没有足够的信心/知道代码如何工作& 需要调试器告诉你这一点。 如果做得正确,TDD 应该会让您不再需要调试器。 仅在极少数情况下或当您涉足其他人的代码时才使用“将调试器附加到 NUNit”。
See if this helps..
How to add NUnit in Visual Studio
(RighteousRant)Although personally I don't like this approach.. If you need a debugger while you are test-driving your code, it's a "smell" in that you do not have enough confidence/know how your code works & need the debugger to tell you that. TDD should free you from needing a debugger if done right. Use 'Attach debugger to NUNit' only for rare cases or when you are wading in someone else's code.