Skip to main content

A Pytest plugin that transparently re-runs tests under a matrix of timezones and locales.

Project description

pytest-tzshift

PyPI version CI License

A tiny Pytest plug-in that automatically re-runs your tests under a matrix of time-zones and locales.

Why?

Time-zones and locales are global process settings. If your code formats dates, parses user input, or depends on datetime.now(), it may behave differently on users' machines.

pytest-tzshift attempts to make these differences visible:

tests/test_format.py::test_human_readable[0|UTC|C]                  PASSED
tests/test_format.py::test_human_readable[1|America/New_York|C]     PASSED
tests/test_format.py::test_human_readable[2|Europe/Berlin|de_DE]    FAILED

Installation

pip install pytest-tzshift          # requires Python ≥ 3.9

No further setup is necessary. The plug-in is auto-discovered by Pytest.

Quick start

Add the tzshift fixture to any test:

def test_price_formatting(tzshift):
    tz, loc = tzshift              # tuple-unpack

    price = 1234.56
    formatted = format_price(price)    # your code

    assert formatted.endswith("€") if loc.startswith("de_") else "€" not in formatted

Run Pytest as usual; each test will execute for every (timezone, locale) pair.

Default matrix

Time-zones (IANA) Locales (POSIX / glibc)
UTC C
America/New_York en_US.UTF-8
Europe/London de_DE.UTF-8
Asia/Kolkata fr_FR.UTF-8
Australia/Sydney ja_JP.UTF-8
Asia/Tokyo

Unavailable zones/locales are skipped with a warning.

Customising the matrix

Project-wide (pytest.ini)

[pytest]
tz_timezones =
    SYSTEM          # keep OS default
    Europe/Paris
tz_locales =
    SYSTEM
    fr_FR.UTF-8
tzshift_max = 10     # cap the total Cartesian product (0 = unlimited)

Command line

pytest --tz-timezones=UTC,Asia/Tokyo --tz-locales=C,ja_JP.UTF-8
pytest --tzshift-max=20
pytest --no-tzshift                     # disable for this run

Per-test marker

import pytest

@pytest.mark.tzshift(timezones=["UTC"], locales=["C"])
def test_once_only(tzshift):
    ...

@pytest.mark.tzshift(disable=True)
def test_native_env():
    ...

Platform notes

  • On Windows, time.tzset() is missing; the time-zone part becomes a no-op (locales still work). You'll still see separate parametrised runs, but all in the system zone.
  • Changing the process locale is global; avoid running pytest-tzshift with parallel workers.

Documentation

  • Quick startdocs/usage/quickstart.md
  • Configuration detailsdocs/usage/configuration.md
  • Markers & fixturesdocs/usage/markers.md
  • API referencedocs/reference/api.md

Browse the full site at https://spedr.github.io/pytest-tzshift once published.

Contributing

Bug reports, feature ideas, and pull requests are warmly welcome! See CONTRIBUTING for tips on setting up a dev environment, coding style, and running the test suite.

License

Released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_tzshift-0.1.1.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_tzshift-0.1.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_tzshift-0.1.1.tar.gz.

File metadata

  • Download URL: pytest_tzshift-0.1.1.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for pytest_tzshift-0.1.1.tar.gz
Algorithm Hash digest
SHA256 37b3c539e98561f5b023ab855ce53389d4619c5750e35cc24ae1d457af082397
MD5 16de4dae68257f7b201cb70cb94ba573
BLAKE2b-256 eb675734b392d633c75abd255e3a47ae6c67db3b6e5a58cad3d7e012806070f9

See more details on using hashes here.

File details

Details for the file pytest_tzshift-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_tzshift-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for pytest_tzshift-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7e2ade04672bbf899a497780e91e630ffc31c72f37bf28d420b6b717f1584e75
MD5 f929730f0e954a3c23c13d845d998813
BLAKE2b-256 0b966239e3e01f658b524e1d3f88e332e42bf52216180e32c0d66879753efa0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page