Python 鼻子测试中的计时问题

发布于 2024-12-17 12:51:05 字数 137 浏览 1 评论 0原文

我有一些对时间敏感的单元测试:一个操作是定时的,如果花费太长时间就会触发错误。当单独运行时,这些测试会通过,但是当在我的模块上递归运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼痛 2024-12-24 12:51:05

我认为你的问题取决于你如何实施计时。我个人采用的解决方案是设置一个控制测试行为的环境变量。候选者可以是:

  • 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 if TIME_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 use time.time() in your tests directly of course]...

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文