KobSen
KobSen

Reputation: 1

NUnit and ability to ignore failed test without exception

I'm working on a project where I have to use something like soft assertion in NUnit in C#. I need to implement a feature which says that if a test failed, go to the next test without showing exception but note that this test was failed. I know that it's inadvisable to use multiple asserts but it's necessary because I got a form where f.e field with surname can fail but next tests are independent and should still run.

public class SoftAssertionTest
{
    public static void AreEqual(object expected, object actual)
    {
        try
        {
            Assert.AreEqual(expected, actual);
        }
        catch (Exception e)
        {
            //Catch exception but remember that test failed and got to the 
            //next
        }
    }
}

Expected result is that all tests run without exceptions but finally result show Fail status.

[TestFixture]
public class TestClass
{
    [Test]
    public static void Test()
    {
        SoftAssertionTest.AreEqual(1, 2);
        SoftAssertionTest.AreEqual(3, 4);
    }
}

Any ideas?

Upvotes: 0

Views: 2990

Answers (2)

Vadim
Vadim

Reputation: 1

Try SoftAssertion

      SoftAssert softAssert = new SoftAssert();

      softAssert.True(false);
      softAssert.False(true);
      softAssert.NotNull(null);

      softAssert.VerifyAll();

Upvotes: 0

Rob Prouse
Rob Prouse

Reputation: 22647

NUnit 3 has Assert.Warn if you just want to warn on failures and Assert.Multiple if you want to run multiple asserts and fail the test if any of the individual asserts fail, but ensure that all of the asserts are run.

Upvotes: 1

Related Questions