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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for pytest_freezeblaster-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45a98eb952b5ea1077da1bc27bb57a2a43ae03806f67edb76fc69c2a051761a3 |
|
MD5 | 99cfef871e3ce7d084a912d27ef2aab6 |
|
BLAKE2b-256 | 17099eb78c2c5990ae451533b52c9375836cbff1b18b796b5de12eb1aebad361 |
Close
Hashes for pytest_freezeblaster-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed7c83cdd3851aaa366b98c40cbc8f18dca5413dd0c280a7b227d64eac6a8e2e |
|
MD5 | 638a46482ca3fb1aa5d319aae0bf6a7b |
|
BLAKE2b-256 | c8674196e1b6ed6d7c3ee40055af3fd8933f24bf71c050df68a9f3e98862279b |