简体   繁体   English

为什么 Assert.Multiple ‘记住’一个已经捕获的断言失败?

[英]Why does Assert.Multiple 'remember' an already caught assertion failure?

The following code:以下代码:

using NUnit.Framework;

namespace Sandbox {
    public class Program {
        public static void Main(string[] args) {
            try {
                Assert.Fail("Fail!");
            } catch (Exception e) {
                Console.WriteLine("Expected exception");
            }

            try {
                Assert.Multiple(() => { Assert.True(true); });
            } catch (Exception e) {
                Console.WriteLine("Unexpected exception: " + e.Message);
            }
        }
    }
}

prints印刷

Expected exception预期异常
Unexpected exception: Fail!意外异常:失败!

Why is the AssertionException , which is caught in the first block, picked up a second time by Assert.Multiple ?为什么在第一个块中捕获的AssertionExceptionAssert.Multiple第二次拾取? If I just do Assert.True(true);如果我只是做Assert.True(true); , no exception is thrown. , 不会抛出异常。

I don't think it matters, but I'm using .NET 6.0.我认为这不重要,但我使用的是 .NET 6.0。

In (very) old versions, NUnit used an exception in order to (1) report errors and then (2) terminate the tests for the current test class.在(非常)旧的版本中,NUnit 使用异常来 (1) 报告错误,然后 (2) 终止当前测试 class 的测试。

Back in 2016, however, these two functions were separated.然而,早在 2016 年,这两个功能就分开了。 This had to be done in order to support Assert.Multiple and to a lesser extent the warning asserts Warn.If and Warn.Unless .必须这样做才能支持Assert.Multiple并且在较小程度上警告断言Warn.IfWarn.Unless Those features require execution to be continued after the error is reported.这些功能需要在报告错误后继续执行。

It should be noted that users have been advised to avoid catching NUnit exceptions for about 20 years.应该注意的是,大约 20 年来,用户一直被建议避免捕获 NUnit 异常。 That's because those exceptions are an internal implementation detail, subject to change.那是因为这些异常是内部实现细节,可能会发生变化。 I should say that the NUnit developers have been giving that advice for many years.我应该说 NUnit 开发人员多年来一直在提供该建议。 Other people, unfortunately, have often published code that involved catching them.不幸的是,其他人经常发布涉及捕获它们的代码。 :-) Anyway, six or seven years ago the inevitable happened: that implementation detail changed. :-) 不管怎样,六七年前不可避免的事情发生了:实现细节发生了变化。

It's a general principal that you should never try to handle or suppress an exception unless you understand what it means.除非您理解异常的含义,否则永远不要尝试处理或抑制异常,这是一条通用原则。 The meaning of the assertion thrown by a test failure is (now) that the error has been recorded and the fixture is about to be terminated.测试失败抛出的断言的意思是(现在)错误已经被记录下来,fixture 即将被终止。

That answers your "Why" question but most likely isn't very satisfying.这回答了您的“为什么”问题,但很可能不是很令人满意。 :-) :-)

I suspect this may be an XY problem.我怀疑这可能是一个 XY 问题。 Perhaps you might post another question about exactly what you are trying to accomplish and we could have another go at it.也许您可能会发布另一个关于您要完成的确切目标的问题,我们可能会有另一个 go。

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

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