Extending the Assert class in xunit
I have a method that validates user input and throws an exception if something went wrong. And thought I would extend the Assert class in xunit with a method DoesNotThrow to unit test it. I'm having some problem extending that class and tho that I could ask for help.
6 Replies
you can use some lib like FluentAssertion for that https://fluentassertions.com/exceptions/
Fluent Assertions
Exceptions
Stack Overflow
Extending XUnit Assert class with new asserts
I'm trying to extend the xUnit assert method by adding some selenium functionality
namespace MyProject.Web.Specs.PageLibrary.Extensions
{
public static class AssertExtensions
{
pub...
After some more exploring, I was unable to create an extension on the Assert class. And it just boils down to that it is a class that just have static methods in it. Looked at the source for xunit and ended up writing a wrapper class instead in the same style. Maybe you guys already know this, but if I mark a method with [Fact] and then throw an exception it counts as a bad test case, and if nothings happens it counts as a good test case. Thought I would share that.
This is what I ended up with:
namespace Tests.Extensions;
public class Assert
{
public static void DoesNotThrow(Action action)
{
try
{
action();
}
catch (Exception e)
{
throw new Exception($"Expected no exception, but got: {e.Message}");
}
}
}
Did you read my link?
There's a base class you can derive your test class from, which has an Assert property, that you can add extension methods to
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered