【问题标题】:xunit core in try catch block is not working righttry catch 块中的 xunit 核心无法正常工作
【发布时间】:2018-01-02 13:10:07
【问题描述】:

[重复]Assert in Try..Catch block is caught

当我在 VS2017 的 NetCore1.1 中使用 Xunit Core 时如下:

     try
        {
            Assert.True(1<0," server is false");//vs2017 show green(test through)

        }
        catch (Exception e)
        {
            while (e != null)
            {
                _outputHelper.WriteLine(e.Message);
                e = e.InnerException;
            }
        }

        Assert.True(1 < 0, "current value is false");// vs2017 show red(test failed

结果是图标是绿色的,不是红色的,第一个Assert.True不正确(失败)。如下:

如果添加第二个Assert.True 它是红色的;

)

这很奇怪,对吗?我认为这是一个错误。

【问题讨论】:

    标签: c# unit-testing .net-core visual-studio-2017 xunit


    【解决方案1】:

    这不是错误。

    所有单元测试框架,包括 xUnit.net,都对失败的断言使用异常。你盲目地捕捉并吞下了所有的异常;这将包括失败断言的异常。

    也不清楚你想用这段代码完成什么。也许这是为了说明的目的而进行的简化...?

    【讨论】:

    • 嗨@Brad Wilson,感谢您的回复?我想测试 EF Demo 功能,here 是完整的演示代码。有没有好的模式可以实现?
    • 只是不要抓住。抛出的任何异常都将显示为失败。您可以将 try/finally 留在资源清理中,而无需捕获。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多