简体   繁体   English

如何从Visual Studio以调试模式运行NUnit?

[英]How do I run NUnit in debug mode from Visual Studio?

I've recently been building a test framework for a bit of C# I've been working on. 我最近一直在构建一个C#的测试框架,我一直在努力。 I have NUnit set up and a new project within my workspace to test the component. 我在我的工作区中设置了NUnit和一个新项目来测试组件。 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. 如果我从Nunit(v2.4)加载我的单元测试,一切运行良好,但我已经达到了在调试模式下运行并设置一些断点非常有用的地步。

I've tried the suggestions from several guides which all suggest changing the 'Debug' properties of the test project: 我已经尝试了几个指南中的建议,这些指南都建议更改测试项目的“Debug”属性:

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. 我在那里使用控制台版本,但也试过调用GUI。 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? 这是因为我通常将\\ DSP.nunit加载到Nunit GUI中并且这是测试所在的位置吗?

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? 我开始认为问题可能是VS想要运行它自己的测试框架,这就是为什么它没能找到NUnit测试?

Edit : To those asking about test fixtures, one of my .cs files in the TestDSP project looks roughly like this: 编辑 :对于那些询问测试装置的人,TestDSP项目中的一个.cs文件看起来大致如下:

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 ;-) ...我对C#和NUnit测试框架都很陌生,所以我完全有可能错过了一些关键的信息;-)

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. 如果您选择Other Languages -> Visual C# -> Test -> Test Project ...当您选择项目类型时,Visual Studio将尝试使用它自己的测试框架。据我所知。 You should pick a normal C# class library project instead and then the instructions in my selected answer will work. 您应该选择一个正常的 C#类库项目,然后我选择的答案中的说明将起作用。

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. 当我需要调试我的NUnit测试时,我只需使用“Debug | Attach to Process”连接到NUnit GUI应用程序nunit-agent.exe ,然后从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? 我误解了你的问题,还是会对你有用?

I use the same technique as you are trying Jon, without the /assembly flag, ie 我使用与你尝试Jon相同的技术,没有/ assembly标志,即

Start External Program: C:\Program Files\NUnit 2.4.8\bin\nunit.exe

Command line arguments: "<path>\bin\Debug\Quotes.Domain.Tests.dll"

Does TestDSP.dll contain all your TestFixtures? TestDSP.dll是否包含所有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 由于我的测试项目不是解决方案中的启动项目,我通过右键单击测试项目并选择Debug - > Start New Instance来运行我的测试

Simply remove the line that looks like 只需删除看起来像的线条

<ProjectTypeGuids>
    {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
</ProjectTypeGuids>

from your project file. 从你的项目文件。 This line basically tells VS.Net that it's a Test project, thus the "Cannot start test project". 这条线基本上告诉VS.Net它是一个测试项目,因此“无法启动测试项目”。 FYI here the 1st Guid says "it's a test", the 2nd says "it's C#". 仅在这里,第一个Guid说“这是一个测试”,第二个说“它是C#”。 For information on those Guids: http://www.mztools.com/Articles/2008/MZ2008017.aspx 有关这些Guids的信息: http ://www.mztools.com/Articles/2008/MZ2008017.aspx

In addition to the answer provided by @Justin here are some more details for NUnit 2.6. 除了@Justin提供的答案之外,还有NUnit 2.6的更多细节。

Using NUnit 2.6 attach to nunit.exe or nunit-console.exe and NOT the agent. 使用NUnit 2.6附加到nunit.exe或nunit-console.exe而不是代理。 The configuration noted by @Justin is slightly different. @Justin注意到的配置略有不同。 Below is an example from nunit.exe.config (same for nunit-console.exe.config). 以下是nunit.exe.config中的示例(对于nunit-console.exe.config也是如此)。

<startup useLegacyV2RuntimeActivationPolicy="true">
  <!-- Comment out the next line to force use of .NET 4.0 -->
  <supportedRuntime version="v2.0.50727" />  
  <supportedRuntime version="v4.0.30319" />
</startup>

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. 对于.NET 4测试项目,要获得断点,您必须注释掉或删除v2.0行,如评论所示。 Once I did that I was able to debug the .NET 4.0 test project. 一旦我这样做,我就能够调试.NET 4.0测试项目。

If you are using NUnit 2.4 or newer you can put the following code in your SetUpFixture class. 如果您使用的是NUnit 2.4或更高版本,则可以将以下代码放入SetUpFixture类中。 (You can do this with older versions but you will need to do whatever equivalent that has to the SetUpFixture , or copy it in to the test itself.) (您可以使用旧版本执行此操作,但您需要执行与SetUpFixture相同的任何操作,或将其复制到测试本身。)

[SetUpFixture]
public class SetupFixtureClass
{
    [SetUp]
    public void StartTesting()
    {
        System.Diagnostics.Debugger.Launch();
    }
}

What Debugger.Launch() does is cause the following dialog to show up when you click Run inside NUnit. Debugger.Launch()所做的是在单击NUnit内部运行时显示以下对话框。

JIT调试器对话框

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. 然后,在项目打开的情况下选择正在运行的visual studio实例(我的屏幕截图中的第二个),然后将附加调试器,并且任何断点或异常都将显示在Visual Studio中。

In Nunit 3.0.1 (I'm using VS2013), Open from main menu > Test > Windows > Test Explorer. 在Nunit 3.0.1(我正在使用VS2013)中,从主菜单>测试> Windows>测试资源管理器打开。 Then in "Test explorer", right-click the test case, you might see: 然后在“测试资源管理器”中,右键单击测试用例,您可能会看到: 在此输入图像描述

Hope this helps. 希望这可以帮助。

Install TestDriven.NET , which is a plugin for Visual Studio 安装TestDriven.NET ,它是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. 从那里,您可以右键单击您的单元测试程序集,然后单击Run Tests运行整个套件,右键单击TestFixture类以仅运行该类中的测试,或者右键单击Test方法以仅运行该方法。

You also have the option to Test With Debugger, if you need to breakpoint into your tests in debug mode. 如果需要在调试模式下断点测试,还可以选择Test With Debugger。

Try NUnitit - a open source Visual Studio Addin for Debugging NUnit Test cases 试试NUnitit - 一个用于调试NUnit测试用例的开源Visual Studio Addin

HomePage - http://nunitit.codeplex.com/ 首页 - http://nunitit.codeplex.com/

从项目文件中删除ProjectTypeGuids。

Now with pictures: 现在有图片:

  1. Run NUnit gui ( Download 2.6.2 from here ) then go to File -> Open Project 运行NUnit gui( 从此处下载2.6.2 ),然后转到File -> Open Project

在此输入图像描述

  1. Select your test .dll from bin folder ( C:\\......\\[SolutionFolder][ProjectFolder]\\bin\\Debug\\xxxxTests.dll ) 从bin文件夹中选择你的测试.dllC:\\......\\[SolutionFolder][ProjectFolder]\\bin\\Debug\\xxxxTests.dll

  2. Go to Visual Studio Debug -> Attach to process (Attach to process window will open) 转到Visual Studio Debug -> Attach to process (将打开附加到进程窗口)

  3. From the list scroll down and select nunit-agent.exe then click Attach 从列表中向下滚动并选择nunit-agent.exe然后单击“ Attach

在此输入图像描述

  1. At this point breakpoints in your tests should turn ripe red (from hollow). 此时,测试中的断点应变为成熟的红色(从空心处)。

  2. Click Run on Nunit Gui and you should get your breakpoint hit... 点击Nunit Gui上的Run ,你应该得到你的断点...

Hope this saves you some time. 希望这能为您节省一些时间。

If project path contains spaces eg "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. 如果项目路径包含空格,例如路径<path>\\bin\\Debug\\New Project\\Quotes.Domain.Tests.dll “New Project”,则将Start Option - > Command Line Arguments项目路径用双引号括起来。

I spent a lot of time to figure this out. 我花了很多时间来解决这个问题。

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. 如果您能够使控制台/或GUI工作,但您的断点没有被击中,可能是因为您的应用程序运行的是与NUnit不同的.NET运行时。 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: 检查您的nunit-console.exe.config / nunit.exe.config是否指定了运行时。(这些配置与nunit exe的目录位于同一目录中。)使用启动节点指定运行时:

<configuration>
    <startup>
       <supportedRuntime version="4.0" />
    </startup>

Regarding what Mr. Patrick McDonald said 关于帕特里克麦克唐纳先生所说的话

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 由于我的测试项目不是解决方案中的启动项目,我通过右键单击测试项目并选择Debug - > Start New Instance来运行我的测试

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. 我试图申请我的测试类库但是在路径上有一些错误,所以我试图删除'命令行参数',幸运的是它运行良好并且符合预期。

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. 现在还有一个扩展“Visual NUnit”,它允许您从Visual Studio中运行测试,就像构建测试框架句柄一样。 Check it out its in the extension manager. 在扩展管理器中查看它。

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 打开Visual Studio --->你的项目--->选择'属性'--->选择'调试' - >选择'启动外部程序'并在那里设置你的NUnit的路径(例如:启动外部程序= C :\\ Program Files \\ NUnit 2.6.2 \\ bin \\ nunit.exe)---->保存

After setting this just click Debug 设置完成后,单击Debug

I got the same error with MSTest. 我和MSTest有同样的错误。 I found that in the Test Output window, some of the tests had duplicate IDs and could not be loaded. 我发现在“ 测试输出”窗口中,某些测试具有重复的ID,无法加载。 I removed all duplicate tests and now I was able to run the tests when i start the project. 我删除了所有重复的测试,现在我能够在启动项目时运行测试。

For me solution was to adapt nunit configuration file. 对我来说,解决方案是适应nunit配置文件。 To use nunit with 4.5-.Net framework and x64 build option, I had to add one line to startup tag (supported runtime-version). 要将nunit与4.5-.Net框架和x64构建选项一起使用,我必须在启动标记中添加一行(支持的运行时版本)。

<startup useLegacyV2RuntimeActivationPolicy="true">
        <!-- Comment out the next line to force use of .NET 4.0 -->
        <supportedRuntime version="v4.0.30319" />
</startup>

Afterwards, I could start by right-click on the Testproject Debug -> Start new instance. 然后,我可以通过右键单击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. 我的调试属性是,C:\\ Program Files(x86)\\ NUnit 2.6.4 \\ bin \\ nunit.exe,带有要测试的.dll位置的参数。

More information: nunit for testing with .NET 4.0 更多信息: 用于使用.NET 4.0进行测试的nunit

It sounds like you are trying to use the wrong library. 听起来你正试图使用​​错误的库。 NUnit can only start if the dll you are using contains TestFixtures. NUnit只能在您使用的dll包含TestFixtures时启动。

+1 on TestDriven.Net. TestDriven.Net上的+1。 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 . 您可以根据http://testdriven.net/purchase_licenses.aspx上的许可证下载个人版本以进行评估。

See if this helps.. How to add NUnit in Visual Studio 看看这是否有帮助.. 如何在Visual Studio中添加NUnit

(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. (RighteousRant)虽然我个人不喜欢这种方法..如果你在测试驱动你的代码时需要一个调试器,那就是“气味”,因为你没有足够的信心/知道你的代码如何工作并且需要调试器告诉你。 TDD should free you from needing a debugger if done right. 如果做得好,TDD应该让你免于需要调试器。 Use 'Attach debugger to NUNit' only for rare cases or when you are wading in someone else's code. 使用'将调试器附加到NUNit'仅在极少数情况下或当您涉及其他人的代码时。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM