# # An example that shows the use of unit # testing # # The programmer initially wrote these functions, # that correctly converts degrees celcius to fahrenheit and back. def f_to_c(temp): """ Convert fahrenheit to celcius """ return (temp - 32.0)*5.0/9.0 def c_to_f(temp): """ Convert celcius to fahrenheit """ return temp*9.0/5.0 + 32.0 # Some test functions are written in a separate file, # say "test_temperatures.py" # Now two years later, the programmer decides to # clean things up a litle, and modifies the functions: def f_to_c(temp): """ Convert fahrenheit to celcius """ return (temp - 32)*5/9 def c_to_f(temp): """ Convert celcius to fahrenheit """ return temp*9/5 + 32 # These functions are buggy! If temp # is an integer, a situation that is quite realistic, # division may give bad results ... # Luckily the tests can capture this, if they are well written # http://articles.latimes.com/1999/oct/01/news/mn-17288