GAE:带有测试床的单元测试任务队列
我正在使用测试床对我的谷歌应用程序引擎应用程序进行单元测试,并且我的应用程序使用任务队列。
当我在单元测试期间将任务提交到任务队列时,看起来该任务在队列中,但该任务并未执行。
如何在单元测试期间执行任务?
I'm using testbed to unit test my google app engine app, and my app uses a taskqueue.
When I submit a task to a taskqueue during a unit test, it appears that the task is in the queue, but the task does not execute.
How do I get the task to execute during a unit test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Saxon 的出色答案,我可以使用 testbed 而不是 gaetestbed 来做同样的事情。这就是我所做的。
将其添加到我的
setUp()
中:然后,在我的测试中,我使用了以下内容:
在此过程中,POST 参数进行了 Base64 编码,因此必须撤消该操作才能使其正常工作。
我比 Saxon 的答案更喜欢这个,因为我可以使用官方测试平台包,并且可以在我自己的测试代码中完成这一切。
编辑:我后来想对使用延迟库提交的任务做同样的事情,并且花了一些时间才弄清楚,所以我在这里分享是为了减轻其他人的痛苦。
如果您的任务队列仅包含使用延迟提交的任务,那么这将运行所有任务以及这些任务排队的任何任务:
Using Saxon's excellent answer, I was able to do the same thing using testbed instead of gaetestbed. Here is what I did.
Added this to my
setUp()
:Then, in my test, I used the following:
Somewhere along the line, the POST parameters get base64 encoded so had to undo that to get it to work.
I like this better than Saxon's answer since I can use the official testbed package and I can do it all within my own test code.
EDIT: I later wanted to do the same thing with tasks submitted using the deferred library, and it took a bit of headbanging to figure it, so I'm sharing here to ease other people's pain.
If your taskqueue contains only tasks submitted with deferred, then this will run all of the tasks and any tasks queued by those tasks:
实现此目的的另一个(更干净的)选项是使用测试台内的任务队列存根。为此,您首先必须通过将以下内容添加到
setUp()
方法来初始化任务队列存根:可以使用以下代码访问任务调度程序:
使用队列存根的接口是如下:
此外,由于它使用 App Engine SDK 自己的设施 - 它与延迟库一起工作得很好。
Another (cleaner) option to achieve this is to use the task queue stub within the testbed. To do this you first have to initialize the task queue stub by adding the following to your
setUp()
method:The tasks scheduler can be accessed using the following code:
The interface for working with the queue stub is as follows:
Also, as this uses App Engine SDK own facilities - it works just fine with the deferred library.
开发应用程序服务器是单线程的,因此当前台线程运行测试时,它无法在后台运行任务。
我修改了 gaetestbed 中的 taskqueue.py 中的 TaskQueueTestCase 以添加以下函数:
为了使此功能正常工作,我还必须将 TaskQueueTestCase 的基类从 BaseTestCase 更改为 WebTestCase。
然后,我的测试会执行如下操作:
因此,这会直接从前台单元测试执行任务。这与生产中不太一样(即,任务将在“一段时间后”根据单独的请求执行),但它对我来说已经足够好了。
The dev app server is single-threaded, so it can't run tasks in the background while the foreground thread is running the tests.
I modified TaskQueueTestCase in taskqueue.py in gaetestbed to add the following function:
For this to work, I also had to change the base class of TaskQueueTestCase from BaseTestCase to WebTestCase.
My tests then do something like this:
This therefore executes the task directly from the foreground unit test. This is not quite the same as in production (ie, the task will get executed 'some time later' on a separate request), but it works well enough for me.
您可能想尝试以下代码。完整说明在这里: http://www.geewax .org/task-queue-support-in-app-engines-ext-testbed/
You might want to try the following code. Full explanation is here: http://www.geewax.org/task-queue-support-in-app-engines-ext-testbed/