Are there soft assertions in NUnit?

Is there such a thing as soft assertions in NUnit? If so, how should I use it?

Background and details of what I mean by "soft statements":

In a set of acceptance tests, I want to check that the form is filled out correctly. I am currently doing this with the following flow step:

Then The new note form is filled out as follows
         | Label       | Value    |
         | First Name  | John     |
         | Last name   | Skeet    |
         | Title       | Cool Kat |


which is implemented something like this:

[Then(@"The new note form is filled out as follows")]
public void ThenTheNewNoteFormIsFilledOutAsFollows(Table table)
    foreach (var row in table.Rows)
        var name = row["Label"];
        var value = row["Value"];

        switch (name)
            case "First Name":
                // Page is part of our automation framework - Page.FirstName is 
                // simply an abstraction over getting the value of the 
                // <input name="FirstName" />
                Assert.That(Page.FirstName, Is.EqualTo(value), 
                            "Incorrect first name: Expected {0}, was {1}", 
                            value, Page.FirstName);
            case "Last Name":
                Assert.That(Page.LastName, Is.EqualTo(value), 
                            "Incorrect first name: Expected {0}, was {1}", 
                            value, Page.LastName);
            // similar cases for the other properties


This works because it gives us the correct test results (pass / fail) and error messages that show what went wrong. However, because test execution is aborted when it Assert

fails, only the first value of the failed form is reported; if, say, the form was empty, then only First Name

the example above would be missing and I would have to rerun the test to see which is Last Name

also missing.

Since these are browser automation tests, the battery life is quite long and it would be nice to get a test failure that reports all invalid form elements, not just the first one.

Is this possible with NUnit?

To readers who want to argue "Don't do this - the unit test should just test one thing":

Yes, I know - but your argument is not valid because it is not a unit test. This is an acceptance test that checks if the entire stack is working. We use NUnit to design and run these tests, but that doesn't make them unit tests.


source to share

All Articles