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

发布于 2024-11-25 14:18:38 字数 754 浏览 2 评论 0原文

我在从 VisualStudio 调试 NUnit 测试时遇到问题。我创建了一个空项目(控制台应用程序),然后添加了对 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);
        }
    }
}

接下来我遵循了此链接中给出的建议 如何从 Visual 在调试模式下运行 NUnit Studio?但该主题中的解决方案都不适合我。执行测试时我的断点都没有被击中。我尝试通过附加到进程以及使用带参数的外部程序运行项目来测试解决方案。

我可以做什么来调试我的单元测试?

I have a problem debugging an NUnit test from VisualStudio. I created an empty project (Console Application), then I added references to the NUnit library and wrote a simple test.

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? 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

紅太極 2024-12-02 14:18:38

直接使用 Visual Studio 运行或调试 NUnit 测试

看起来没有扩展!

只需配置您的测试项目,以便当您点击 F5(开始调试)或 Ctrl-F5(启动但不调试)时,它将自动启动 NUnit GUI 并执行其中的所有测试。如果遇到任何断点,您还可以简单地调试测试代码。

带有图像的分步指南 向您展示了具体的操作方法。

懒惰是一切发明之母 :-)

Running or debugging NUnit tests directly with Visual Studio

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. 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 :-)

腹黑女流氓 2024-12-02 14:18:38

假设您使用的是 Express Edition 以外的 Visual Studio 版本,则 TestDriven.NET 可能有用。

安装后 在

  1. 测试方法中设置一个断点
  2. 右键单击​​并从“测试方式”菜单中选择“调试器
  3. ” 调试器应该启动并命中断点

不幸的是,您不能在 Visual Studio Express 版本中使用此方法,因为 TestDriven.NET 是一个插件Visual Studio 和 Express 版本不支持使用插件

在控制台应用程序中运行测试

您还可以通过控制台应用程序在调试器中运行测试:

  1. 创建一个新的控制台应用程序
  2. 引用您的单元测试项目
  3. 在 Main 中控制台应用程序的方法创建测试装置的新实例,然后调用其中一种测试方法。例如,如果我有一个名为 MyTests 的装置和一个名为 Test1 的测试,我会写:

    var myTests = new MyTests();
    myTests.Test1();
    
  4. 在创建 MyTests 类实例的行处设置断点,然后按 F5

  5. 调试器将命中断点,然后您可以使用 F11 单步执行 TestFixture 的构造函数,或者单步执行测试本身

Assuming you're using a version of Visual Studio other than Express Edition then TestDriven.NET might be of use.

After installing it

  1. Set a breakpoint within your test method
  2. Right click and choose Debugger from the Test With menu
  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

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. For example if I have a fixture named MyTests and a test named Test1 I'd write:

    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

  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
一页 2024-12-02 14:18:38

如果您使用的是 NUnit 2.4,您可以将以下代码放入 SetUpFixture 类。 但您需要执行与 SetUpFixture 相同的任何操作,或者将其复制到测试本身中。)

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

(您可以使用旧版本执行此操作, .microsoft.com/en-us/library/system.diagnostics.debugger.launch.aspx" rel="noreferrer">Debugger.Launch() 确实会导致以下对话框当你出现时单击 NUnit 中的“运行”。

JIT 调试器对话框

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

If you are using NUnit 2.4 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 the SetUpFixture, or copy it in to the test itself.)

[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.

JIT Debugger Dialog

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.

美煞众生 2024-12-02 14:18:38

请查看 CodePlex 上的 NHarness

它是一个非常简单的、基于反射的测试运行器库,可以识别 NUnit 属性,并且可以从 Visual Studio Express 中的项目运行,从而允许调试测试。

它目前具有测试类级别的粒度,但据说很快就会添加方法级别的调用。

Have a look at NHarness on CodePlex.

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.

It currently has a test class level granularity, but method level calls are, supposedly, going to be added soon.

水晶透心 2024-12-02 14:18:38

NUnit 2.5.3 也有同样的问题,最终找到了不同的解决方案。看看这是否适合您:

打开 NUnit GUI 并进入“工具”菜单“设置...”对话框。在设置树中选择测试加载器部分的程序集隔离子部分。将默认进程模型设置为直接在 NUnit 进程中运行测试。

我将其设置为在单个单独的进程中运行测试,这就是为什么调试器无法将我的待测 dll 链接到用于调试它的符号。我仍然使用每个程序集使用单独的 AppDomain 作为我的默认域使用。

Had the same problem with NUnit 2.5.3, and eventually found a different solution. See if this works for you:

Open NUnit GUI and go into Tools menu Settings... dialog. Select Assembly Isolation subsection of Test Loader section in Settings tree. Set the Default Process Model to Run tests dierctly in the NUnit process.

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. I am still using Use a sperate AppDomain per Assembly for my Default Domain Usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文