如何在 Django 中对会话超时进行单元测试
我有一个这样的要求:
一旦用户注册(并且将处于等待状态,直到他确认他的电子邮件地址),会话变量就会被设置为类似“FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY”(抱歉,如果这个名字听起来令人困惑!)将被设置为一个日期时间对象,在当前时间上加上 8 小时。
这对用户有何影响,用户有 8 小时的时间来实际使用我们网站的所有功能,而无需确认他的注册电子邮件地址。 8 小时后,每个视图/页面都会显示一个大横幅,告诉用户确认。 (所有这些功能都是使用每个视图的单个“ensure_confirmed_user”装饰器来实现的)。
我想使用 django 的 unittest 插件(TestCase 类)测试相同的功能。我该怎么做?
更新:我是否需要手动更新提到的会话变量值(将 8 小时修改为几秒)才能完成?或者有什么更好的办法吗?
更新:这可能听起来很疯狂,但我想模拟来自未来的请求。
I have a requirement something like this:
As soon as the user signsup(and will be in the waiting state untill he confirms his email address), a session variable is set something like "FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY"(sorry if the name sounds confusing!) which will be set to a datetime object adding 8hrs to the current time.
How this should impact the user is, the user gets 8hrs time to actually use all the features of our site, without confirming his signedup email address. After 8hrs, every view/page will show a big banner telling the user to confirm. (All this functionality is achieved using a single "ensure_confirmed_user" decorator for every view).
I want to test the same functionality using django's unittest addon(TestCase class). How do I do it?
Update: Do I need to manually update the mentioned session variable value(modified 8hrs to a few seconds) so as to get it done? Or any better way is there?
Update: This may sound insane, but I want to simulate a request from the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,如果单元测试很困难,因为产品代码依赖于不合作的外部资源,您可以抽象出这些资源并用可以执行您想要的操作的虚拟资源替换它们。
在这种情况下,外部资源就是时间。不要使用 datetime.now(),而是重构代码以接受外部时间函数。它可以默认为 datetime.now。然后在单元测试中,您可以随着测试的进行更改时间。
这比将会话超时更改为几秒钟要好,因为即使这样,您也必须在测试中休眠几秒钟才能获得您想要的效果。单元测试应该尽可能快地运行,以便更频繁地运行。
Generally, if unit testing is difficult because the product code depends on external resources that won't cooperate, you can abstract away those resources and replace them with dummies that do what you want.
In this case, the external resource is the time. Instead of using datetime.now(), refactor the code to accept an external time function. It can default to datetime.now. Then in your unit tests, you can change the time as the test progresses.
This is better than changing the session timeout to a few seconds, because even then, you have to sleep for a few seconds in the test to get the effect you want. Unit tests should run as fast as you can get them to, so that they will be run more often.
我可以想到几种可能性。在测试运行期间,覆盖
FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY
变量并将其设置为较小的时间限制。然后,您可以等到该时间限制结束,然后验证该功能是否按预期工作。或者替换您自己的
datetime
函数(假设您的功能依赖于datetime
)您可以通过覆盖
setup_
和teardown_test_environment
来完成这些操作> 方法。I can think of a couple of possibilities. During your test run, override the
FIRST_TIME_FREE_SESSION_EXPIRY_AGE_KEY
variable and set it to a smaller time limit. You can then wait until that time limit is over and verify if the feature is working as expected.Alternately replace your own
datetime
functions (assuming your feature relies ondatetime
)You can accomplish these by overriding
setup_
andteardown_test_environment
methods.我的 settings.py 略有不同,具体取决于 django 是在生产环境中还是在开发环境中运行。我有 2 个设置模块:settings.py 和 settings_dev.py。开发版本如下所示:
现在您可以通过不同的方式解决您的问题:
您可以像这样使用活动设置模块:
My settings.py differs slightly depending on whether django is run in a production environment or in a development environment. I have 2 settings modules: settings.py and settings_dev.py. The development version looks as follows:
Now you can solve your problem in different ways:
You can use the active settings module like this: