Home > Software design >  How to convert an unexpected exception into an assertion failure in NUnit test/Selenium/C#?
How to convert an unexpected exception into an assertion failure in NUnit test/Selenium/C#?

Time:01-03

I am a newbie and couldn't make out in the NUnit documentation. I think this link contains useful information: https://docs.nunit.org/articles/nunit/writing-tests/constraints/ThrowsConstraint.html

How to convert unexpected exception whether "element is not clickable", "click is intersepted" into an assertion failure.

Without a convert, the [Retry] NUnit attribute does not restart failed tests on an unexpected exception.

CodePudding user response:

If I understood your problem correctly, you have a unit test method and within your method, you wish to handle the exception that has occurred. In such a case, you do not need to use the ThrowsContraint as you mentioned; Just call Assert.Fail() at that point. You can find the documentation here.

You should rather use ThrowsConstraint when you want to ensure that your given method throws an exception (in which case, the test is deemed as Passed).

CodePudding user response:

In this case, you want the ThrowsNothingConstraint

Assert.That(() => SomeMethod(), Throws.Nothing);
  • Related