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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | db926708da48c63fe1e8adf21e5fa0cae4462a53215053ad58d910d7fc5a6ec7 |
|
MD5 | cef4d584dca4cfbb93a05ff8de3bc170 |
|
BLAKE2b-256 | f435d3099bd44c80287fac8a209abbb0efbbc22475646b8313dbd13bd4bb6f02 |
Close
Hashes for pytest_freezeblaster-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60ce63a3f82edd9bb3a8014b39891d64c3d9db953b8a5b0cef7669713e1ef4e7 |
|
MD5 | 5c9b54779e19e2c90b8a7d63415c2338 |
|
BLAKE2b-256 | 031efc859e078cdfe087037c84ccda1466518e83d87c24f0e599ecabc1150e35 |