Feature: Assert.DoesNotThrow() needs exception details

Bug #957899 reported by Chris Capon
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
NUnit V2
Fix Released
High
Charlie Poole

Bug Description

NUnit 2.5.10 11092 (Windows)

When Assert.DoesNotThrow() actually does throw an Exception, the Errors and Failures window of the GUI does not show any details about the exception. Rather, it just shows the provided text parameter and:

Unexpected exception: NUnit.Framework.AssertionException

It would be handy if the Assert would also show details about the Exception being thrown. Otherwise, test code has to be changed to find out what went wrong.

Thanks.

Related branches

Revision history for this message
Jv (jv-ravichandran) wrote : Re: [Bug 957899] [NEW] Feature: Assert.DoesNotThrow() needs exception details

Hi,

I m not sure if my response to your query is correct but here is a try:

When ExpectedException is not thrown then the report says Expected
exception: some exception but was some other exception. Will this serve
your purpose because i m not sure if E & F should be expected to show
exception details....just my few pennies!

Jv

On Sat, Mar 17, 2012 at 6:45 PM, Chris Capon <email address hidden> wrote:

> Public bug reported:
>
> NUnit 2.5.10 11092 (Windows)
>
> When Assert.DoesNotThrow() actually does throw an Exception, the Errors
> and Failures window of the GUI does not show any details about the
> exception. Rather, it just shows the provided text parameter and:
>
> Unexpected exception: NUnit.Framework.AssertionException
>
> It would be handy if the Assert would also show details about the
> Exception being thrown. Otherwise, test code has to be changed to find
> out what went wrong.
>
> Thanks.
>
> ** Affects: nunitv2
> Importance: Undecided
> Status: New
>
> --
> You received this bug notification because you are a member of NUnit
> Developers, which is subscribed to NUnit V2.
> https://bugs.launchpad.net/bugs/957899
>
> Title:
> Feature: Assert.DoesNotThrow() needs exception details
>
> Status in NUnit V2 Test Framework:
> New
>
> Bug description:
> NUnit 2.5.10 11092 (Windows)
>
> When Assert.DoesNotThrow() actually does throw an Exception, the
> Errors and Failures window of the GUI does not show any details about
> the exception. Rather, it just shows the provided text parameter and:
>
> Unexpected exception: NUnit.Framework.AssertionException
>
> It would be handy if the Assert would also show details about the
> Exception being thrown. Otherwise, test code has to be changed to
> find out what went wrong.
>
> Thanks.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/nunitv2/+bug/957899/+subscriptions
>

--
Regards,

Ravichandran Jv
http://ravichandranjv.blogspot.com

Revision history for this message
Chris Capon (ttabyss) wrote :

I think ExpectedException is used for Assert.Throws(), not Assert.DoesNotThrow() isn't it?

Knowing the type of exception alone, though, often isn't enough to diagnose an unexpected failure. You need details. Without them you have to modify the test code to catch the Exception - which, in turn, opens it up to failure.

Currently, DoesNotThrow() prints "Unexpected Exception: {0}", ex.GetType(). Instead, it could just convert the exception to a string and provide complete details.

Throws() would probably also benefit from this.

Revision history for this message
Charlie Poole (charlie.poole) wrote :

This is out of date. The 2.6 release shows information about any unexpected exception.

Regarding how various subfeatures depend upon one another...

The Asserts all depend on the constraints (e.g. Assert.Throws on ConstraintExpression.Throws)

The "Not" versions depend on the ones without Not's

ExpectedExceptionAttribute is entirely separate, since it only has effect after the test method ends.

Charlie

Changed in nunitv2:
status: New → Invalid
Revision history for this message
Chris Capon (ttabyss) wrote :

No offence, but it is NOT changed in 2.6. I just downloaded and installed 2.6.0.12051 and the source for DoesNotThrow() is the same. It shows the Exception type but no details.

Changed in nunitv2:
status: Invalid → New
Revision history for this message
Chris Capon (ttabyss) wrote :

Here is an example:

   FileNotFoundException ex = new FileNotFoundException( "invalid path", "/path/to/nowhere" );
   Console.WriteLine( ex );
   Assert.DoesNotThrow( delegate { throw ex; }, "file test" );

The Console.Write displays:

   System.IO.FileNotFoundException: invalid path
   File name: '/path/to/nowhere'

The Assert.DoesNotThrow() displays:

   file test
   Unexpected exception: System.IO.FileNotFoundException

I think the details of the exception are important for debugging an application.

Revision history for this message
Charlie Poole (charlie.poole) wrote :

Hmm... it could be that the change was made for Throws and not propogated to DoesNotThrow.

Revision history for this message
Charlie Poole (charlie.poole) wrote :

It turns out that the fix in 2.6 is not even as extensive as the release notes imply:

"When Assert.Throws() fails due to an exception of the wrong Type being thrown, the error message now includes the Message and Stack Trace from the exception. "

Actually, this only applies to the Assert.Throws(Type, ...) overloads and should be made clear in the documentation.

The release notes are correct in excluding Assert.DoesNotThrow as well as the constraint syntax:
   Assert.That(<code>, Throws.Exception....);
   Assert.That(<code>, Throws.Nothing);
   ...

I'll try to add this feature wherever the wrong type of exception is detected. It doesn't seem to make sense when testing for a specific value of an exception property, since the message already gives the needed info.

Changed in nunitv2:
status: New → Triaged
importance: Undecided → High
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 2.6.1
Changed in nunitv2:
status: Triaged → Fix Committed
Changed in nunitv2:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.