How to test that an exception is raised ?

Antoon Pardon apardon at forel.vub.ac.be
Fri Jan 28 10:14:12 EST 2005


Op 2005-01-28, StepH schreef <stephane.bronsart at bea.be>:
> Thanks for you answer.
> I'm new to Python (coming from C/C++).
>
> Do you say that it's not possible to test (using unittest) if an exception
> is well raised if the tested code catch it ?
> How to solve this paradoxe ?  How to automaticaly test such code ?

IMO you want something unittest are not designed for.

Unittest are supposed to test for particular results, not for particular
behaviour within. If the expected behaviour is that calling code doesn't
see an exception raised, then the test passed if no exception was seen.

You equally can't test which branch of an if statement was taken or
which parameter was given to a helper function in order to get to
the desired result.

That you internally raise an exception and catches it, is an
implementation detail, that normally is of no concern to
the tester.

-- 
Antoon Pardon



More information about the Python-list mailing list