regebro / tzlocal

A Python module that tries to figure out what your local timezone is
MIT License
185 stars 58 forks source link

pytest fails on "Can not find any timezone configuration, defaulting to UTC." #89

Closed dvzrv closed 3 years ago

dvzrv commented 4 years ago

Hi! I'm packaging python-tzlocal for Arch Linux. When building version 2.1 I ran pytest against the build (pytest -v tests/tests.py), but it fails on one test:

============================= test session starts ==============================
platform linux -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python
cachedir: .pytest_cache
rootdir: /build/python-tzlocal/src/python-tzlocal-2.1
collecting ... collected 12 items

tests/tests.py::TzLocalTests::test_assert_tz_offset PASSED               [  8%]
tests/tests.py::TzLocalTests::test_env PASSED                            [ 16%]
tests/tests.py::TzLocalTests::test_fail FAILED                           [ 25%]
tests/tests.py::TzLocalTests::test_get_reload PASSED                     [ 33%]
tests/tests.py::TzLocalTests::test_only_localtime PASSED                 [ 41%]
tests/tests.py::TzLocalTests::test_symlink_localtime PASSED              [ 50%]
tests/tests.py::TzLocalTests::test_timezone PASSED                       [ 58%]
tests/tests.py::TzLocalTests::test_timezone_setting PASSED               [ 66%]
tests/tests.py::TzLocalTests::test_timezone_top_line_comment PASSED      [ 75%]
tests/tests.py::TzLocalTests::test_vardbzoneinfo_setting PASSED          [ 83%]
tests/tests.py::TzLocalTests::test_zone_setting PASSED                   [ 91%]
tests/tests.py::TzWin32Tests::test_win32_on_unix PASSED                  [100%]

=================================== FAILURES ===================================
____________________________ TzLocalTests.test_fail ____________________________

self = <tests.tests.TzLocalTests testMethod=test_fail>

    def test_fail(self):
        out = StringIO()
        stderr = sys.stderr
        try:
            sys.stderr = out
            tz = tzlocal.unix._get_localzone(
                _root=os.path.join(self.path, 'test_data'))
        finally:
            sys.stderr = stderr
        self.assertEqual(tz, pytz.utc)
>       self.assertIn('Can not find any timezone configuration',
            out.getvalue())
E       AssertionError: 'Can not find any timezone configuration' not found in ''

tests/tests.py:116: AssertionError
=============================== warnings summary ===============================
tests/tests.py::TzLocalTests::test_fail
  /build/python-tzlocal/src/python-tzlocal-2.1/tzlocal/unix.py:158: UserWarning: Can not find any timezone configuration, defaulting to UTC.
    warnings.warn('Can not find any timezone configuration, defaulting to UTC.')

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================== short test summary info ============================
FAILED tests/tests.py::TzLocalTests::test_fail - AssertionError: 'Can not fin...
=================== 1 failed, 11 passed, 1 warning in 0.16s ====================

I have tried exporting TZ for a valid timezone before the pytest call, but it doesn't get picked up. I'm a little clueless as to why this doesn't work tbh. The python setup.py test call works, but will soon be obsoleted, which is why I'd rather run pytest.

Any insights would be highly appreciated!

regebro commented 4 years ago

So then pytest, as usual, does something incomprehensible, which is the reason I don't use it. Thanks for the heads up, I'll look into this next time I get time.

tapakund commented 4 years ago

I am facing the same issue while running pytest on tzlocal.

test_assert_tz_offset (tests.tests.TzLocalTests) ... /usr/src/photon/BUILD/tzlocal-2.1/tzlocal/unix.py:158: UserWarning: Can not find any timezone configuration, defaulting to UTC.
  warnings.warn('Can not find any timezone configuration, defaulting to UTC.')
ok
test_env (tests.tests.TzLocalTests) ... ok
test_fail (tests.tests.TzLocalTests) ... FAIL
test_get_reload (tests.tests.TzLocalTests) ... ok
test_only_localtime (tests.tests.TzLocalTests) ... ok
test_symlink_localtime (tests.tests.TzLocalTests) ... ok
test_timezone (tests.tests.TzLocalTests) ... ok
test_timezone_setting (tests.tests.TzLocalTests) ... ok
test_timezone_top_line_comment (tests.tests.TzLocalTests) ... ok
test_vardbzoneinfo_setting (tests.tests.TzLocalTests) ... ok
test_zone_setting (tests.tests.TzLocalTests) ... ok
test_win32_on_unix (tests.tests.TzWin32Tests) ... ok

======================================================================
FAIL: test_fail (tests.tests.TzLocalTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/src/photon/BUILD/tzlocal-2.1/tests/tests.py", line 116, in test_fail
    self.assertIn('Can not find any timezone configuration',
AssertionError: 'Can not find any timezone configuration' not found in ''

----------------------------------------------------------------------
Ran 12 tests in 0.026s

FAILED (failures=1)
Test failed: <unittest.runner.TextTestResult run=12 errors=0 failures=1>
error: Test failed: <unittest.runner.TextTestResult run=12 errors=0 failures=1>
pganssle commented 4 years ago

This issue has to do with pytest's warnings capture. You can use -p no:warnings and the test will start working again.

If tzlocal adopts pytest, there's a much easier way to write this test using [pytest.warns](https://docs.pytest.org/en/3.0.2/recwarn.html). I've also written a custom assertWarns context manager that, IIRC, was compatible with pytest as well. (Though obviously if this project doesn't support the use of pytest as a test runner, then people should use python -m unittest discover or whatever the proper invocation is).

agronholm commented 4 years ago

I've done this in https://github.com/regebro/tzlocal/pull/94.