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-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84ad6326236c8a0a0f0eac681c4f1ed3c5612196df5bd82521442f654bf9d552 |
|
MD5 | 741a1b8d71061b0eb8757a4f3926136b |
|
BLAKE2b-256 | a9e80406f1db65be3db3936b4bc99cfb5a02a0921b3b7f2bdbe4b5001bb0b9bf |
Close
Hashes for pytest_freezeblaster-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 952344d12f6b8c61d13329cee03edc0136e50c51bb452412f1f1ef8e6adbff04 |
|
MD5 | 456610bfe7fdc6a5206797a0fc4220a2 |
|
BLAKE2b-256 | 77eb2f197fbe9673e85432b52bb0cbb905b9b49df5a6ad2cecf82dcba1bfe8b7 |