Skip to main content

Wrap tests with fixtures in freeze_time

Project description

pytest-freezeblaster

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-freezeblaster" via pip from PyPI:

$ pip install pytest-freezeblaster

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)

Credits

All credits go to ktosiek.

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_freezeblaster-2.0.4.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_freezeblaster-2.0.4-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_freezeblaster-2.0.4.tar.gz.

File metadata

  • Download URL: pytest_freezeblaster-2.0.4.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_freezeblaster-2.0.4.tar.gz
Algorithm Hash digest
SHA256 b7435da9870030a6e8988a1ec2b0011814d5d34eb9df0c980ed12a1aca8a4d61
MD5 b0dc39e41cd46b009d7c6f44438cdf51
BLAKE2b-256 d85ec0ec750a193ae5666f03c36ae437f680c82886b085d976e74dd3cbb9dfd6

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_freezeblaster-2.0.4.tar.gz:

Publisher: release.yml on jammymalina/pytest-freezeblaster

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

File details

Details for the file pytest_freezeblaster-2.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_freezeblaster-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8bc7db545e93e3f438af6176b08535a85f4af3ee6ddc6a96474f1641bd0d37f8
MD5 3e6a7aaf968f0f518c4e9b71a640524f
BLAKE2b-256 24e14c8dd7415047d86e0ee8c6ef1abbf994c3cab242fda45040c3fea11d69a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_freezeblaster-2.0.4-py3-none-any.whl:

Publisher: release.yml on jammymalina/pytest-freezeblaster

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