If you are moving from NUnit to xUnit, you will likely encounter the absence of the DoesNotThrow method in xUnit. At first I believed this did not exist but it was pointed out to me that it exists in another form. There seems to be two ways to go about this. One is to implicitly check for DoesNotThrow
by letting the code not throw an exception and the test therefore passes, if the code throws an exception the test will fail, like it will with any uncaught exception. I like the way that Brad Wilson describes it on github: Think of it this way: every line of code you write outside of a try block has an invisible Assert.DoesNotThrow around it.
However as Christian Diac pointed out in the comments down below there is a way to do this more explicitly, by using the Record class of xUnit:
//Act
var exception = Record.Exception(() => callYourMethod());
//Assert
Assert.Null(exception);
In the above we try to record if an exception is thrown. If no exception is thrown the Record.Exception
method returns null, this indicates that no exception was thrown. While I see this as less explicit than the nUnit "DoesNotThrow", I still think it is much better than not showing what the intention of the test is (no check for an exception). Tests can be hard to understand and the more explicit the intentions are, the easier it is to reason with the test.
That is all
I hope you enjoyed this post, let me know in the comments what you think :)