Skip to main content

pytest-freezegun

https://img.shields.io/pypi/v/pytest-freezegun.svg See Build Status on Travis CI See Build Status on AppVeyor

Wrap tests with fixtures in freeze_time

Features

  • Freeze time in both the test and fixtures

  • Access the freezer when you need it

Installation

You can install “pytest-freezegun” via pip from PyPI:

$ pip install pytest-freezegun

Usage

Freeze time by using the freezer fixture:

def test_frozen_date(freezer):
    now = datetime.now()
    time.sleep(1)
    later = datetime.now()
    assert now == later

This can then be used to move time:

def test_moving_date(freezer):
    now = datetime.now()
    freezer.move_to('2017-05-20')
    later = datetime.now()
    assert now != later

You can also pass arguments to freezegun by using the freeze_time mark:

@pytest.mark.freeze_time('2017-05-21')
def test_current_date():
    assert date.today() == date(2017, 5, 21)

The freezer fixture and freeze_time mark can be used together, and they work with other fixtures:

@pytest.fixture
def current_date():
    return date.today()

@pytest.mark.freeze_time
def test_changing_date(current_date, freezer):
    freezer.move_to('2017-05-20')
    assert current_date == date(2017, 5, 20)
    freezer.move_to('2017-05-21')
    assert current_date == date(2017, 5, 21)

They can also be used in class-based tests:

class TestDate:

    @pytest.mark.freeze_time
    def test_changing_date(self, current_date, freezer):
        freezer.move_to('2017-05-20')
        assert current_date == date(2017, 5, 20)
        freezer.move_to('2017-05-21')
        assert current_date == date(2017, 5, 21)

Contributing

Contributions are very welcome. Tests can be run with tox. You can later check coverage with coverage combine && coverage html. Please try to keep coverage at least the same before you submit a pull request.

License

Distributed under the terms of the MIT license, “pytest-freezegun” is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This Pytest plugin was generated with Cookiecutter along with @hackebrot’s Cookiecutter-pytest-plugin template.

Metadata

Release files for pytest-freezegun 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-freezegun 0.4.2
File Size Uploaded
pytest-freezegun-0.4.2.zip 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-freezegun 0.4.2
File Interpreter ABI Platform
pytest_freezegun-0.4.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 13.6 kB

Release files / pytest-freezegun-0.4.2.zip

Download URL pytest-freezegun-0.4.2.zip
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
19c82d5633751bf3ec92caa481fb5cffaac1787bd485f0df6436fd6242176949
BLAKE2b-256 checksum
How to use checksums
f0e3c39d7c3d3afef5652f19323f3483267d7e6b0d9911c3867e10d6e2d3c9ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2

Release files / pytest_freezegun-0.4.2-py2.py3-none-any.whl

Download URL pytest_freezegun-0.4.2-py2.py3-none-any.whl
Size 4.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5318a6bfb8ba4b709c8471c94d0033113877b3ee02da5bfcd917c1889cde99a7
BLAKE2b-256 checksum
How to use checksums
9e090bdd7d24b9d21453ad3364ae1efbd65082045bb6081b5fd5eade91a9b644
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.3.0

1 release file

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page