Python 鼻子测试中的计时问题
我有一些对时间敏感的单元测试:一个操作是定时的,如果花费太长时间就会触发错误。当单独运行时,这些测试会通过,但是当在我的模块上递归运行nosetest时,它们经常会失败。我运行并发测试,这可能是计时关闭的原因之一。有什么方法可以表明我希望此测试不间断地运行吗?
I have some unit tests that are timing sensitive: an action is timed and an error is triggered if it takes too long. When run individually, these tests pass, but when running nosetest recursively on my modules, they often fail. I run concurrent tests, which likely is one reason why the timing is off. Is there any way to indicate that I want this test to be run with no interruptions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题取决于你如何实施计时。我个人采用的解决方案是设置一个控制测试行为的环境变量。候选者可以是:
if WITH_TIMING == False
[完全关闭计时]TIME_STRETCH_FACTOR = ...
[在运行并发测试的情况下应用时间拉伸乘数,因此例如,如果TIME_STRETCH_FACTOR
为 1.5,时间限制 5 将变为 7.5]如果这不是一个选项,则可能会出现ugly 解决方法是模拟
time.time()
函数,使其返回一个常量值 [这仅在您使用time.time() 时有效
当然直接在你的测试中]...HTH
I think your problem is dependent from how you implemented the timing. The solution I would personally adopt would be to set an environment variable that controls the behaviour of the tests. Candidates could be:
if WITH_TIMING == False
[turn off timing altogether]TIME_STRETCH_FACTOR = ...
[apply a time-stretching multiplier in case of concurrent test are run, so that for example a time limit of 5 would become 7.5 ifTIME_STRETCH_FACTOR
would be 1.5]If this is not an option, a possible ugly workaround would be to mock the
time.time()
function, making it return a constant value [this would only work if you usetime.time()
in your tests directly of course]...HTH