Skip to main content

Frozen date: freeze datetime.now at a known value for testing

Project description

Frozendate suspends time while your tests run.

Frozendate mocks datetime.datetime and datetime.date to provide known times when testing.

Usage

import frozendate

with frozendate.freeze(1999, 1, 1):
    party_like_its_1999()

# freeze relative freezes time relative to the current date...
with freeze_relative(days=-1):
    assert all_my_troubles_seemed_so_far_away()

with freeze(1999, 12, 31):
    # ...or relative to the previous freeze
    with freeze_relative(days=1):
        print "happy new year!"

Normally time doesn’t actually stop when you use freeze – it just starts again from the fixed point you specify, eg:

>>> import frozendate
>>> import datetime
>>> with frozendate.freeze(2000, 1, 1):
...     print datetime.now().replace(microsecond=0)
...     time.sleep(1)
...     print datetime.now().replace(microsecond=0)
...
2000-01-01 00:00:00
2000-01-01 00:00:01

But you can always get the same value back if you pass hard=True:

>>> with frozendate.freeze(2000, 1, 1, hard=True):
...     print datetime.now().replace(microsecond=0)
...     time.sleep(1)
...     print datetime.now().replace(microsecond=0)
...
2000-01-01 00:00:00
2000-01-01 00:00:00

Instead of a context manager there are also regular patch and unpatch functions. These are useful in test case setup/teardown methods:

def setUp(self):
    frozendate.patch(2000, 1, 1)

def tearDown(self):
    frozendate.unpatch()

When you call freeze or patch, it freezes time for all modules found in sys.modules. Sometimes you want to restrict to a few named modules:

frozendate.freeze(modules=['mypackage.mymodule'])

Or patch everything, but exclude a few modules that need the real datetime still:

frozendate.freeze(dontpatch=['somemodule', 'someotherpackage'])

0.1.4 (released 2022-10-04)

  • Bugfix: don’t attempt to patch pkg_resources

0.1.3 (released 2018-10-10)

  • Fix exception when used with dateutil

  • Add support for Python 3.7

  • Drop support for Python 2.6

Version 0.1.2

  • Add support for freezing datetime.utcnow()

Version 0.1.1

  • Fix for ImportErrors when used with six

Version 0.1

  • Initial release

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

frozendate-0.1.4.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

frozendate-0.1.4-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file frozendate-0.1.4.tar.gz.

File metadata

  • Download URL: frozendate-0.1.4.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/62.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.14

File hashes

Hashes for frozendate-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a39ec95ab3dd4bcb3451d1e794fd973499eaaf5fbf5825ab740ff8a35631345b
MD5 b5c2d695ab58d8803ba39cda797d8b62
BLAKE2b-256 26d37ee64872877c4f75d1ef65a37473355cff2bb96ed1d633a4c1614e835215

See more details on using hashes here.

File details

Details for the file frozendate-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: frozendate-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/62.1.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.7.14

File hashes

Hashes for frozendate-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 340d23c974f27b794512cb66937183d163f33b0cf6180a2a6a163d23fec8210b
MD5 8d5238a6a8e9e899ef1c605c1cf3adf7
BLAKE2b-256 7bf2282e1fe3a7d340238c92ab21e4f38c54441b04639ac2c168a0be521c4161

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