异常断言 &在 VS 中调试 C# 项目

发布于 2024-08-18 09:58:58 字数 1047 浏览 10 评论 0原文

我们一直在使用 NUnit & VisualStudio 编写 C# .NET 代码已经有一段时间了。 的风格完成的

测试异常是按照旧语法

[Test]
[ExpectException(typeof(ExceptionType))] 
public void TestExceptionType()
{

}

:现在 NUnit 发布了 2.5.2 版本,其中引入了 Assert.Throws( Type ExpectedExceptionType, TestDelegate code ); 这使得异常测试成为可能更加灵活。我们的异常测试现在如下所示:

新语法:

[Test]
public void TestWithNullBufferArgument()
{
   ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => _testInstance.TestFunction(null));

   // now you can examine the exception and it's properties
   Assert.AreEqual(ex.Message, "Argument was null");
}

我们的问题是,如果使用 Assert.Throws,则在使用 NUnit(控制台或 GUI 运行程序)时,Visual Studio 将弹出一个窗口,显示未处理的异常来调试程序。

为了澄清这一点:我们已将包含单元测试的 VS 项目设置为在调试时运行 nunit-x86.exe。 (请参阅项目属性、调试选项卡,启动操作设置为运行 nunit-x86.exe)

这会阻止 NUnit 继续测试。可以通过按 F5 继续调试/单元测试,但这不是一个可行的解决方案。

有什么办法可以避免这种情况吗?在 Assert.Throws 周围放置 try...catch 块不会执行任何操作,因为异常发生在委托代码中。

我希望有人能对此有所启发。

We've been using NUnit & VisualStudio to write C# .NET code for a while now. Testing Exceptions was done in the style of

old syntax:

[Test]
[ExpectException(typeof(ExceptionType))] 
public void TestExceptionType()
{

}

Now NUnit has released version 2.5.2 which introduced Assert.Throws( Type expectedExceptionType, TestDelegate code ); This makes exception testing a whole lot more flexible. Our exception tests now look like this:

new syntax:

[Test]
public void TestWithNullBufferArgument()
{
   ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => _testInstance.TestFunction(null));

   // now you can examine the exception and it's properties
   Assert.AreEqual(ex.Message, "Argument was null");
}

Our problem is that if Assert.Throws is used Visual Studio will cough up a window showing an unhandled exception when NUnit (either console or GUI runner) is used to debug the program.

to clarify this: we've set the VS project containing the unit tests to run nunit-x86.exe when debugging. (See project properties, debugging tab, start action is set to run nunit-x86.exe)

This stops NUnit from continuing the tests. It is possible to continue debugging/unit testing by pressing F5 but this is not a viable solution.

Is there any way to avoid this? Putting a try...catch block around the Assert.Throws does nothing since the exception happens in the delegate code.

I hope someone can shed some light on this.

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

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

发布评论

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

评论(4

ヅ她的身影、若隐若现 2024-08-25 09:58:58

出现问题本身的原因很可能是您打开了“仅启用我的代码”选项(工具->选项->调试->常规->仅启用我的代码)。

“启用此功能后,调试器仅显示并单步执行用户代码(“我的代码”),忽略系统代码和其他经过优化或没有调试符号的代码”(请参阅​​“常规、调试、选项对话框")

通常,您有一个 nunit.framework.dll 的发布版本,它不会没有相应的 nunit.framework.pdb 文件。

因此有 2 个选项:

  1. 禁用“仅我的代码”功能

  2. 下载 nunit 的源代码(来自 http://www.nunit.org/index.php?p=download),以调试模式构建它们,放入所有 nunit.framework。 并在测试项目中引用该 nunit.framework.dll。

希望这有帮助。

The problem itself appears because most likely you have option Enable Just My Code turned on (Tools->Options->Debugging->General->Enable Just My Code).

"When this feature is enabled, the debugger displays and steps into user code ("My Code") only, ignoring system code and other code that is optimized or does not have debugging symbols" (see "General, Debugging, Options Dialog Box")

Normally you have a release version of nunit.framework.dll which does not have a corresponding nunit.framework.pdb file.

So there are 2 options:

  1. Disable "Just My Code" feature

  2. Download sources of nunit (from http://www.nunit.org/index.php?p=download), build them in debug mode, put all nunit.framework.* (dll, pdb, xml) into lib or other directory in your solution and reference that nunit.framework.dll in your test project.

Hope this helps.

最美的太阳 2024-08-25 09:58:58

同样的问题也困扰了我很长一段时间,我做了一些测试并发现了以下内容:

如果一个库(本例中为 nunit)是在调试信息设置为“none”的情况下编译的,那么如果执行类似于下面的构造当库和委托的代码抛出异常时,VS 就会停止抱怨用户代码未处理的异常。

库代码:

public static Exception Throws(TestDelegate code, string message)
{
    Exception caughtException = null;

    try
    {
        code();
    }
    catch (Exception ex)
    {
        caughtException = ex;
    }        

    return caughtException;
}

客户端代码:

private void btnTest_Click(object sender, EventArgs e)
{
  var ex = MyAssert.Throws(() => { throw new Exception(); }, "");    
}

将库项目的调试信息设置为“无”以外的任何其他选项可以解决问题,即调试器不会再因那些“未处理”的异常而停止。我使用 nunit 和我自己的手工库以及上面的代码对其进行了测试(从 nunit 的 Throws 方法中获取了一个片段)。我想这是VS的一个特性或者“特性”。

它留给我们的选择并不多:

  1. 按照之前的建议过滤异常

  2. 重新编译 nunit.framework.dll 以供本地使用,以避免那些烦人的停止

其他选择可能是联系 MS 或 NUnit 团队或两者,并要求他们调查/澄清问题并分别使用最低级别的调试信息编译 NUnit。

编辑:

又找到了一个选项。

  1. 就我而言,取消选中“抑制模块加载时的 JIT 优化”也可以解决问题,即使编译的库没有调试信息。但是,它仅在项目在发布配置中运行时才有效。

The same problem also annoyed me for quite some time, I did a few tests and found the following:

If a library (nunit in this case) is compiled with debug info set to 'none', then if construct similar to one below is executed withing the library and delegate's code throws an exception, then VS stops complaining about exception not handled by the user code.

Library code:

public static Exception Throws(TestDelegate code, string message)
{
    Exception caughtException = null;

    try
    {
        code();
    }
    catch (Exception ex)
    {
        caughtException = ex;
    }        

    return caughtException;
}

Client code:

private void btnTest_Click(object sender, EventArgs e)
{
  var ex = MyAssert.Throws(() => { throw new Exception(); }, "");    
}

Setting debug info of a library project to any other option other than 'none' resolves the problem i.e. debugger does not stop anymore on those kinda "unhandled" exceptions. I tested it with nunit and my own hand-rolled library with the above code (took a snippet from nunit's Throws method). I suppose it is a feature or a "feature" of VS.

It leaves us with not so many options:

  1. Filter exception as previously suggested

  2. Recompile nunit.framework.dll for local use, to avoid those annoying stops

Other options could be to contact either MS or NUnit teams or both and ask them to investigate/clarify issue and compile NUnit with minimal level of debugging info respectevily.

Edit:

Found one more option.

  1. In my case unchecking 'Suppress JIT optimization on module load' also does the trick, even if libraries compiled with no debug info. However it only works when project is run in release configuration.
白日梦 2024-08-25 09:58:58

我认为您被 NUnit 断言蒙蔽了双眼。您可以通过简单的 try/catch 实现相同的目标。

try
{
  _testInstance.TestFunction(null);
  Assert.Fail("The method should have thrown...");
}catch{}

现在,您已拥有所需的一切。如果未引发异常并且常规代码可以按预期处理异常,则失败。

I think you are being blinded by the NUnit assertion. You could achieve the same thing with a simple try/catch.

try
{
  _testInstance.TestFunction(null);
  Assert.Fail("The method should have thrown...");
}catch{}

Now, you have everything you need. You fail if the exception isn't thrown and your regular code can handle exceptions as expected.

救赎№ 2024-08-25 09:58:58

是否可以通过禁用异常来实现。
打开“调试/异常”菜单,然后搜索您的异常。

Could it be achievable by disabling the Exception.
Open Debug/Exceptions menu , and search for your Exception.

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