使用Thread.sleep进行测试 [英] Testing with Thread.sleep

查看:138
本文介绍了使用Thread.sleep进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Thread.sleep()来加快测试的建议方法是什么。

What are the recommended approaches to using Thread.sleep() to speed up tests.

我是在删除连接或发生超时错误等时测试具有重试功能的网络库。但是,库在重试之间使用 Thread.sleep()(因此赢了服务器重启时连接数千次。调用显着减慢了单元测试,我想知道选项是什么来覆盖它。

I am testing a network library with a retry functionality when connections are dropped or timeout errors occur, etc. The library however, uses a Thread.sleep() between the retries (so it won't connect thousands times while the server is restarting). The call is slowing the unit tests significantly, and I wonder what the options are to override it.

注意,我愿意实际更改代码,或使用模拟Thread.sleep()的模拟框架,但是想先听听你的意见/建议。

Note, I'm open to actually changing the code, or using a mocking framework to mock Thread.sleep(), but would like to hear your opinions/recommendation first.

推荐答案

通常是将与时间相关的功能委托给单独的组件的好主意。这包括获取当前时间,以及像Thread.sleep()这样的延迟。这样,在测试期间很容易用mock替换这个组件,并切换到不同的实现。

It is usually a good idea to delegate time-related functionality to a separate component. That include getting the current time, as well as delays like Thread.sleep(). This way it is easy to substitute this component with mock during testing, as well as switch to a different implementation.

这篇关于使用Thread.sleep进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆