AppEngine 中的单元测试任务队列
很长一段时间以来,我一直在 AppEngine 上使用任务队列来安排任务,就像我应该做的那样。
但我一直想知道如何为此编写测试?到目前为止,我只是进行了测试以确保对任务进行排队的 API 上不会发生错误,然后为执行任务的 API 编写更合适的测试。
然而最近我开始对此感到有点不满意,我正在寻找一种方法来实际测试正确的任务是否已添加到正确的队列中。希望这可以比简单地部署代码并希望得到最好的结果更好。
我正在使用 django-nonrel,如果这对答案有任何影响的话。
回顾一下:如何编写单元测试来确认任务已排队?
For a very long time now I've been using task queues on AppEngine to schedule tasks, just the way I'm supposed to.
But what I've always been wondering is how does one write tests for that? Until now I've simply made tests to make sure an error doesn't occur on the API that queues a task and then wrote the more proper tests for the API executing the task.
However lately I've started feeling a bit unsatisfied by this and I'm searching for a way to actually test that the correct task has been added to the correct queue. Hopefully this can be done better than simply by deploying the code and hoping for the best.
I'm using django-nonrel, if that has any bearing on the answer.
To recap: How can a unit test be written to confirm tasks have been queued?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用
google.appengine.ext.testbed
的 GAE Testbed(GAE Testbed 现已弃用并移至ext.testbed
),您可以执行以下操作:If you're using
google.appengine.ext.testbed
instead of GAE Testbed (GAE Testbed is now deprecated and being moved intoext.testbed
), you can do the following:使用 GAE Test Bed 将允许您删除任务队列。
如果您继承自
FunctionalTestCase
或TaskQueueTestCase
,您将获得诸如get_tasks
和assertTasksInQueue
之类的方法。您实际上也可以运行任务。根据您使用任务还是延迟,具体操作方法会有所不同。
对于延迟,我有一些像这样的代码:
运行任务是类似的,但是在获取任务之后,您使用 WebTest(GAE 测试床构建于其之上)以 POST 请求及其参数提交到任务的 URL。
Using GAE Test Bed will allow you to stub out a task queue.
If you inherit from
FunctionalTestCase
orTaskQueueTestCase
, you'll get methods such asget_tasks
andassertTasksInQueue
.You can actually run the tasks, too. How to do it differs depending on whether you use tasks or deferred.
For deferreds, I have some code like this:
Running tasks is similar, but after you fetch the task, you use WebTest (which GAE Test Bed is built on top of) to submit as POST request to the task's URL with its parameters.
有一个名为 gaetestbed 的小测试框架可能适合您的需要。详情请参考:https://github.com/jgeewax/gaetestbed。
该测试环境与nose、nose-gae 插件和WebTest 包结合使用。就我而言,给定 python 包的混合是测试 GAE 应用程序的最佳方法。
There is a little test framework called gaetestbed which may suit your need. For details please refer to: https://github.com/jgeewax/gaetestbed.
This testing environment works in connection with nose, nose-gae plugin and WebTest package. Given mix of python packages is the best way to test GAE applications as far as I'm concerned.
SDK 1.4.3 Testbed API 提供了用于本地集成测试的存根库的轻松配置。
任务队列
的服务存根可用。The SDK 1.4.3 Testbed API provides easy configuration of stub libraries for local integration tests.
A Service stubs for
Task Queue
is available.