How to test that an exception is raised ?

StepH stephane.bronsart at teledisnet.be
Fri Jan 28 15:54:41 EST 2005


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 ?  My goal is to test if an 
exception is well raised when a bad filename is passed to the mps2xml 
function.

> 
> 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.

> 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...

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

I'll conclude this thred here...  In fact, I think I've to go-back to 
theory (at least about exception)...

Thanks at all fo trying to help me.  I'm new to Python, and i've not 
already embrass the "python way" completly.

See you later.

StepH.



More information about the Python-list mailing list