简体   繁体   English

NUnit声明抛出控制台输出

[英]NUnit Assert.Throws swallows console output

Here is a sample program to reproduce the issue: 这是重现此问题的示例程序:

[TestFixture] // NUnit ver. 3.9
public class IssueTest
{
    [Test]
    public void AssertThrowsConsoleIssue()
    {
        Console.WriteLine(1);
        Assert.Throws<Exception>(() =>
        {
            Console.WriteLine(2);
            throw new Exception("test");
        });
        Console.WriteLine(3);
    }
}

I expect an output to be 我希望输出是

1
2
3

But in reality it is 但实际上是

1
3

I encountered this issue when was debugging a unit test and noticed that some console log output was missing. 我在调试单元测试时遇到此问题,并注意到缺少某些控制台日志输出。

As for me this looks like a bug. 对我来说,这似乎是一个错误。 Is there a way to prevent Assert.Throws from swallowing Console output? 有没有一种方法可以防止Assert.Throws吞噬Console输出?

This is a bug and you just found it! 这是一个错误,您才发现它! Usually bugs need to be confirmed but seeing your example I can understand exactly what is happening. 通常需要确认错误,但是看到您的示例,我可以确切地了解正在发生的事情。

Throws creates a temporary test result and context, which we throw away. Throws会创建一个临时的测试结果和上下文,我们将其丢弃。 If we didn't do that, the thrown exception would be recorded in the result. 如果我们不这样做,则抛出的异常将记录在结果中。 Obviously, we need to save some things, including the text output, from that temporary result. 显然,我们需要从该临时结果中保存一些内容,包括文本输出。 It will be cool if you can file a bug for this on GitHub. 如果您可以在GitHub上提交一个错误,这将很酷。

As a workaround, you can use an async delegate as suggested by Roman. 解决方法是,可以使用Roman建议的异步委托。 If you do that, Assert.Throws will then use different code that doesn't create a throwaway context and result. 如果这样做, Assert.Throws将使用不同的代码,这些代码不会创建一次性的上下文和结果。

If you file a bug on this, I'll try to give you a better workaround on the issue. 如果您对此提交了错误,我将尝试为您提供一个更好的解决方案。 ;-) ;-)

I have the same issue, Roman's hack fixed my problem. 我有同样的问题,Roman的破解解决了我的问题。 Waiting for the next release with the fix :) 等待修复的下一个版本:)

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

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