API Reference

This page provides a detailed reference for the public API of pytest-tzshift, including the main fixture, data objects, constants, and the underlying Pytest hooks that power the plugin.

This documentation is generated directly from the source code's docstrings.

Fixture

The primary way to interact with this plugin is through the tzshift fixture.

tzshift

Requesting the tzshift fixture in a test function is what triggers the timezone and locale parametrization for that test. The fixture is responsible for setting up the environment before your test runs and safely tearing it down afterward.

During the test run, it yields a TzShift object, giving you read-only access to the active settings.

A pytest fixture that sets the timezone and locale for a single test run.

For each parametrized test case, this fixture: 1. Saves the original TZ environment variable and system locale. 2. Sets the TZ environment variable and the LC_ALL locale according to the current test's parameters. A special value of "SYSTEM" for either setting will leave the original system value untouched. 3. Calls time.tzset() to ensure the operating system recognizes the timezone change. 4. Yields a TzShift object containing the active (timezone, locale) pair. 5. After the test completes, it reliably restores the original timezone and locale settings.

Example

from pytest_tzshift import TzShift
import time
import locale

def test_time_and_locale_behavior(tzshift: TzShift):
    """
    A test that uses the tzshift fixture.

    This test will be run for each combination of configured
    timezones and locales.
    """
    # The `tzshift` object provides the active settings for this run.
    print(f"Active Timezone: {tzshift.timezone}")
    print(f"Active Locale:   {tzshift.locale}")

    # The environment reflects these settings.
    # Note: time.tzname will reflect the active timezone.
    print(f"time.tzname: {time.tzname}")

    # Note: locale.getlocale() will reflect the active locale.
    print(f"locale.getlocale(locale.LC_ALL): {locale.getlocale(locale.LC_ALL)}")

    # Your test logic here...
    assert True

Data Objects

The plugin uses a simple data object to pass information to your tests.

TzShift

This object is yielded by the tzshift fixture.

An immutable data object holding the active timezone and locale.

This object is yielded by the tzshift fixture to give tests read-only access to the environment settings for the current parametrized run.

It behaves like a 2-element tuple, supporting unpacking, indexing, and len().

Attributes:
  • timezone (str) –

    The IANA timezone name (e.g., "America/New_York") or the "SYSTEM" sentinel string.

  • locale (str) –

    The locale identifier (e.g., "en_US.UTF-8") or the "SYSTEM" sentinel string.

as_tuple()

Helper for explicit tuple.

Example

from pytest_tzshift import TzShift

def test_unpacking(tzshift: TzShift):
    # Unpack like a tuple
    tz, loc = tzshift

    assert tz == tzshift.timezone
    assert loc == tzshift.locale

    # Access by index
    assert tzshift[0] == tzshift.timezone
    assert tzshift[1] == tzshift.locale

    # Get length
    assert len(tzshift) == 2

    # Explicitly convert to a tuple
    assert tzshift.as_tuple() == (tz, loc)

Constants

The plugin exposes sentinel values that have special meaning in configuration.

SYSTEM_TZ

A sentinel string used in timezone lists to indicate that the original system timezone should be used as one of the parameterization values.

SYSTEM_LOCALE

A sentinel string used in locale lists to indicate that the original system locale should be used as one of the parameterization values.

Pytest Hooks

For advanced users and contributors, pytest-tzshift implements the following standard Pytest hooks. You do not need to interact with these directly to use the plugin, but they are documented here for completeness. The user-facing results of these hooks are the command-line options, pytest.ini settings, and the @pytest.mark.tzshift marker.

pytest_generate_tests

This is the core engine of the plugin, responsible for creating the parametrized test runs.

A pytest hook that parametrizes tests requesting the 'tzshift' fixture.

This is the core engine of the plugin. It performs the following steps: 1. Checks if a test function requests the tzshift fixture. 2. Gathers timezone and locale lists from CLI options or the pytest.ini file. 3. Filters these lists to include only those valid on the current system. 4. Checks for @pytest.mark.tzshift to apply test-specific overrides. 5. Generates the Cartesian product of the final timezone and locale lists. 6. Caps the number of combinations if --tzshift-max is set. 7. Injects the (timezone, locale) pairs into the test run.

pytest_addoption

This hook adds the command-line and pytest.ini configuration options. For details on using these options, see the Configuration guide.

A pytest hook that adds command-line and ini-file options.

This function defines the configuration interface for the plugin, allowing users to specify default timezones, locales, and other settings globally for the test suite.

Options Added

--tz-timezones: Comma-separated list of timezones. --tz-locales: Comma-separated list of locales. --tzshift-max: Integer cap on the number of generated tests. --no-tzshift: A flag to disable the plugin entirely.

pytest_configure

This hook registers the tzshift marker, allowing for per-test or per-scope overrides.

A pytest hook that registers the 'tzshift' marker.

This allows tests to be marked with @pytest.mark.tzshift(...) to customize parametrization for a specific test, class, or module.

The marker signature is

tzshift(timezones=None, locales=None, disable=False)

Parameters:
  • timezones

    A list of timezone names to use for this scope.

  • locales

    A list of locale identifiers to use for this scope.

  • disable

    If True, disables tzshift parametrization for this scope.