How can I display more info in an error message when using NUnit Assert in a loop?

↘锁芯ラ 提交于 2020-01-02 01:18:07

问题


Consider the following code:

[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()

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.


回答1:


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

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


来源:https://stackoverflow.com/questions/3038733/how-can-i-display-more-info-in-an-error-message-when-using-nunit-assert-in-a-loo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!