-
Notifications
You must be signed in to change notification settings - Fork 757
Description
When using Assert.Multiple and assertion failures happen inside the lambda, a MultipleAssertException is thrown by the framework.
This exception has absolutely no details on the actual failures that happened, which drastically impacts the debugging experience: users have to wait for the run to finish and then inspect the Test Explorer window to see what errors have occurred.
This is where it is being thrown currently:
https://github.com/nunit/nunit/blob/efd115b26c8f5d4ebe6d52f5108097ceb0c77ffb/src/NUnitFramework/framework/Assert.cs#L340
if (context.MultipleAssertLevel == 0 && context.CurrentResult.PendingFailures > 0)
throw new MultipleAssertException();
This results in the following message:
"One or more failures in Multiple Assert block:"
This gives no hint to what happened and leaves the developer wondering how to see the actual failures. I think this is a really bad practice with exceptions in general: they should provide as much detail as humanly possible.
I'm aware that the display on Test Explorer has full details, but NUnit shouldn't assume that all developers run tests instead of debugging them.
I propose the following:
-
That failures be integrated as inner exceptions
This would be similar to how anAggregateExceptionworks: have all failures as child exceptions so that people debugging the application can see what errors occurred. Maybe using anAggregateExceptionwould be ideal, but I heard you had some issues with that because the type doesn't exist in all versions of the framework. Instead of just inspecting thePendingFailurescounter like above, we could convert anyAssertionResultintoAssertionExceptions and add them to the parentMultipleAssertExceptionbefore throwing it. I'd even suggest ditching theAssertionResultmodel itself in favor of directly handlingAssertionExceptionobjects, but that's probably a bigger discussion that I don't want to get into right now. -
Create an user-friedly
.ToStringimplementation onMultipleAssertException
In combination with # 1, the exception'sToStringshould provide a summary of failures. That would allow developers to see what errors were reported easily, and only have to inspect the inner exceptions in case further details are required. Maybe ommiting the stacktrace for each error on this summary would be a good idea, for example.
This lack of debugging support had a very significant impact when migrating to Assert.Multiple on our team: we used to have a custom method to perform this task which reported a much cleaner error in debug mode showing all assertions that failed, and when we switched to Assert.Multiple there was justified backlash.