Closed kloczek closed 5 months ago
Hi there!
Yes, this is expected since jc
performs naive and aware timestamp tests so the correct timezone must be set prior to tests.
Prepend TZ="America/Los_Angeles"
to your test command and test should pass. Thanks!
OK so why this env variable is not set in pytest session? 🤔
Mostly because it’s documented to work this way and there isn’t a clean method to change the time zone in a safe way on Windows, so I leave it to the tester.
There's not so much an issue of getting the environment variable into the tests, rather the issue is how to cleanly set the timezone in the OS during the test. It's fairly trivial in Linux/Unix because the shell just uses the TZ
env variable to set the timezone for the session (and only that session). That's why TZ="America/Los_Angeles" python3 -m unittest -v
works.
Unfortunately, there is no analog in Windows, and if we change the timezone during the test it changes the timezone for the whole system, which could put the system in a weird state for the user while the test is running. That is why it's left up to the tester so it is explicit and there are no surprises.
For example, in Github Actions we use szenius/set-timezone to set the timezone for the OS (linux, macOS, or Windows) before running the tests.
Also, we use unittest
instead of pytest
, though it seems pytest
may be able to run unittest
s? I'm not sure as I've not used pytest
before.
That's why TZ="America/Los_Angeles" python3 -m unittest -v works.
What is the problem with add that en variable in pytest configuration? Documentation about that takes more characters than entry in pytest configuration. If you want to have more tickets like mine that is OK.
And unittest
.. correctly written unittest
based test suite can be handled correctly by pytest.
unittest
provides as well setting up env variables in test suite code/settings.
Nothing here needs to be documented. All what is needed is correct setup/test suite settings.
What is the problem with add that en variable in pytest configuration?
Adding the env variable within python does not change the timezone. There is some magic in the shell that does this based on the value of the TZ
environment variable. That's why it is best to set it before running the tests.
Yes, it is possible to set the timezone in python, but again, not in a consistent way across platforms. If jc
were only a linux cli tool then I would just do that in the tests. jc
is packaged on dozens of distros and platforms and this question does occasionally come up. Once I point them to the docs it doesn't seem to be a problem going forward.
Looks like something is wrong and pytest fails in units coparing time. I'm using tzdata 2024a.