Skip to main content

Wrap tests with fixtures in freeze_time

Project description

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.

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

pytest-freezegun-0.4.1.zip (9.1 kB view details)

Uploaded Source

Built Distribution

pytest_freezegun-0.4.1-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-freezegun-0.4.1.zip.

File metadata

  • Download URL: pytest-freezegun-0.4.1.zip
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.5

File hashes

Hashes for pytest-freezegun-0.4.1.zip
Algorithm Hash digest
SHA256 060cdf192848e50a4a681a5e73f8b544c4ee5ebc1fab3cb7223a0097bac2f83f
MD5 020d9a01e4bd018af7eb2a34789d2692
BLAKE2b-256 4f36b571371da5cb33bede2764f3e63cd77475c350d2e2a7db2abc2f1008dbf4

See more details on using hashes here.

File details

Details for the file pytest_freezegun-0.4.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_freezegun-0.4.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.5

File hashes

Hashes for pytest_freezegun-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1c77b2917883b8abf817fc39a1fd3f40ac43711eb923512abe7b6557e55eaaf1
MD5 e3be5e4c76fcbf79995321fbd49ae680
BLAKE2b-256 8555abc6b25af154cb82b0119335131ce1c40b6ce55652af9d3519cdbaef1992

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