How to test that an exception is raised ?
apardon at forel.vub.ac.be
Mon Jan 31 03:08:29 EST 2005
Op 2005-01-28, StepH schreef <stephane.bronsart at teledisnet.be>:
> Antoon Pardon a écrit :
>> 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.
> So why the assertRaises function in unittest?
To see if an exception is propagated to the caller.
> My goal is to test if an
> exception is well raised when a bad filename is passed to the mps2xml
>> 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.
> No (unless i don't well understand you), the expected behavior is to
> launch an exception if a bad filename is passed. If no exception is
> raised, this is not normal.
What do you mean with launch an exception? Should the exception
propagate to the caller or not? If it should your code was wrong
to catch it.
>> 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.
> I'm agree with out on this point, but not for the exception part...
Why not? Exceptions are nothing special. Either you want to propagated
them to the caller, in which case they can be seen as somekind of result
and this can be tested for with a unittest or you don't want them to
be propagted and then they are just an implementation detail of your
More information about the Python-list