简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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