在 Visual Studio C# Express 2010 中调试 Nunit 测试

发布于 2024-11-27 22:49:26 字数 678 浏览 2 评论 0原文

我遵循这个建议来调试 NUnit 测试。

http://www.blackwasp.co.uk/NUnitCSharpExpress.aspx

但是,我有几个执行 Assert.Throws<...> 的测试,这会导致调试器在我正在测试的异常发生时中断,而实际上我希望它在异常发生在外部时中断那些电话。

如何让调试器忽略这些方法中引起的异常?


编辑:我已经尝试过以下方法,但不起作用!

[Test]
public void InstanciatingWithNullParameterThrowsException()
{
    try
    {
        Assert.Throws<ArgumentNullException>(() => new CachedStreamingEnumerable<int>(null));
        // This still throws and stops be being able to debug tests called after this one
    }
    catch
    {

    }
}

I've followed this advice to get debugging working for NUnit tests.

http://www.blackwasp.co.uk/NUnitCSharpExpress.aspx

However, i have several tests that do Assert.Throws<...>, which causes the debugger to break when the exception i'm testing for occurs, when really i want it to break if an exception occurs outside of those calls.

How can i get the debugger to ignore exceptions caused from within these kinds of methods?


EDIT: I've event tried the below, which doesn't work!

[Test]
public void InstanciatingWithNullParameterThrowsException()
{
    try
    {
        Assert.Throws<ArgumentNullException>(() => new CachedStreamingEnumerable<int>(null));
        // This still throws and stops be being able to debug tests called after this one
    }
    catch
    {

    }
}

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

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

发布评论

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

评论(4

错々过的事 2024-12-04 22:49:26

这是对我有用的(虽然在 Visual Studio Professional 中,而不是 Express,但我想这应该不重要)。

  • 按照 Ninjapig 的建议调出“例外”对话框。

  • 点击Add...按钮,打开“New Exception”对话框。

  • 在下拉框中选择“公共语言运行时异常”
  • 在编辑框中输入“NUnit.Framework.AssertionException”。
  • 单击确定关闭“新建异常”对话框。
  • 返回“异常”对话框,确保两个复选框(Thrown User-unhandled)均未选中

现在,调试器应该完全忽略 NUnit 断言失败(即抛出、捕获或未捕获,NUnit.Framework.AssertionException)。

UPDATE:这只会阻止闯入调试器,它不能忽略异常本身;即它不会改变实际的程序流程。除了更改、替换或封装 try-catch 块中的断言调用之外,我认为没有任何方法可以实现这一目标(至少不能自动实现)。

Here is what worked for me (although in Visual Studio Professional, not Express, but I guess that should not matter).

  • Bring up the "Exceptions" Dialog as suggested by Ninjapig.

  • Click on the Add... Button, to open the "New Exception" dialog.

  • Select "Common Language Runtime Exceptions" in the drop down box
  • In the Edit box enter "NUnit.Framework.AssertionException".
  • Click OK to close the "New Exception" dialog.
  • Back in the "Exceptions" dialog make sure that both checkboxes (Thrown and User-unhandled) are unchecked.

Now, the debugger should completely ignore a NUnit assertion failure (i.e. a thrown, caught or not, NUnit.Framework.AssertionException).

UPDATE: This will only prevent from breaking into the debugger, it cannot ignore the exception itself; i.e. it will not alter the actual program flow. Appart from changing or replacing or encapsulating the Assert-calls in try-catch blocks, I don't think there is anything that can achieve that (at least not automatically).

陌上青苔 2024-12-04 22:49:26

我不确定 VS2010 Express 是否有此选项,但您可以选择要中断的例外。

转到“调试”菜单,然后选择“异常”
Menu options

从这里您可以选择要中断的异常
例外选择窗口

I'm uncertain if VS2010 Express has this option, but you can choose the exceptions to break on .

Go to the 'Debug' menu, then select 'Exceptions'
Menu options

and from here you can select what exceptions to break on
Exceptions selection window

时光无声 2024-12-04 22:49:26

我最终引用了 nunit-gui-runner.dll 并像这样调用它,

NUnit.Gui.AppEntry.Main(new string[] { Dll });

这会调出 NUnit gui。然后我可以运行我感兴趣的特定测试。

I've ended up referencing nunit-gui-runner.dll and invoking it like

NUnit.Gui.AppEntry.Main(new string[] { Dll });

This brings up the NUnit gui. I can then run the specific test i'm interested in.

心的位置 2024-12-04 22:49:26

我也有同样的问题。尽管您的原始方法(不需要 try...catch 块)适用于大多数异常类型,但 ArgumentNullException 不起作用。我这样修复它:

[Test]
public void InstanciatingWithNullParameterThrowsException()
{
    bool isArgumentNullExceptionThrown = false;
    try
    {
       new CachedStreamingEnumerable<int>(null);
    }
    catch (ArgumentNullException)
    {
        isArgumentNullExceptionThrown = true;
    }
    Assert.That(isArgumentNullExceptionThrown);
}

它不是那么优雅,但它似乎确实有效。

I had the same problem. Although your original approach (without the need for a try...catch block) works for most exception types, ArgumentNullException doesn't work. I fixed it like this:

[Test]
public void InstanciatingWithNullParameterThrowsException()
{
    bool isArgumentNullExceptionThrown = false;
    try
    {
       new CachedStreamingEnumerable<int>(null);
    }
    catch (ArgumentNullException)
    {
        isArgumentNullExceptionThrown = true;
    }
    Assert.That(isArgumentNullExceptionThrown);
}

It's not as elegant, but it does seem to work.

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