[TIP] Testing for wrong type errors

Michael Schlenker msc at contact.de
Mon Nov 12 08:44:10 PST 2007

Nate Lowrie schrieb:
> I am just curious.  Since Python doesn't have explicit type
> declarations, do y'all test for type errors.  For example, if you're
> writing a test for a function that has argument A that needs to be an
> int, do you have a test to see if it throws an error if it is another
> type like a string or do you just assume that other functions and
> methods will play nice?
Depends a bit on how critical it is. If the function under test has potential
to mask errors in a bigger system i add some basic type checks
to ensure it really blows up early. Especially functions that silently return
None or convert str() to unicode() can be ugly.

The more exposed to direct user input the code is, the more rigid the checks
usually get. If the function faces the 'outside world' e.g. websites, the checks
get draconic to minimize security issues (like revealing tracebacks thrown into
the face of a user, code injection or other nasty stuff).

Performance issues weaken this a little bit.
Remember: The greatest performance improvement of all is when a system goes from not-working to working


Michael Schlenker
Software Engineer

CONTACT Software GmbH           Tel.:   +49 (421) 20153-80
Wiener Straße 1-3               Fax:    +49 (421) 20153-41
28359 Bremen
http://www.contact.de/          E-Mail: msc at contact.de

Sitz der Gesellschaft: Bremen | Geschäftsführer: Karl Heinz Zachries
Eingetragen im Handelsregister des Amtsgerichts Bremen unter HRB 13215

More information about the testing-in-python mailing list