时间包的测试(和部分)替换,用于完全控制的时间相关测试。
stime的Python项目详细描述
模拟时间
对python的time
包的测试(和部分)替换,用于完全受控的时间相关测试。
测试依赖于时间的函数总是很棘手,而且可能需要很长时间。为了节省时间并避免依赖于time.sleep()
,这个包允许任意地快进时间。
它实现了stime.time()
和stime.monotonic()
函数。
Note: This package in not meant to replace
time
in production! Only in testing environments!
用法
其思想是让您的代码在测试时使用stime.test()
,而不是time.test()
。
理想情况下,您要测试的代码是通过依赖注入接收时间源,如下面示例中的Timer
类。
如果您正在测试的代码没有使用依赖注入,那么您很可能仍然可以在测试中重写time函数。这不太优雅,但可以是一个公平的权衡,以避免其测试套件依赖于time.sleep()
。
一旦代码使用stime
,就可以使用stime.reset()
和stime.tick()
:
stime.time()
(或stime.monotonic()
)的输出。
tick(n)
:将当前时间增加n
秒(例如1.3
秒)reset(t)
:(re-)将当前时间设置为时间戳t
(例如1561120200
)
示例
在^{
# test_timer.pyimportunittestimportstime# ①fromtimerimportTimer# your package with time-dependent functions to be testedclassTestTimer(unittest.TestCase):deftest_timer_rings_for_five_seconds_starting_at_alarm_time(self):# create a new timer using stime as a time sourcecooking_timer=Timer(time_source=stime)cooking_timer.set_alarm(1561120200)# Unix timestamp for 21 June 2019 around noonstime.reset(1561120199)# ② a second before alarm timeis_ringing=cooking_timer.is_ringing()# calls stime.time() because it is the timer time_sourceself.assertEqual(is_ringing,False,"expected the timer NOT to ring before alarm time")stime.reset(1561120200)# ③ exactly alarm timeis_ringing=cooking_timer.is_ringing()# calls stime.time() because it is the timer time_sourceself.assertEqual(is_ringing,True,"expected the timer to ring at alarm time")stime.tick(5)# ④ 5 seconds after alarm timeis_ringing=cooking_timer.is_ringing()# calls stime.time() because it is the timer time_sourceself.assertEqual(is_ringing,True,"expected the timer to be ringing 5 seconds after alarm time")stime.tick()# ⑤ add 1 more secondis_ringing=cooking_timer.is_ringing()# calls stime.time() because it is the timer time_sourceself.assertEqual(is_ringing,False,"expected the timer NOT to be ringing 6 seconds after alarm time")# [...]
- ①导入
stime
,如果不是测试的话,您可以导入time
。 - ②将当前时间设置为任何方便的时间…
- ③根据需要经常复位……
- ④方便时快进……
- ⑤或一次前进一秒!
开发
开始
(可选)为此项目创建虚拟环境并将其激活。
python -m venv timer_venv # assuming Python 3
. timer_venv/bin/activate
那就做你的事!
# 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
学分
标题中的owl emoji是从属于google和was published under the Apache License v2.0 as part of Noto Emoji的svg呈现的。
许可证
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/>.