Skip to main content

Fake timing tools for testing time-dependent functionality

Project description

Are you tired of unpredictable time values messing with your testing? Enter Tyminator, a testing toolkit that terminates uncertainty and boosts the reliability of your tests.

Just like how database fixtures provide predictable data sets, Tyminator brings that same level of predictability to time. Tyminator enables precise control over time-related operations in testing scenarios by seamlessly replacing the built-in Python time functions.

Importance of Predictable Time Test Fixtures

Test fixtures are the guardians of correctness and stability in software applications, setting the stage for tests with data, dependencies, and environment configurations. But when it comes to time-sensitive operations like scheduling events or handling timeouts, relying on the actual passage of time introduces non-deterministic behavior, making it challenging to reproduce and debug issues consistently.

By controlling time, developers can isolate specific scenarios, simulate time-based events, and test edge cases that would otherwise be difficult to replicate reliably. This control allows for comprehensive testing of time-sensitive functionality, including handling time zones, time-based calculations, and scheduling logic.

Key features

The library offers a range of features and methods to manipulate time, schedule events, calculate durations, and track elapsed time, ensuring accurate and repeatable test results.

  • Patching Time Functions: Tyminator seamlessly replaces the built-in Python time functions with a fake clock implementation. This allows tests to control and manipulate time-related operations without affecting or being affected by the actual system time.

  • Fine-Grained Time Manipulation: Test fixtures can move time forward in controlled increments, making it possible to simulate various time-based scenarios accurately. This enables comprehensive testing of time-dependent code paths.

  • Predictable Timestamp Generation: Test fixtures can generate consistent and predictable timestamps, eliminating non-deterministic behavior associated with real-time values. This ensures reliable test results and simplifies debugging.

  • Time Zone Support: Supports simulating alternate local time zones, allowing tests to ensure that time-dependent code handles regional changes correctly.

  • Events: Ability to specify specific times when for triggering events, helping to simulate specific sequences of normally unpredictable sequences in asynchronous code paths.

Installation

Tyminator is available as the Python tyminator package.

For example, to install using pip:

$ pip install tyminator

Usage with unittests

import time
import unittest
from datetime import datetime, timezone
from datetime import timedelta

from tyminator.clock import Clock
from tyminator.clock import installed

EPOCH = datetime(2016, 8, 29, 18, 18)
EASTERN_TIME = timedelta(hours=-5)

class MyTimeTest(unittest.TestCase):
    def setUp(self):
        self.clock = Clock(EPOCH, local_tz=timezone(EASTERN_TIME))

    def test_example(self):
        with installed(self.clock):
            now1 = datetime.fromtimestamp(time.time())
            assert now1 == EPOCH

            now2 = datetime.fromtimestamp(time.time())
            assert now2 == EPOCH + self.clock.step

            naive_utc = EPOCH + self.clock.step * 2 - EASTERN_TIME
            assert self.clock.current_utc_datetime == naive_utc.replace(tzinfo=timezone.utc)

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

tyminator-1.0.1.tar.gz (10.3 kB view details)

Uploaded Source

File details

Details for the file tyminator-1.0.1.tar.gz.

File metadata

  • Download URL: tyminator-1.0.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for tyminator-1.0.1.tar.gz
Algorithm Hash digest
SHA256 272ab64a1f0c0d6260676e8fd6d68df6bbff52355a688f5863891adbaef433ae
MD5 ee5a5e6a5ec24b342d9b39daad768958
BLAKE2b-256 ae1b51a14abd581d7e61a016c4a34d504db4df014af4cfbf2c699875a69c3eb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for tyminator-1.0.1.tar.gz:

Publisher: release.yml on milton-hirsch-institute/tyminator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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