在循环中使用NUnit声明时,如何在错误消息中显示更多信息? [英] How can I display more info in an error message when using NUnit Assert in a loop?

查看:97
本文介绍了在循环中使用NUnit声明时,如何在错误消息中显示更多信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

[Test]
public void WidgetTest()
{
    foreach (Widget widget in widgets)
    {
        Assert.AreEqual(0, widget.SomeValue);
    }
}

如果其中一个断言失败,我将收到一条非常无用的错误消息,如下所示:

If one of the asserts fails, I will get a very unhelpful error message like the one below:

1) Test Failure : WidgetTest.TestSomeValue
  Expected: 0
  But was:  1

at WidgetTest.TestSomeValue()

所以,问题是,如何使NUnit显示更多有用的信息,例如小部件的名称或循环的迭代等?甚至行号也将更有帮助,因为它是自动运行的,我希望能够在不调试代码的情况下发现失败的断言.

So, the question is, how can I get NUnit to display more useful info, such as the name of the widget, or the iteration of the loop, etc? Even a line number would be more helpful, since this is run in automated manner and I'd like to be able to spot the failing assert without debugging into the code.

推荐答案

您也可以使用带消息的重载:

You can use the overload which takes a message as well:

Assert.AreEqual(0, widget.SomeValue,
                "Widget " + widget + " should have SomeValue of 0");

这篇关于在循环中使用NUnit声明时,如何在错误消息中显示更多信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆