Question

pytest passes locally, fails in GitHub Actions with a timezone difference

Solved · 9 views · asked by marco_py · edited

A test asserts a date string. Locally it's 2026-09-17, in CI it's 2026-09-16.

What I’ve tried

Set TZ=Europe/Amsterdam in the workflow. Fixed CI, but now the test lies about production, which runs in UTC.

Comment

1 answer

Marked as helpful by the asker
marco_py · edited

Your test encodes local time as truth. Make the code and the test both explicit:

from datetime import datetime, timezone
now = datetime.now(timezone.utc)

and in tests, freeze time with freezegun or pass now in as a parameter. A function that reads the wall clock is untestable; a function that receives the time is trivial.

Comment