Skip to main content

Datetime mocking in tests

Project description

https://circleci.com/gh/closeio/freezefrog/tree/master.svg?style=svg&circle-token=010565a97316df8a248f0f32d584357021a3873b

FreezeFrog lets you mock datetimes in tests.

(Interested in working on projects like this? Close.io is looking for great engineers to join our team.)

Why FreezeFrog?

FreezeFrog is a Python library that lets you mock datetimes in tests. Its goal is to be simple and fast.

  • In comparison to certain other time freezing libraries, FreezeFrog doesn’t loop through all imported modules, making it fast even for larger projects.

  • FreezeFrog currently supports mocking the following basic methods:

    • datetime.datetime.now

    • datetime.datetime.utcnow

    • time.time

  • FreezeFrog supports both datetime and pytz timezone objects.

Usage

Use the FreezeTime context manager to freeze the time. Pass the desired datetime object to the constructor, and the timezone to mock the system’s timezone. The constructor also takes the fold argument (0 by default), which defines whether an ambiguous time refers to its first or second appearance, and the tick argument (False by default), which makes the clock start ticking.

import datetime

from freezefrog import FreezeTime

UTC = datetime.timezone.utc

with FreezeTime(datetime.datetime(2014, 1, 1), UTC):
    # The clock is frozen.
    # Always prints 2014-01-01 00:00:00
    print(datetime.datetime.utcnow())

with FreezeTime(datetime.datetime(2014, 1, 1), UTC, tick=True):
    # The clock starts ticking immediately.
    # Example output: 2014-01-01 00:00:00.000005
    print(datetime.datetime.utcnow())

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

freezefrog-0.4.0.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file freezefrog-0.4.0.tar.gz.

File metadata

  • Download URL: freezefrog-0.4.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for freezefrog-0.4.0.tar.gz
Algorithm Hash digest
SHA256 6bab35af099849e34c31eee4b03126312d72040488ae6ab5b4de6dce0cdb7f6e
MD5 284b4dee6c2328382ff1b813b9346d6a
BLAKE2b-256 e620b58ae94551d5cd4ac74d0438dec13b2a16f4b45f95dd220f2c0e7b313f81

See more details on using hashes here.

Supported by

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