Skip to content

MultipleAssertException doesn't provide proper details on failures #2695

@julealgon

Description

@julealgon

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:

  1. That failures be integrated as inner exceptions
    This would be similar to how an AggregateException works: have all failures as child exceptions so that people debugging the application can see what errors occurred. Maybe using an AggregateException would 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 the PendingFailures counter like above, we could convert any AssertionResult into AssertionExceptions and add them to the parent MultipleAssertException before throwing it. I'd even suggest ditching the AssertionResult model itself in favor of directly handling AssertionException objects, but that's probably a bigger discussion that I don't want to get into right now.

  2. Create an user-friedly .ToString implementation on MultipleAssertException
    In combination with # 1, the exception's ToString should 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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions