问题
My function is
def validate_latitude(lat):
"""Enforce latitude is in range
>>> validate_latitude(65)
65
>>> validate_latitude(91)
90
>>> validate_latitude(-91)
-90
"""
lat = min(lat, 90)
lat = max(lat, -90)
return lat
And the test fails with this output
**********************************************************************
File "packages/utils.py", line 64, in __main__.validate_latitude
Failed example:
validate_latitude(-91)
Expected:
-90
Got:
-90
**********************************************************************
Cant see why it fails in spite of having the deisred output
回答1:
In these two lines:
>>> validate_latitude(-91)
-90
You have a Tab character before the -
in -90
, and four space characters after the 0
. When doctests runs this code the extra whitespace is of course not produced, so the equality comparison fails.
Good editors, e.g. vim, have ways to highlight trailing spaces, and stray tabs, so that you don't fall afould of such accidents. Not sure what editor you're using or how you have set it up, so it's hard to give more specific advice (besides the obvious one of ensuring you use an editor WITH such capabilities, and enable the capabilities in question;-).
回答2:
Whitespace?
If I highlight your output, I can see additional whitespace following the "Expected" value. Not sure whether this is relevant or not.
来源:https://stackoverflow.com/questions/1650184/doctest-failing-inspite-of-having-correct-output