使用 python 进行日期敏感的回归测试

发布于 2024-09-29 19:43:12 字数 374 浏览 11 评论 0原文

嘿, 我正在帮助为我们的 python Web 应用程序设置回归测试套件。我们的许多测试都是安排式测试,其中当前日期很重要。例如:创建一个从 2 月 1 日开始每周运行一个月的重复事件。

为了测试这一点,我真正想做的是覆盖当前日期,以便我可以及时前后移动以检查状态该应用程序。例如,我可以添加一个仅测试页面,让我设置“当前”日期,该日期传递到 python 后端并用于日期计算。

过去,当我完成此操作时,我从第一天起就将其设计到应用程序架构中。不幸的是,我来得太晚了,并且没有对此的应用程序支持。

所以这是我的问题,有什么方法可以覆盖网络服务调用的当前日期吗?例如,我可以拦截当前日期的呼叫(可能是猴子修补吗?)。我宁愿不必做整个 IOC 的事情,因为这意味着改变数百个方法。 - 戴夫

Hey,
I am helping to set up a regression testing suite for our python web application. Many of our tests are scheduling style tests where the current date is important. For example: create a recurring event that runs every week for a month starting on Feb 1.

In order to test this, what I really want to do is override the current date so I can move back and forward in time to check the state of the app. For example, I may add an test-only page that lets me set the 'current' date which gets passed to the python back end and is used for date calculations.

In the past when I have done this, I engineered it into the application architecture from day 1. Unfortunately, I am coming in late to this project and there is no application support for this.

So here's my question, is there any way I can override the current date on a web service call? For example, can I intercept calls for the current date (monkey patching possibly?). I would rather not have to do a whole IOC thing as it would mean changing hundreds of methods.
- dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

趴在窗边数星星i 2024-10-06 19:43:13

你可以这样做:

from datetime import datetime
orig_datetime_now = datetime.now

datetime.now = lambda: datetime(2010, 2, 1)

# tests here

datetime.now = orig_datetime_now

不过,在你的类上放置一个辅助方法来获取当前日期,并进行与上面所示相同的猴子修补,这可能是一个更好的主意(不要与第三方库等混在一起) 。

You could do something like this:

from datetime import datetime
orig_datetime_now = datetime.now

datetime.now = lambda: datetime(2010, 2, 1)

# tests here

datetime.now = orig_datetime_now

Though, it's probably a better idea (as not to screw with third party libraries, etc.) to put a helper method on your class to get the current date, and do the same monkey patching as show above.

薄荷梦 2024-10-06 19:43:13

回答我自己的问题:

我们当前的计划是在虚拟机上进行测试并更改虚拟机上的日期/时间。这是一个更完整的解决方案,因为它同时获取数据库和工作线程。

当我们真正开始做的时候,我会发布一些现实世界经验的更新。

Answering my own question:

Our current plan is to test on virtual machines and change the date/time on the VMs. This is a more complete solution as it gets the database and worker threads all at once.

I'll post an update with some real world experience when we come to actual do it.

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