Python Pre-testing for exceptions when coverage fails
- by Tal Weiss
I recently came across a simple but nasty bug.
I had a list and I wanted to find the smallest member in it. I used Python's built-in min().
Everything worked great until in some strange scenario the list was empty (due to strange user input I could not have anticipated). My application crashed with a ValueError (BTW - not documented in the official docs).
I have very extensive unit tests and I regularly check coverage to avoid surprises like this. I also use Pylint (everything is integrated in PyDev) and I never ignore warnings, yet I failed to catch this bug before my users did.
Is there anything I can change in my methodology to avoid these kind of runtime errors?
(which would have been caught at compile time in Java / C#?).
I'm looking for something more than wrapping my code with a big try-except. What else can I do? How many other build in Python functions are hiding nasty surprises like this???