[mono-devel-list] System.Drawing.Pens test patch

Ben Maurer bmaurer at ximian.com
Sun Jul 31 11:20:35 EDT 2005


On Sun, 2005-07-31 at 05:56 -0700, Andrew Skiba wrote:
> Hi.
> 
> In many tests in System.Drawing there is a strange construct like:
> 
> try {
> 	Do.Something ();
> 	Fail ("This should throw ArgumentException");
> }
> catch (Exception e) {
> 	Assert ("MSG", e is ArgumentException);
> }
> 
> Problem with this approach is that catch will take care of all
> exceptions and then rethrow nunit assertion exception via Assert (...).
> The source and the call stack of original exception will be lost. So I
> changed it to:
> 
> catch (ArgumentException) {
> 	Assert ("MSG", true);
> }
> 
> In this way, if e is not argument exception, its contents will be seen
> in TestResults.xml, together with the whole call stack.
> 
> I'm not sure if there is any use of Assert ("MSG", true), may be it can
> be omitted?
> 
> While it would be best to fix all such places, I changed only those I
> needed right now, and the rest can be done later.

The cleanest way to do these is to factor each out into a different
method and use the ExpectedException attribute. First, this is much
cleaner. Second, both the old code and your suggestion don't do as much
checking as possible. ArgumentException is a base class for other
exceptions.

benm at alpha:~$ monop ArgumentOutOfRangeException
[Serializable]
public class ArgumentOutOfRangeException : ArgumentException, System.Runtime.Serialization.ISerializable {

With ExpectedException, nunit tests the exception thrown is of the
*exact* type specified in the attribute. However the "is blah" tests and
the catch (Blah) clauses catch any derived types. So, they are not as
good of tests.

-- Ben




More information about the Mono-devel-list mailing list