A testing (and partial) replacement for the time package, for fully-controlled time-dependent tests.
A testing (and partial) replacement for Python's
time package, for fully-controlled time-dependent tests.
Testing functions that depend on time is always tricky, and can take long. To save time and avoid relying on
time.sleep(), this package allows to fast-forward time arbitrarily.
It implements the functions
Note: This package in not meant to replace
timein production! Only in testing environments!
The idea is to make your code to use
stime.test() instead of
time.test() while it is being tested.
Ideally, the code you want to test is receiving a time source through dependency injection, like the
Timer class in the example below.
If the code you are testing is not using dependency injection, it is likely that you can still override the time function in your tests. That is less elegant, but can be a fair trade-off to avoid having its test suite rely on
Once your code is using
stime, you can precisely control the output of
tick(n): increments the current time by
reset(t): (re-)sets the current time to the timestamp
Find the full code for this example in
# test_timer.py import unittest import stime # ① from timer import Timer # your package with time-dependent functions to be tested class TestTimer(unittest.TestCase): def test_timer_rings_for_five_seconds_starting_at_alarm_time(self): # create a new timer using stime as a time source cooking_timer = Timer(time_source=stime) cooking_timer.set_alarm(1561120200) # Unix timestamp for 21 June 2019 around noon stime.reset(1561120199) # ② a second before alarm time is_ringing = cooking_timer.is_ringing() # calls stime.time() because it is the timer time_source self.assertEqual(is_ringing, False, "expected the timer NOT to ring before alarm time") stime.reset(1561120200) # ③ exactly alarm time is_ringing = cooking_timer.is_ringing() # calls stime.time() because it is the timer time_source self.assertEqual(is_ringing, True, "expected the timer to ring at alarm time") stime.tick(5) # ④ 5 seconds after alarm time is_ringing = cooking_timer.is_ringing() # calls stime.time() because it is the timer time_source self.assertEqual(is_ringing, True, "expected the timer to be ringing 5 seconds after alarm time") stime.tick() # ⑤ add 1 more second is_ringing = cooking_timer.is_ringing() # calls stime.time() because it is the timer time_source self.assertEqual(is_ringing, False, "expected the timer NOT to be ringing 6 seconds after alarm time") # [...]
- ① Import
stimewhere you would have imported
timeif it wasn't testing.
- ② Set the current time to whatever is convenient...
- ③ Reset it as often as needed...
- ④ Fast-forward when convenient...
- ⑤ or progress one second at a time!
Optionally, create a virtual environment for this project and activate it.
python -m venv timer_venv # assuming Python 3 . timer_venv/bin/activate
Then do your thing!
# run the example test suite: python test_timer.py # once you're done deactivate the virtual environment if you use one: deactivate
# Install the latest setuptools and wheel (in a virtual environment eventually) pip install --upgrade setuptools wheel # Update the package version number and tag it: vim setup.py git tag -a 'v1.0.0' -m 'Initial release' git push origin master --tags # Build the distribution files make build # And upload them to PyPI make upload_to_pypi
The owl emoji in the header was rendered from an SVG that belongs to Google and was published under the Apache License v2.0 as part of Noto Emoji.
stime Copyright (C) 2019 Gonzalo Bulnes Guilpain This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size stime-1.0.0-py3-none-any.whl (16.9 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size stime-1.0.0.tar.gz (4.4 kB)||File type Source||Python version None||Upload date||Hashes View|