简体   繁体   English

如何防止预期异常破坏调试测试运行?

[英]How to prevent expected exceptions from breaking the debug test run?

When running MSTEST unit tests in debug mode, the execution stops in every expected exception that is thrown.在调试模式下运行 MSTEST 单元测试时,执行会在抛出的每个预期异常中停止。 My test looks like this我的测试看起来像这样

[TestMethod()]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowExceptionWhenPassingNull()
{
    object data = null;
    target.CheckNull(data);
}

the target method looks like this:目标方法如下所示:

public void CheckNull(object data)
{
    if (ReferenceEquals(null, data))
    {
        throw new ArgumentNullException("data");
    }
} // test run breaks here: ArgumentNullException was unhandled by user code

Did you try running the tests using ctrl-R ctrl-T instead of ctrl-R T ? 您是否尝试使用ctrl-R ctrl-T而不是ctrl-R T来运行测试?

EDIT If it's not a keyboard shortcut issue, then check out this link. 编辑如果它不是键盘快捷键问题,请查看链接。 You could try the following as noted there: 您可以尝试以下所述:

  1. Disable "break on user unhandled exceptions" for the exception types you you are encountering here (via Debug -> Exceptions) 对于您在此处遇到的异常类型,禁用“中断用户未处理的异常”(通过调试 - >异常)
  2. Disable "break on user unhandled exceptions" for all exceptions (via Debug -> Exceptions) 对所有异常禁用“中断用户未处理的异常”(通过Debug - > Exceptions)
  3. Disable "Just My Code" 禁用“只是我的代码”

CTRL + RA works for me without changing any option. CTRL + RA适用于我,无需更改任何选项。

I think the problem you have is because you are running the test project from the Start Debugging (F5) button on the toolbar. 我认为您遇到的问题是因为您正在从工具栏上的“开始调试”(F5)按钮运行测试项目。 If you click the green play button, you will stop in every exception, even expected ones. 如果单击绿色播放按钮,您将在每个例外中停止,即使是预期的例外。

To run all tests without stopping in every exception, click on: Test -> Run -> All Tests in Solution or use the shortcut: CTRL + R, A 要在不停止每个异常的情况下运行所有​​测试,请单击:测试 - >运行 - >解决方案中的所有测试或使用快捷方式:CTRL + R,A

With the test results window open, CTRL + R, D also works. 打开测试结果窗口,CTRL + R,D也可以。 In the test results window it becomes clear the difference between Run Tests and Debug tests. 在测试结果窗口中,运行测试和调试测试之间的区别变得清晰。

@dcp's suggestion looks like it would work for MSTEST, but you might want to consider getting TestDriven.Net . @ dcp的建议看起来适用于MSTEST,但您可能需要考虑获取TestDriven.Net I use it as a test runner almost exclusively and don't have this problem using the right-click "Run Tests in Debug" mode. 我几乎只使用它作为测试运行器,并且使用右键单击“在调试中运行测试”模式时没有出现此问题。 In addition I find it to be more convenient than the built-in test runner in almost all circumstances. 此外,我发现它几乎在所有情况下都比内置测试跑步者更方便。

A recent 2022 article from Microsoft on shows this: Microsoft 上最近的一篇 2022 年文章显示了这一点:

In the Exception Settings window ( Debug > Windows > Exception Settings ), expand the node for a category of exceptions, such as Common Language Runtime Exceptions .Exception Settings window( Debug > Windows > Exception Settings )中,展开异常类别的节点,例如Common Language Runtime Exceptions Then select the check box for a specific exception within that category, such as System.AccessViolationException .然后 select 该类别中特定异常的复选框,例如System.AccessViolationException You can also select an entire category of exceptions.您还可以 select 整个类别的异常。

Example picture from the same article:来自同一篇文章的示例图片: 在此处输入图像描述

If this happens to work, I'll add an example to this answer as well.如果这恰好有效,我也会在此答案中添加一个示例。

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

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