Back to articles
My Python tests passed. Production still broke.

My Python tests passed. Production still broke.

via Dev.to PythonNico Reyes

Deployed yesterday. Tests green. Production crashed 3 hours later. The bug? A datetime object I created in tests wasn't aware of timezones. Production API expects UTC timestamps. My test just used datetime.now() which returns naive datetime. Code looked fine, tests passed, production exploded. # What I was doing (broken) from datetime import datetime def get_report_timestamp (): return datetime . now () # naive datetime, no timezone # Tests passed because I compared it to another naive datetime assert get_report_timestamp () < datetime . now () Production error logs just said "invalid timestamp format". Took me 2 hours to figure out the datetime was missing timezone info. The API rejected it, my code couldn't parse the error properly, whole system stalled. # What actually works from datetime import datetime , timezone def get_report_timestamp (): return datetime . now ( timezone . utc ) # aware datetime with UTC # Better test that would've caught this assert get_report_timestamp (). tz

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
3 views

Related Articles