时间包的测试(和部分)替换,用于完全控制的时间相关测试。

stime的Python项目详细描述


An owl emoji

模拟时间

对python的time包的测试(和部分)替换,用于完全受控的时间相关测试。

PyPI



测试依赖于时间的函数总是很棘手,而且可能需要很长时间。为了节省时间并避免依赖于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/>.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
tomcat java。lang.NoClassDefFoundError:无法初始化类com。太阳代理$Proxy8   java Spring引导CommandLineRunner异常处理建议   java文件。getParentFile()。mkdirs();在osx中创建重复文件夹   java Lucene IndexWriter线程安全   HashMapJava加速了哈希映射的创建   java执行器服务从Servlet线程启动   guava在Java中创建异构列表   泛型Java将比较器传递给构造函数   在Java中生成公钥和私钥的加密会生成类似的所有密钥,包括私钥和公钥   java ResourceLocation抛出FileNotFoundException Minecraft Forge 1.12.2   java使用动态键序列化JSON响应   在空映射上调用方法时发生java NullPointerException   java在Selenium中选择不同行中的按钮   优化在Java中,降低时间和空间复杂性的最佳方法是什么?   java表。setValueAt(…)不为JTable工作   javajsonrpc与JSON   ant从java编译中排除类的特定方法