pytest-tzcheck 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ """Run tests in several timezones and report the ones whose outcome changes."""
@@ -0,0 +1,164 @@
1
+ """pytest plugin: run each test in several timezones, report tests whose outcome changes."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections import defaultdict
6
+
7
+ import pytest
8
+
9
+ from pytest_tzcheck import zones
10
+
11
+ FIXTURE = "tzcheck_zone"
12
+ PROP_ZONE = "tzcheck_zone"
13
+ PROP_BASE = "tzcheck_base_nodeid"
14
+
15
+ # Worst outcome wins when a test has several phases (setup, call, teardown).
16
+ _RANK = {"passed": 0, "xfailed": 1, "xpassed": 1, "skipped": 2, "failed": 3, "error": 4}
17
+
18
+
19
+ def pytest_addoption(parser: pytest.Parser) -> None:
20
+ group = parser.getgroup("tzcheck", "timezone dependence")
21
+ group.addoption(
22
+ "--tzcheck",
23
+ action="store_true",
24
+ default=None,
25
+ help="run each test in several timezones and report tests whose outcome depends on it.",
26
+ )
27
+ group.addoption(
28
+ "--tzcheck-zones",
29
+ metavar="ZONES",
30
+ help=f"comma-separated IANA zones or presets ({', '.join(zones.PRESETS)}). "
31
+ f"'{zones.LOCAL}' is the current zone. Default: ci ({','.join(zones.PRESETS['ci'])}).",
32
+ )
33
+ parser.addini("tzcheck", "enable --tzcheck by default", type="bool", default=False)
34
+ parser.addini("tzcheck_zones", "zones for --tzcheck (see --tzcheck-zones)", default="ci")
35
+
36
+
37
+ def pytest_configure(config: pytest.Config) -> None:
38
+ config.addinivalue_line(
39
+ "markers",
40
+ "no_tzcheck: run this test only in the local timezone, even with --tzcheck.",
41
+ )
42
+ enabled = config.getoption("tzcheck")
43
+ if enabled is None:
44
+ enabled = config.getini("tzcheck")
45
+ if not enabled:
46
+ return
47
+ if not zones.supported():
48
+ raise pytest.UsageError("--tzcheck needs time.tzset(), which this platform does not have.")
49
+ names = zones.parse(config.getoption("tzcheck_zones") or config.getini("tzcheck_zones"))
50
+ for name in names:
51
+ try:
52
+ zones.validate(name)
53
+ except ValueError as exc:
54
+ raise pytest.UsageError(f"--tzcheck-zones: {exc}") from None
55
+ config.pluginmanager.register(TzCheck(zones.dedupe(names)), "tzcheck-session")
56
+
57
+
58
+ @pytest.fixture
59
+ def tzcheck_zone(request: pytest.FixtureRequest) -> str:
60
+ """The timezone this test runs in: an IANA name, or "local" for the untouched environment."""
61
+ return getattr(request, "param", zones.LOCAL)
62
+
63
+
64
+ def _zone_of(item: pytest.Item) -> str | None:
65
+ callspec = getattr(item, "callspec", None)
66
+ return callspec.params.get(FIXTURE) if callspec else None
67
+
68
+
69
+ def _base_nodeid(nodeid: str, zone: str) -> str:
70
+ suffix = f"tz={zone}]"
71
+ if not nodeid.endswith(suffix):
72
+ return nodeid
73
+ head = nodeid[: -len(suffix)]
74
+ return head[:-1] if head.endswith("[") else head[:-1] + "]"
75
+
76
+
77
+ def _outcome(report: pytest.TestReport) -> str | None:
78
+ if hasattr(report, "wasxfail"):
79
+ return "xfailed" if report.skipped else "xpassed"
80
+ if report.failed:
81
+ return "failed" if report.when == "call" else "error"
82
+ if report.skipped:
83
+ return "skipped"
84
+ return "passed" if report.when == "call" else None
85
+
86
+
87
+ class TzCheck:
88
+ def __init__(self, names: list[str]) -> None:
89
+ self.names = names
90
+ self.local = zones.local_name()
91
+ self.results: dict[str, dict[str, str]] = defaultdict(dict)
92
+
93
+ def label(self, zone: str) -> str:
94
+ return f"{zone} ({self.local})" if zone == zones.LOCAL else zone
95
+
96
+ def pytest_report_header(self) -> str:
97
+ if len(self.names) < 2:
98
+ return (
99
+ f"tzcheck: only one distinct zone ({self.label(self.names[0])}), nothing to compare"
100
+ )
101
+ return "tzcheck: " + ", ".join(self.label(z) for z in self.names)
102
+
103
+ @pytest.hookimpl(trylast=True)
104
+ def pytest_generate_tests(self, metafunc: pytest.Metafunc) -> None:
105
+ if len(self.names) < 2 or metafunc.definition.get_closest_marker("no_tzcheck"):
106
+ return
107
+ if FIXTURE not in metafunc.fixturenames:
108
+ metafunc.fixturenames.append(FIXTURE)
109
+ metafunc.parametrize(
110
+ FIXTURE, self.names, indirect=True, ids=[f"tz={z}" for z in self.names]
111
+ )
112
+
113
+ @pytest.hookimpl(hookwrapper=True)
114
+ def pytest_runtest_protocol(self, item: pytest.Item, nextitem: pytest.Item | None):
115
+ zone = _zone_of(item)
116
+ if zone is None:
117
+ yield
118
+ return
119
+ item.user_properties.append((PROP_ZONE, zone))
120
+ item.user_properties.append((PROP_BASE, _base_nodeid(item.nodeid, zone)))
121
+ with zones.set_local_zone(zone):
122
+ yield
123
+
124
+ def pytest_runtest_logreport(self, report: pytest.TestReport) -> None:
125
+ props = dict(report.user_properties)
126
+ zone, base = props.get(PROP_ZONE), props.get(PROP_BASE)
127
+ outcome = _outcome(report)
128
+ if zone is None or base is None or outcome is None:
129
+ return
130
+ current = self.results[base].get(zone)
131
+ if current is None or _RANK[outcome] > _RANK[current]:
132
+ self.results[base][zone] = outcome
133
+
134
+ def dependent(self) -> dict[str, dict[str, str]]:
135
+ return {
136
+ base: by_zone
137
+ for base, by_zone in self.results.items()
138
+ if len(set(by_zone.values())) > 1
139
+ }
140
+
141
+ def pytest_terminal_summary(self, terminalreporter) -> None:
142
+ if not self.results:
143
+ return
144
+ dependent = self.dependent()
145
+ if not dependent:
146
+ terminalreporter.write_line(
147
+ f"tzcheck: no timezone-dependent tests "
148
+ f"({len(self.results)} tests x {len(self.names)} zones)"
149
+ )
150
+ return
151
+ terminalreporter.section("timezone-dependent tests", red=True, bold=True)
152
+ for base in sorted(dependent):
153
+ by_outcome: dict[str, list[str]] = defaultdict(list)
154
+ for zone in self.names:
155
+ if zone in dependent[base]:
156
+ by_outcome[dependent[base][zone]].append(self.label(zone))
157
+ parts = " ".join(
158
+ f"{outcome}: {', '.join(labels)}" for outcome, labels in by_outcome.items()
159
+ )
160
+ terminalreporter.write_line(f"{base} {parts}")
161
+ terminalreporter.write_line(
162
+ f"{len(dependent)} of {len(self.results)} tests changed outcome with the timezone",
163
+ red=True,
164
+ )
@@ -0,0 +1,118 @@
1
+ """Timezone presets, validation, and switching the process's local timezone."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import contextlib
6
+ import os
7
+ import sys
8
+ import time
9
+ from collections.abc import Iterator
10
+ from datetime import datetime, timedelta, timezone
11
+ from zoneinfo import TZPATH, ZoneInfo, ZoneInfoNotFoundError
12
+
13
+ LOCAL = "local"
14
+
15
+ PRESETS: dict[str, list[str]] = {
16
+ "ci": [LOCAL, "UTC"],
17
+ "wide": [
18
+ LOCAL,
19
+ "UTC",
20
+ "Pacific/Kiritimati", # UTC+14: the local date differs from UTC's most of the day
21
+ "America/St_Johns", # half-hour offset, northern DST
22
+ "Australia/Lord_Howe", # half-hour DST shift, southern hemisphere
23
+ ],
24
+ }
25
+
26
+
27
+ def supported() -> bool:
28
+ return hasattr(time, "tzset")
29
+
30
+
31
+ def parse(spec: str) -> list[str]:
32
+ """Expand a comma-separated list of zone names and presets, keeping order."""
33
+ names: list[str] = []
34
+ for part in spec.split(","):
35
+ part = part.strip()
36
+ for name in PRESETS.get(part, [part] if part else []):
37
+ if name not in names:
38
+ names.append(name)
39
+ return names
40
+
41
+
42
+ def validate(name: str) -> None:
43
+ """Raise ValueError unless the C library can load `name` from the system tz database.
44
+
45
+ libc silently falls back to UTC for a TZ it cannot find, so a typo would
46
+ look like a real run in UTC.
47
+ """
48
+ if name == LOCAL:
49
+ return
50
+ try:
51
+ ZoneInfo(name)
52
+ except (ZoneInfoNotFoundError, ValueError) as exc:
53
+ raise ValueError(f"unknown timezone {name!r}") from exc
54
+ if not any(os.path.isfile(os.path.join(path, name)) for path in TZPATH):
55
+ raise ValueError(
56
+ f"timezone {name!r} is not in the system tz database ({', '.join(TZPATH)})"
57
+ )
58
+
59
+
60
+ def _apply() -> None:
61
+ time.tzset()
62
+ tzlocal = sys.modules.get("tzlocal")
63
+ reload_localzone = getattr(tzlocal, "reload_localzone", None)
64
+ if reload_localzone is not None:
65
+ with contextlib.suppress(Exception):
66
+ reload_localzone()
67
+
68
+
69
+ @contextlib.contextmanager
70
+ def set_local_zone(name: str) -> Iterator[None]:
71
+ """Make `name` the process's local timezone (and its subprocesses') for the block."""
72
+ if name == LOCAL:
73
+ yield
74
+ return
75
+ previous = os.environ.get("TZ")
76
+ os.environ["TZ"] = name
77
+ _apply()
78
+ try:
79
+ yield
80
+ finally:
81
+ if previous is None:
82
+ os.environ.pop("TZ", None)
83
+ else:
84
+ os.environ["TZ"] = previous
85
+ _apply()
86
+
87
+
88
+ def signature(name: str) -> tuple[int, ...]:
89
+ """UTC offset at noon UTC on every day of the current year."""
90
+ with set_local_zone(name):
91
+ start = datetime(datetime.now(timezone.utc).year, 1, 1, 12, tzinfo=timezone.utc)
92
+ return tuple(
93
+ time.localtime((start + timedelta(days=d)).timestamp()).tm_gmtoff for d in range(366)
94
+ )
95
+
96
+
97
+ def dedupe(names: list[str]) -> list[str]:
98
+ """Drop zones whose offsets match an earlier zone's, e.g. 'UTC' when local is UTC."""
99
+ seen: set[tuple[int, ...]] = set()
100
+ kept = []
101
+ for name in names:
102
+ sig = signature(name)
103
+ if sig not in seen:
104
+ seen.add(sig)
105
+ kept.append(name)
106
+ return kept
107
+
108
+
109
+ def local_name() -> str:
110
+ """Best-effort IANA name of the current local zone, for display."""
111
+ tz = os.environ.get("TZ")
112
+ if tz:
113
+ return tz.lstrip(":")
114
+ with contextlib.suppress(OSError):
115
+ target = os.path.realpath("/etc/localtime")
116
+ if "zoneinfo/" in target:
117
+ return target.split("zoneinfo/", 1)[1]
118
+ return time.tzname[0]
@@ -0,0 +1,148 @@
1
+ Metadata-Version: 2.5
2
+ Name: pytest-tzcheck
3
+ Version: 0.1.0
4
+ Summary: Run your tests in the timezone CI uses, and find the ones that only pass in yours.
5
+ Project-URL: Repository, https://github.com/taltal-beep/pytest-tzcheck
6
+ Author-email: Ariel Even <ttlgptinc@gmail.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Keywords: flaky,pytest,testing,timezone,tz
10
+ Classifier: Framework :: Pytest
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: MacOS
14
+ Classifier: Operating System :: POSIX
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Software Development :: Testing
17
+ Requires-Python: >=3.9
18
+ Requires-Dist: pytest>=7
19
+ Description-Content-Type: text/markdown
20
+
21
+ # pytest-tzcheck
22
+
23
+ Run your tests in the timezone CI uses, and find the ones that only pass in yours.
24
+
25
+ "Passes on my machine, fails in CI" often comes down to the local timezone: your laptop is in Asia/Jerusalem, the CI runner is in UTC. `pytest --tzcheck` runs every test in both zones and tells you which ones change outcome, before you push.
26
+
27
+ ```
28
+ $ pytest --tzcheck tests/server/schemas/test_schedules.py
29
+ tzcheck: local (Asia/Jerusalem), UTC
30
+ ...
31
+ =========================== timezone-dependent tests ===========================
32
+ tests/server/schemas/test_schedules.py::TestRRuleSchedule::test_rrule_schedule_preserves_and_localizes_rdates passed: local (Asia/Jerusalem) failed: UTC
33
+ ```
34
+
35
+ ## Install
36
+
37
+ ```bash
38
+ pip install pytest-tzcheck
39
+ ```
40
+
41
+ Python 3.9+, pytest 7+, Linux or macOS (it needs `time.tzset()`, which Windows does not have).
42
+
43
+ ## Usage
44
+
45
+ ```bash
46
+ pytest --tzcheck # local zone + UTC (the "ci" preset)
47
+ pytest --tzcheck --tzcheck-zones=wide # local, UTC, +14h, half-hour offsets, southern DST
48
+ pytest --tzcheck --tzcheck-zones=UTC,America/New_York
49
+ pytest --tzcheck -k "tz=UTC" # only the UTC runs
50
+ ```
51
+
52
+ Or turn it on in config:
53
+
54
+ ```ini
55
+ [pytest]
56
+ tzcheck = true
57
+ tzcheck_zones = ci
58
+ ```
59
+
60
+ | Zone list | Zones |
61
+ |---|---|
62
+ | `ci` (default) | `local`, `UTC` |
63
+ | `wide` | `local`, `UTC`, `Pacific/Kiritimati` (UTC+14, the local date is not UTC's most of the day), `America/St_Johns` (half-hour offset), `Australia/Lord_Howe` (half-hour DST shift, southern hemisphere) |
64
+
65
+ `local` is the environment as you started pytest. Presets and IANA names can be mixed. Zones with the same UTC offsets all year are merged, so on a UTC CI runner `--tzcheck` does not double the run.
66
+
67
+ Opt a test out with `@pytest.mark.no_tzcheck`. A test can ask which zone it runs in through the `tzcheck_zone` fixture.
68
+
69
+ A failure in any zone fails the run, so `--tzcheck` works as a CI gate as well as a local check.
70
+
71
+ ## How it works
72
+
73
+ 1. Each test is parametrized over the zones (`test_x[tz=local]`, `test_x[tz=UTC]`), so the runs are ordinary pytest items: `-k`, `--lf`, JUnit XML and pytest-xdist all work.
74
+ 2. Around each run (setup, call and teardown) the plugin sets `TZ`, calls `time.tzset()`, and restores both afterwards. Subprocesses started by the test inherit the zone. If `tzlocal` is imported, its cache is reloaded.
75
+ 3. The outcome of each run is recorded, and the summary lists every test whose outcome differs between zones. A test that fails everywhere is not listed: that is a bug, but not a timezone bug.
76
+
77
+ ## Case study: a prefect PR
78
+
79
+ This plugin started from a real bug. While fixing RDATE/EXDATE handling in prefect's `RRuleSchedule` ([PrefectHQ/prefect#23436](https://github.com/PrefectHQ/prefect/pull/23436)), the new regression tests passed locally (Asia/Jerusalem) and failed in CI (UTC).
80
+
81
+ The cause: dateutil parses a timestamp ending in `Z` as `tzutc()`, unless the machine's local zone is UTC, in which case it returns `tzlocal()`. The fix checked `isinstance(dt.tzinfo, tzutc)`, which is true in Jerusalem and false in UTC. Nothing in that code path calls a local-time function; dateutil just compares against `time.tzname`.
82
+
83
+ Prefect's schedule tests on the commit that failed CI (`bb4f72475`):
84
+
85
+ ```
86
+ $ pytest tests/client/schemas/test_schedules.py tests/server/schemas/test_schedules.py
87
+ 155 passed, 6 skipped, 2 xfailed
88
+
89
+ $ pytest --tzcheck tests/client/schemas/test_schedules.py tests/server/schemas/test_schedules.py
90
+ =========================== timezone-dependent tests ===========================
91
+ tests/client/schemas/test_schedules.py::TestRRuleScheduleFromRRule::test_rruleset_keeps_rdate_and_exdate_instants_in_another_timezone passed: local (Asia/Jerusalem) failed: UTC
92
+ tests/server/schemas/test_schedules.py::TestRRuleSchedule::test_rrule_schedule_preserves_and_localizes_exdates passed: local (Asia/Jerusalem) failed: UTC
93
+ tests/server/schemas/test_schedules.py::TestRRuleSchedule::test_rrule_schedule_preserves_and_localizes_rdates passed: local (Asia/Jerusalem) failed: UTC
94
+ tests/server/schemas/test_schedules.py::TestRRuleSchedule::test_serialization_preserves_rdate_and_exdate_in_another_timezone passed: local (Asia/Jerusalem) failed: UTC
95
+ tests/server/schemas/test_schedules.py::TestRRuleSchedule::test_serialization_preserves_rrules_rdates_exrules_exdates passed: local (Asia/Jerusalem) failed: UTC
96
+ 5 of 163 tests changed outcome with the timezone
97
+ 5 failed, 305 passed, 12 skipped, 4 xfailed
98
+ ```
99
+
100
+ On the fixed commit (`9e24d058d`, which checks the UTC offset instead of the tzinfo type):
101
+
102
+ ```
103
+ $ pytest --tzcheck tests/client/schemas/test_schedules.py tests/server/schemas/test_schedules.py
104
+ tzcheck: no timezone-dependent tests (163 tests x 2 zones)
105
+ 310 passed, 12 skipped, 4 xfailed
106
+ ```
107
+
108
+ ## Why run every test, rather than detect the risky ones?
109
+
110
+ The cheaper design is to trace calls to local-time APIs (`time.localtime`, naive `datetime.now()`, `astimezone()` without arguments) and rerun only the tests that make them. It would have missed the bug above: in Jerusalem, dateutil only *reads* `time.tzname`, an attribute, so no call is made. Running each test in each zone and comparing outcomes has no false positives and no blind spots, at the cost of one extra run per zone. Linters such as ruff's `DTZ` rules cover the static side.
111
+
112
+ ## Limitations
113
+
114
+ - Fixtures with module, class or session scope are created once, in the zone of the first test that uses them. Values computed at import time (a module-level `tz.tzlocal()`, a cached `get_localzone()` result) also stay in the first zone.
115
+ - The zone is per process: threads running during the test see it too.
116
+ - POSIX only.
117
+ - libc silently treats an unknown `TZ` as UTC, so the plugin refuses zone names that are not in the system tz database.
118
+
119
+ ## Roadmap
120
+
121
+ - `--tzcheck-mode=probe`: trace local-time calls with `sys.monitoring` (Python 3.12+) and rerun only those tests in the extra zones, as a fast pre-filter for large suites, with the blind spot above documented.
122
+ - Run each zone at a few fixed instants (around midnight UTC, DST transitions), since many timezone bugs only show up at certain hours.
123
+ - A JSON report for CI annotations.
124
+
125
+ ## Development
126
+
127
+ ```bash
128
+ uv sync
129
+ uv run pytest
130
+ uv run ruff check . && uv run ruff format --check .
131
+ ```
132
+
133
+ To try it on another project without installing it: `PYTHONPATH=path/to/pytest-tzcheck/src pytest -p pytest_tzcheck.plugin --tzcheck`.
134
+
135
+ ## Releasing
136
+
137
+ Pushing a `v*` tag runs `.github/workflows/release.yml`: it checks the tag matches the version in `pyproject.toml`, runs lint and tests, builds, and publishes to PyPI through trusted publishing (no API token).
138
+
139
+ ```bash
140
+ uv version --bump patch # or minor / major
141
+ git commit -am "Release $(uv version --short)"
142
+ git tag "v$(uv version --short)"
143
+ git push origin main --tags
144
+ ```
145
+
146
+ ## License
147
+
148
+ MIT
@@ -0,0 +1,8 @@
1
+ pytest_tzcheck/__init__.py,sha256=TLUuyJx0N4tQFG3JAN50i9qkcgwP0IAu2bcDn-r-l8w,80
2
+ pytest_tzcheck/plugin.py,sha256=zRxBOrX3sy-5iIpGyNalKPlhMaAR0UPV5MkZTpKc1Jw,6207
3
+ pytest_tzcheck/zones.py,sha256=WwneTnJ__nVWiVzKoR0SN6Z1nR7AHeVnhIDlYhbLQz0,3534
4
+ pytest_tzcheck-0.1.0.dist-info/METADATA,sha256=PtOPHQF-mKEbgiFJOEDzLp5nhWWYAuWWCjj5oFOY_F8,7594
5
+ pytest_tzcheck-0.1.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
6
+ pytest_tzcheck-0.1.0.dist-info/entry_points.txt,sha256=NBsaePkT6Qv02MzXfPDTcGdBw7nvMa8AtKfH4a3sC9Q,43
7
+ pytest_tzcheck-0.1.0.dist-info/licenses/LICENSE,sha256=pPpLggfVf4TFbMr-FLE9gLslHnElioeve81adhHgzNI,1067
8
+ pytest_tzcheck-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.4
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [pytest11]
2
+ tzcheck = pytest_tzcheck.plugin
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ariel Even
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.