简体   繁体   English

如何在Visual Studio 2010中调试Nunit测试

[英]How to debug Nunit test in Visual Studio 2010

I have a problem debugging an NUnit test from VisualStudio. 我在调试VisualStudio的NUnit测试时遇到问题。 I created an empty project (Console Application), then I added references to the NUnit library and wrote a simple test. 我创建了一个空项目(控制台应用程序),然后我添加了对NUnit库的引用并编写了一个简单的测试。

namespace ReimplementingLinq.Tests
{
    [TestFixture]
    public class WhereTest
    {
        [Test]
        public void SimpleFiltering()
        {
            int[] source = { 1, 2, 3, 4, 2, 8, 1 };
            var result = source.Where(val => val < 4);
            int[] expected = {1,2,3,4};
            CollectionAssert.AreEqual(expected, result);
        }
    }
}

Next I followed the advice given in this link How do I run NUnit in debug mode from Visual Studio? 接下来我遵循此链接中给出的建议如何从Visual Studio以调试模式运行NUnit? but none of the solutions in that topic work for me. 但是那个主题的解决方案都不适合我。 None of my breakpoints are hit while performing the test. 执行测试时没有遇到任何断点。 I tried testing the solution by attaching to the process and also by running the project with an external program with arguments. 我尝试通过附加到流程以及使用带参数的外部程序运行项目来测试解决方案。

What can I do to debug my unit test? 我该怎么做才能调试我的单元测试?

Running or debugging NUnit tests directly with Visual Studio 直接使用Visual Studio运行或调试NUnit测试

Look ma' no extensions! 看马'没有扩展名!

Simply configure your test project so that when you hit F5 (Start debugging) or Ctrl-F5 (Start without debugging) it will automatically start NUnit GUI and execute all tests within it. 只需配置您的测试项目,以便在按F5 (开始调试)或Ctrl-F5 (无需调试时启动)时,它将自动启动NUnit GUI并执行其中的所有测试。 If any breakpoints get hit, you will also be able to simply debug your test code. 如果遇到任何断点,您也可以简单地调试测试代码。

A step-by-step guide with images shows you exactly how to do it. 带图像的分步指南向您展示了如何操作。

Laziness is the mother of all invention :-) 懒惰是所有发明的母亲 :-)

Assuming you're using a version of Visual Studio other than Express Edition then TestDriven.NET might be of use. 假设您使用的是Visual Studio以外的Visual Studio版本,则可能会使用TestDriven.NET

After installing it 安装后

  1. Set a breakpoint within your test method 在测试方法中设置断点
  2. Right click and choose Debugger from the Test With menu 右键单击并从Test With菜单中选择Debugger
  3. The debugger should launch and hit your breakpoint 调试器应该启动并命中你的断点

Unfortunately you can't use this method with Express editions of visual studio because TestDriven.NET is a plugin for visual studio and the Express editions do not support the use of plugins 遗憾的是,您不能将此方法用于Visual Studio的Express版本,因为TestDriven.NET是Visual Studio的插件,而Express版本不支持使用插件

Running a test within a console app 在控制台应用程序中运行测试

You can also run a test in the debugger via a console application: 您还可以通过控制台应用程序在调试器中运行测试:

  1. Create a new console application 创建一个新的控制台应用程
  2. Reference your unit tests project 参考您的单元测试项目
  3. Inside the Main method of the console application create a new instance of your test fixuture and then call one of the test methods. 在控制台应用程序的Main方法内部,创建测试fixuture的新实例,然后调用其中一个测试方法。 For example if I have a fixture named MyTests and a test named Test1 I'd write: 例如,如果我有一个名为MyTests的夹具和一个名为Test1的测试,我会写:

     var myTests = new MyTests(); myTests.Test1(); 
  4. Set a breakpoint at the line where you create an instance of the MyTests class and press F5 在创建MyTests类的实例的行处设置断点,然后按F5

  5. The debugger will hit your breakpoint and then you can use F11 to step into your TestFixture's constructor, or step over that into the test itself 调试器将命中你的断点,然后你可以使用F11进入你的TestFixture的构造函数,或者将其转入测试本身

If you are using NUnit 2.4 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中。

Have a look at NHarness on CodePlex . 看看CodePlex上的NHarness

It's a very simple, reflection based, test runner library, that recognises NUnit attributes, and can be run from a project within Visual Studio Express, allowing debug testing. 它是一个非常简单,基于反射的测试运行器库,可识别NUnit属性,可以从Visual Studio Express中的项目运行,从而允许进行调试测试。

It currently has a test class level granularity, but method level calls are, supposedly, going to be added soon. 它目前具有测试类级别的粒度,但据推测,方法级调用将很快添加。

Had the same problem with NUnit 2.5.3, and eventually found a different solution. 与NUnit 2.5.3有同样的问题,并最终找到了不同的解决方案。 See if this works for you: 看看这是否适合您:

Open NUnit GUI and go into Tools menu Settings... dialog. 打开NUnit GUI并进入“工具”菜单“设置...”对话框。 Select Assembly Isolation subsection of Test Loader section in Settings tree. 在Settings树中选择Test Loader部分的Assembly Isolation子部分。 Set the Default Process Model to Run tests dierctly in the NUnit process. 在NUnit进程中将Default Process Model设置为Directly。

I had it set to Run tests in a single seperate process, which is why the debugger could not link my dll under test to the symbols for debugging it. 我将它设置为在单个单独的进程中运行测试,这就是为什么调试器无法将我测试的dll链接到用于调试它的符号。 I am still using Use a sperate AppDomain per Assembly for my Default Domain Usage. 我仍然使用每个程序集使用sperate AppDomain作为我的默认域使用。

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

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