.. image:: https://travis-ci.org/ktosiek/pytest-freezegun.svg?branch=master :target: https://travis-ci.org/ktosiek/pytest-freezegun :alt: See Build Status on Travis CI
.. image:: https://ci.appveyor.com/api/projects/status/github/ktosiek/pytest-freezegun?branch=master&svg=true :target: https://ci.appveyor.com/project/ktosiek/pytest-freezegun/branch/master :alt: See Build Status on AppVeyor
Wrap tests with fixtures in freeze_time
You can install "pytest-freezegun" via
pip install pytest-freezegun
Freeze time by using the
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
@pytest.mark.freeze_time('2017-05-21') def test_current_date(): assert date.today() == date(2017, 5, 21)
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: 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)
Contributions are very welcome.
Tests can be run with
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.
Distributed under the terms of the
MIT_ license, "pytest-freezegun" is free and open source software
If you encounter any problems, please
file an issue_ along with a detailed description.
Pytest plugin was generated with
Cookiecutter along with
file an issue: https://github.com/ktosiek/pytest-freezegun/issues